nooz/notemanager.py

59 lines
1.8 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, author):
print "[+] adding notes from directory: " + 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)
author_string = path.split("/")[-1]
print "[info] author string is " + author_string
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
add_note(subject, date, notes, author, author_string)
2013-01-19 11:05:28 +00:00
def add_note(subject, datestring, notes, author, author_string):
2013-01-19 11:05:28 +00:00
connection_db = connect_db()
db = connection_db.cursor()
db.execute('insert into notes (subject, datestring, author, author_string ) values (?, ?, ?, ?)', [subject, datestring, author, author_string])
2013-01-19 11:05:28 +00:00
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"
2013-01-19 11:05:28 +00:00
def connect_db():
return sqlite3.connect(DATABASE)
def init_db():
db = connect_db()
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: ")
author = raw_input("Author: ")
2013-01-19 11:05:28 +00:00
try:
add_all_notes_in_dir(path, author)
2013-01-19 11:05:28 +00:00
except OSError:
pass
if __name__ == "__main__":
main()