nooz/notemanager.py

59 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
import os
import sqlite3
DATABASE = 'notes.db'
def add_all_notes_in_dir(path, author):
print "[+] adding notes from directory: " + path
subjects = os.listdir(path)
if "bootstrap" in subjects:
subjects.remove("bootstrap")
if "mini" in subjects:
subjects.remove("mini")
if ".directory" in subjects:
subjects.remove(".directory")
print "all subjects: " + str(subjects)
author_string = path.split("/")[-1]
print "[info] author string is " + author_string
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})
print subject + " " + date + note
add_note(subject, date, notes, author, author_string)
def add_note(subject, datestring, notes, author, author_string):
connection_db = connect_db()
db = connection_db.cursor()
db.execute('insert into notes (subject, datestring, author, author_string ) values (?, ?, ?, ?)', [subject, datestring, author, author_string])
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()
print "[+] note added"
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: ")
author = raw_input("Author: ")
try:
add_all_notes_in_dir(path, author)
except OSError:
pass
if __name__ == "__main__":
main()