-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoteorg.py
More file actions
535 lines (509 loc) · 19.4 KB
/
Copy pathnoteorg.py
File metadata and controls
535 lines (509 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## @package noteorg
#
# \brief Note Organizer the file header
#
# ### The file header follows this format
#
# > <!-- <br>
# > +Author: 刘嘉屹 <br>
# > +Date Created: 16 Jan 2014 <br>
# > +Date Changed: <br>
# > +Tags: General; Chinese; <br>
# > +标签: 通用 <br>
# > -->
#
# ### JSON data structure
#
# *object*
#
# > { <br>
# > "Author": xxx, <br>
# > "Date_create": xxx, <br>
# > "Date_change": xxx, <br>
# > "tags": [xxx,xxx,xxx], <br>
# > "ctags": [xxx,xxx,xxx], <br>
# > "content": xxx <br>
# > }
#
from os.path import isfile,abspath,basename,getmtime
import sys
import sqlite3 as sql
import glob
import re
from time import localtime, strftime,strptime
from systools import *
# set for python 2.7 version
reload(sys)
sys.setdefaultencoding("UTF-8")
##
# \brief Class for markdown file note
# \details This class build to load the file into program and support multiple output formats
# \date 2014-01-17
class Note:
##
# \brief Initialization code
# \param path path to the file (the last */* is needed)
# \param filename name string of a file to load-in
# \param fulltext if set, then create the Note from input text
def __init__(self,path,filename,fulltext=[]):
try:
## Filename of the Note
self.filename = unicode(filename)
## Path to Note
self.path = unicode(path)
#if self.path[-1]!='/':
# self.path = str.append(self.path,'/')
filename = self.path+self.filename
if len(fulltext) == 0: # create new Note from file
# !!! Exam file existency !!! #
if not isfile(filename):
raise Exception("!! Fail to open %s !!"%filename)
# !!! initial empty content !!! #
## Author of the Note
self.author = unicode("EMPTY")
## Create Date of the Note
self.dateCreate = None
## Create Date of the Note
self.dateChange = None
## system last modification time
self.modifyTime = localtime(getmtime(filename))
## Tags for the Note
self.tags = []
## Tags for the Note (Chinese)
self.ctags = []
## Main content of the Note
self.content = ''
# !!! Load file content !!! #
with open(filename,'r') as f:
fulltext = f.readlines()
# !!! empty file case !!! #
if len(fulltext) == 0:
raise Exception("!! File %s is empty"%filename)
else: # the fulltext is loaded from database
# !!! empty Database case !!! #
if len(fulltext) == 0:
raise Exception("!! Database item %s is empty"%filename)
# !!! phrasing content !!! #
self.phraseContent(fulltext)
except Exception as inst:
warning("!!Note"+inst.args[0])
if inst.args[0][:2] == "!!":
raise Exception("NoteClass","!!Fail to initialize Note class from %s"%self.filename)
else:
raise
##
# \brief phrase the whole content
def phraseContent(self,fulltext):
if fulltext[0].strip(NEWLINE) != "<!--":
raise Exception("!! Fail to recognize header from: %s"
%fulltext[0].strip(NEWLINE))
finishFlag = False
for ii,line in enumerate(fulltext[1:]):
if (line.strip(NEWLINE) == "-->"):
finishFlag = ii
break
# !!! Start pumping header !!! #
currentName = ''
if line[0] == '+': # identify header
ia = line.find(':')
ib = line.find(":")
if not (ia & ib):
raise Exception("!! Not seperator in header !!")
ia = ia if ia != -1 else 100000
ib = ib if ib != -1 else 100000
if ib > ia: # choose the smallest
currentName = line[1:ia]
self.__populateHeaderObj(currentName,line[ia+1:])
else:
currentName = line[1:ib]
self.__populateHeaderObj(currentName,line[ib+3:])
elif currentName != '': # continue previous header
self.__populateHeaderObj(currentName,line)
else: # unrecognized case
warning("unknown case:: %s"%line)
if not finishFlag:
raise Exception("!! Unfinished header !!")
# !!! insert main body !!! #
if len(fulltext) == ii+2:
raise Exception("!! Empty Content !!")
self.content = unicode("".join(fulltext[ii+2:]))
##
# \brief Populate the header content into Note class
# \param name the keyword
# \param content string contains all keywords
def __populateHeaderObj(self,name,content):
content = content.strip() # remove leading and trailing spaces
if name.lower() == "author":
self.author = unicode(content)
if name.lower() == "date created":
if content == "":
self.dateCreate = localtime()
else:
self.dateCreate = strptime(content,"%d %b %Y")
if name.lower() == "date changed":
if content == "":
self.dateChange = self.dateCreate
else:
self.dateChange = strptime(content,"%d %b %Y")
if name.lower() == "tags":
self.__populateTagArray(content)
if name.lower() == "标签":
self.__populateTagArrayS(content)
return
##
# \brief Populate the header tags array, work only for or ";"
# \param content a ;-seperated list of tags
def __populateTagArray(self,content):
self.tags = re.split(';+', content)
self.tags = [unicode(x.strip().capitalize()) for x in self.tags if x]
return
##
# \brief Populate the header ctags array, work only for or ";" and ";" 中文标签
# \param content a ;-seperated list of tags
# Populate the header tag array, work only for or ";"
def __populateTagArrayS(self,content):
self.ctags = re.split(';+|;+', content)
self.ctags = [unicode(x.strip().capitalize()) for x in self.ctags if x]
return
##
# \brief return header information in one string
def returnHeader(self):
str = "<!--\n" + \
"+Author: %s"%self.author + NEWLINE + \
"+Date Created: %s"%strftime("%d %b %Y",self.dateCreate) + NEWLINE + \
"+Date Changed: %s"%strftime("%d %b %Y",self.dateChange) + NEWLINE + \
"+Tags: %s"%";".join(self.tags) + NEWLINE + \
"+标签: %s"%";".join(self.ctags) + NEWLINE + \
"-->\n"
return str
##
# \brief write Note into file
# \param filename name of the file to write in (need to be a new file)
def writeFile(self,filename):
try:
if isfile(filename):
raise Exception("!! File (%s)Exist; Stop to overwrite !!"%filename)
with open(filename,'w') as f:
f.write(self.returnHeader()+self.content)
except Exception as inst:
warning(inst.args[0])
raise
#sys.exit(1)
return
##
# \class IndexPage
# \brief index all Note classes
#
# Create an index page contains all tags and filename(link)
class IndexPage:
pass
##
# \class NoteDB
# \brief interface of database
# \date 2014-01-20
#
# head - store file header
# content -store note content
# tag - store file tags
# ctag - store chinese tags
#
# main function:
#
# + import(): import a new path into database (no file conflict checking)
# + update(): update a path which contains files already in database
# + getFileByTag(): get file names by tag
# + getContent(): get file content by file name
#
# ### Database entry
#
# *table*
#
# + head
#
# --------------------------------------
# | Name | Type |
# |------------- |---------------------|
# | fid | INTEGER PRIMARY KEY |
# | filename | TEXT |
# | path | TEXT |
# | author | TEXT |
# | date_create | TEXT |
# | date_change | TEXT |
# | sys_modified | TEXT |
# --------------------------------------
#
# UNIQUE (filename,path)
#
# + content
#
# ----------------------------------------------------------
# | Name | Type |
# |---------|----------------------------------------------|
# | fid | INTEGER NOT NULL PRIMARY KEY REFERENCES head |
# | content | TEXT |
# ----------------------------------------------------------
#
# + tag
#
# ----------------------------------------------------------
# | Name | Type |
# |--------|-----------------------------------------------|
# | tid | INTEGER NOT NULL PRIMARY KEY |
# | tag | TEXT UNIQUE |
# ----------------------------------------------------------
#
# + ctag
#
# ----------------------------------------------------------
# | Name | Type |
# |--------|-----------------------------------------------|
# |ctid |INTEGER NOT NULL PRIMARY KEY|
# |ctag |TEXT UNIQUE|
# --------------------
#
# + tf_bridge
#
# ----------------------------------------------------------
# | Name | Type |
# |--------|-----------------------------------------------|
# | fid | INTEGER NOT NULL REFERENCES head |
# | tid | INTEGER NOT NULL REFERENCES tag |
# ----------------------------------------------------------
#
# PRIMARY KEY (fid,tid)
#
# + ctf_bridge
#
# ----------------------------------------------------------
# | Name | Type |
# |--------|-----------------------------------------------|
# | fid | INTEGER NOT NULL REFERENCES head |
# | ctid | INTEGER NOT NULL REFERENCES ctag |
# ----------------------------------------------------------
#
# PRIMARY KEY (fid,ctid)
#
class NoteDB():
##
# initialize the NoteDB class
# \param filename file name of the database
# if file exist, then read the database, otherwise ask to create one
def __init__(self,filename):
if isfile(filename):
## SQL connector
self.conn = sql.connect(filename)
self.conn.text_factory = str
## SQL cursor
self.cur = self.conn.cursor()
else:
s = raw_input(">> Create a database '%s'? [n]/y: "%filename)
if s != 'y':
sys.exit(1)
path = raw_input(">> Input the path of md files: ")
self.__createDB(filename)
self.__importDB(path)
## destructor of NoteDB class
#
def __del__(self):
if hasattr( self, "conn"):
self.conn.close()
##
# create a empty database
# \param filename file name of the database
# \todo add time stamp
def __createDB(self,filename):
self.conn = sql.connect(filename)
c = self.conn.cursor()
c.executescript(''' CREATE TABLE head
(
fid INTEGER PRIMARY KEY,
filename TEXT,
path TEXT,
author TEXT,
date_create TEXT,
date_change TEXT,
sys_modified TEXT,
UNIQUE (filename,path)
);
CREATE TABLE content
(
fid INTEGER NOT NULL PRIMARY KEY REFERENCES head,
content TEXT
);
CREATE TABLE tag
(
tid INTEGER NOT NULL PRIMARY KEY,
tag TEXT UNIQUE
);
CREATE TABLE ctag
(
ctid INTEGER NOT NULL PRIMARY KEY,
ctag TEXT UNIQUE
);
CREATE TABLE tf_bridge
(
fid INTEGER NOT NULL REFERENCES head,
tid INTEGER NOT NULL REFERENCES tag,
PRIMARY KEY (fid,tid)
);
CREATE TABLE ctf_bridge
(
fid INTEGER NOT NULL REFERENCES head,
ctid INTEGER NOT NULL REFERENCES ctag,
PRIMARY KEY (fid,ctid)
)
''')
self.conn.commit()
self.cur = c
##
# expend the current database by given path
# \param path path to md files
def __importDB(self,path):
path = abspath(path)+'/' # extend to full path
files = glob.glob(path+"*.md")
for ifile in files:
try:
ifile = basename(ifile)
print "- Insert "+ifile
inote = Note(path,ifile)
self.insertNote(inote)
except Exception as inst:
if (len(inst.args) == 2) and (inst.args[0]=="NoteClass"): # read-in fail
warning(inst.args[1])
else: # other problems
raise
self.conn.commit()
##
# insert a Note Class
# \param note a Note Class
def insertNote(self,note):
# insert header
self.cur.execute('''INSERT INTO
head (filename,path,author,date_create,date_change,sys_modified)
VALUES (?,?,?,?,?,?)''',
[note.filename, note.path, note.author,
strftime("%Y-%m-%d",note.dateCreate),
strftime("%Y-%m-%d",note.dateChange),
strftime("%Y-%m-%d %H:%M:%S",note.modifyTime)
])
self.cur.execute("SELECT last_insert_rowid()")
fid = self.cur.fetchone()[0]
# insert content
self.cur.execute("INSERT INTO content VALUES (?,?)",[fid,unicode(note.content)])
# insert tag and tf_bridge
for itag in note.tags:
self.cur.execute("INSERT INTO tag (tag) select (?) where not exists(select tid from tag where tag=?)",[itag,itag])
self.cur.execute("select tid from tag where tag =?",[itag]);
tid = self.cur.fetchone()[0]
self.cur.execute("INSERT INTO tf_bridge (fid,tid) VALUES (?,?)",[fid,tid])
# insert ctag and ctf_bridge
for itag in note.ctags:
itag = unicode(itag)
self.cur.execute("INSERT INTO ctag (ctag) select (?) where not exists(select ctid from ctag where ctag=?)",[itag,itag])
self.cur.execute("select ctid from ctag where ctag =?",[itag]);
tid = self.cur.fetchone()[0]
self.cur.execute("INSERT INTO ctf_bridge (fid,ctid) VALUES (?,?)",[fid,tid])
##
# update a Note Class
# \param note Note class
def updateNote(self,note):
# examine whether note is already in the database
self.cur.execute("SELECT fid FROM head WHERE filename == ? AND path == ?",
[note.filename,note.path])
s = self.cur.fetchone()
if s is None: # new Note
warning("- Insert "+note.filename,color=33)
self.insertNote(note)
else: # Note exist
self.cur.execute("SELECT fid from head WHERE fid == ? AND sys_modified < ?"
,[s[0],strftime("%Y-%m-%d %H:%M:%S",note.modifyTime)])
s = self.cur.fetchone()
# Note need to be updated
if s is not None:
fid = s[0]
warning("- Updating "+note.filename,color=32)
# update head
self.cur.execute('''UPDATE head SET filename = ?, path = ?,
author = ?, date_create = ?, date_change = ?, sys_modified = ?
WHERE fid = ?''',
[ note.filename, note.path, note.author,
strftime("%Y-%m-%d",note.dateCreate),
strftime("%Y-%m-%d",note.dateChange),
strftime("%Y-%m-%d %H:%M:%S",note.modifyTime),
fid
])
# update content
self.cur.execute("UPDATE content SET content = ? where fid = ?",[note.content,fid])
# update tags
pendingtags = note.tags
self.cur.execute("SELECT tag, tid FROM tf_bridge JOIN tag USING (tid) WHERE fid = ?",[fid])
tagsindb = self.cur.fetchall()
for itag in tagsindb:
if itag[0] in pendingtags: # in tag list
pendingtags.remove(itag[0])
else: # remove from database
self.cur.execute("DELETE FROM tf_bridge WHERE fid = ? AND tid = ?",[fid,itag[1]])
for itag in pendingtags: # add new tags
self.cur.execute("INSERT INTO tag (tag) SELECT (?) WHERE NOT EXISTS(SELECT tid FROM tag WHERE tag=?)",[itag,itag])
self.cur.execute("SELECT tid FROM tag WHERE tag =?",[itag]);
tid = self.cur.fetchone()[0]
self.cur.execute("INSERT INTO tf_bridge (fid,tid) VALUES (?,?)",[fid,tid])
# update ctags
pendingtags = note.ctags
self.cur.execute("SELECT ctag, ctid FROM ctf_bridge JOIN ctag USING (ctid) WHERE fid = ?",[fid])
tagsindb = self.cur.fetchall()
for itag in tagsindb:
if itag[0] in pendingtags: # in tag list
pendingtags.remove(itag[0])
else: # remove from database
self.cur.execute("DELETE FROM ctf_bridge WHERE fid = ? AND ctid = ?",[fid,itag[1]])
for itag in pendingtags: # add new tags
self.cur.execute("INSERT INTO ctag (ctag) SELECT (?) WHERE NOT EXISTS(SELECT ctid FROM ctag WHERE ctag=?)",[itag,itag])
self.cur.execute("SELECT ctid FROM ctag WHERE ctag =?",[itag]);
tid = self.cur.fetchone()[0]
self.cur.execute("INSERT INTO ctf_bridge (fid,ctid) VALUES (?,?)",[fid,tid])
##
# update the database for given directory
# \param path path to the notes files
def updateDB(self,path):
path = abspath(path)+'/' # extend to full path
files = glob.glob(path+"*.md")
for ifile in files:
try:
ifile = basename(ifile)
inote = Note(path,ifile)
self.updateNote(inote)
except Exception as inst:
if (len(inst.args) == 2) and (inst.args[0]=="NoteClass"): # read-in fail
warning(inst.args[1])
else: # other problems
raise
self.conn.commit()
##
# \brief clean up to make the database tight
#
# clean the tag/tags which are not available in bridge table anymore.
def cleanTag(self):
self.cur.execute("DELETE FROM tag WHERE tid IN ( SELECT tid from tag LEFT OUTER JOIN tf_bridge USING (tid) WHERE fid IS NULL)")
##
# fetch the content from DB
# return a list of informations for testing
def fetchNoteName(self):
self.cur.execute("SELECT filename, ctag FROM head JOIN ctf_bridge USING (fid) JOIN ctag USING (ctid)")
return self.cur.fetchall()
##
# \fn main()
# for testing
def main():
#a = Note("./","rules.md")
#s = a.forDB()
a=NoteDB("test.db")
a.updateDB("./")
a.cleanTag()
s = a.fetchNoteName()
for i in s:
print i[0],i[1]
if __name__=="__main__":
main()