nooz/notemanager.py

55 lines
1.5 KiB
Python
Raw Normal View History

2013-01-19 11:05:28 +00:00
# -*- coding: utf-8 -*-
import os
import sqlite3
2013-01-19 21:45:02 +00:00
DATABASE = 'notes.db'
2013-01-19 11:05:28 +00:00
def add_all_notes_in_dir(path):
2013-01-19 21:45:02 +00:00
subjects = os.listdir(path)
2013-01-19 11:05:28 +00:00
if "bootstrap" in subjects:
subjects.remove("bootstrap")
2013-01-26 19:41:39 +00:00
if "mini" in subjects:
subjects.remove("mini")
if ".directory" in subjects:
subjects.remove(".directory")
print "all subjects: " + str(subjects)
2013-01-19 11:05:28 +00:00
for subject in subjects:
2013-01-19 21:45:02 +00:00
for date in os.listdir(path+"/" + subject):
2013-01-19 11:05:28 +00:00
notes = []
2013-01-19 21:45:02 +00:00
for note in os.listdir(path+"/" + subject +"/" + date ):
2013-01-19 11:05:28 +00:00
notes.append({"tags": "none", "name": note})
2013-01-26 19:41:39 +00:00
print subject + " " + date + note
2013-01-19 11:05:28 +00:00
add_note(subject, date, notes)
def add_note(subject, datestring, notes):
connection_db = connect_db()
db = connection_db.cursor()
db.execute('insert into notes (subject, datestring ) values (?, ?)', [subject, datestring])
note_id = db.lastrowid
for note in notes:
db.execute('insert into pages (note_id, name, tags ) values (?, ?, ?)', [note_id, note["name"], note["tags"]])
connection_db.commit()
2013-01-26 19:41:39 +00:00
print "note added"
2013-01-19 11:05:28 +00:00
def connect_db():
return sqlite3.connect(DATABASE)
def init_db():
db = connect_db()
2013-01-19 21:45:02 +00:00
print "initialize database..."
2013-01-19 11:05:28 +00:00
f = open("schema.sql", "r")
db.cursor().executescript(f.read())
db.commit()
def main():
init_db()
path = raw_input("Path: ")
try:
add_all_notes_in_dir(path)
except OSError:
pass
if __name__ == "__main__":
main()