nooz/notemanager.py

48 lines
1.2 KiB
Python

# -*- coding: utf-8 -*-
import os
import sqlite3
DATABASE = 'notes.db'
def add_all_notes_in_dir(path):
subjects = os.listdir(path)
if "bootstrap" in subjects:
subjects.remove("bootstrap")
for subject in subjects:
for date in os.listdir(path+"/" + subject):
notes = []
for note in os.listdir(path+"/" + subject +"/" + date ):
notes.append({"tags": "none", "name": note})
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()
def connect_db():
return sqlite3.connect(DATABASE)
def init_db():
db = connect_db()
print "initialize database..."
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()