1
1
import json , os , time , sys
2
- from markdown2 import markdown
2
+ from os . path import join , exists
3
3
import chardet
4
4
5
5
from evernoteapi import EvernoteController
@@ -20,7 +20,7 @@ def __init__(self):
20
20
self .token , self .isSpecialToken , self .sandbox , self .isInternational , self .expireTime , self .lastUpdate = self .__load_config ()
21
21
self .encoding = sys .stdin .encoding
22
22
def __load_config (self ):
23
- if not os . path . exists (CONFIG_DIR ): return '' , False , True , False , 0 , 0
23
+ if not exists (CONFIG_DIR ): return '' , False , True , False , 0 , 0
24
24
with open (CONFIG_DIR ) as f : r = json .loads (f .read ())
25
25
return r .get ('token' , '' ), r .get ('is-special-token' , False ), r .get ('sandbox' , True ), r .get ('is-international' , False ), r .get ('expire-time' , 0 ), r .get ('last-update' , 0 )
26
26
def __store_config (self ):
@@ -50,35 +50,30 @@ def __str_l2c(self, s):
50
50
except :
51
51
return s .decode (chardet .detect (s )['encoding' ] or 'utf8' ).encode ('utf8' )
52
52
def read_note (self , noteFullPath ):
53
- if os .path .exists (os .path .join (* [self .__str_c2l (p ) for p in noteFullPath .split ('/' )])):
54
- noteFullPath += noteFullPath .split ('/' )[- 1 :]
55
- for postfix in ('.md' , '.txt' ):
56
- if os .path .exists (os .path .join (* [self .__str_c2l (p ) for p in (noteFullPath + postfix ).split ('/' )])):
57
- noteFullPath += postfix
58
- isMd = postfix == '.md'
59
- break
53
+ attachmentDict = {}
54
+ if exists (join (* self .__str_c2l (noteFullPath ).split ('/' ))):
55
+ for attachment in os .walk (join (* self .__str_c2l (noteFullPath ).split ('/' ))).next ()[2 ]:
56
+ with open (join (* (self .__str_c2l (noteFullPath )+ '/' + attachment ).split ('/' )), 'rb' ) as f :
57
+ attachmentDict [self .__str_l2c (attachment )] = f .read ()
60
58
else :
61
- return None , False
62
- with open (os .path .join (* [self .__str_c2l (p ) for p in noteFullPath .split ('/' )])) as f : r = f .read ()
63
- try :
64
- r .decode ('utf8' )
65
- return r , isMd
66
- except :
67
- try :
68
- return r .decode (chardet .detect (r )).encode ('utf8' ), isMd
69
- except :
70
- return None , False
59
+ fileList = os .walk (join (* self .__str_c2l (noteFullPath ).split ('/' )[:- 1 ])).next ()[2 ]
60
+ for postfix in ('.md' , '.txt' ):
61
+ fName = noteFullPath .split ('/' )[:- 1 ] + postfix
62
+ if self .__str_c2l (fName ) in fileList :
63
+ with open (join (* self .__str_c2l (noteFullPath + postfix ))) as f :
64
+ attachmentDict [fName ] = f .read ()
65
+ return attachmentDict
71
66
def write_note (self , noteFullPath , contentDict = {}):
72
67
if '/' in noteFullPath :
73
68
nbName , nName = self .__str_c2l (noteFullPath ).split ('/' )
74
69
# clear environment
75
- if os . path . exists (nbName ):
70
+ if exists (nbName ):
76
71
for postfix in ('.md' , '.txt' ):
77
- if os . path . exists (os . path . join (nbName , nName + postfix )): os .remove (os . path . join (nbName , nName + postfix ))
78
- if os . path . exists (os . path . join (nbName , nName )):
79
- for fName in os .walk (os . path . join (nbName , nName )).next ()[2 ]:
80
- os .remove (os . path . join (nbName , nName , fName ))
81
- os .rmdir (os . path . join (nbName , nName ))
72
+ if exists (join (nbName , nName + postfix )): os .remove (join (nbName , nName + postfix ))
73
+ if exists (join (nbName , nName )):
74
+ for fName in os .walk (join (nbName , nName )).next ()[2 ]:
75
+ os .remove (join (nbName , nName , fName ))
76
+ os .rmdir (join (nbName , nName ))
82
77
else :
83
78
os .mkdir (nbName )
84
79
# download files
@@ -88,29 +83,29 @@ def write_note(self, noteFullPath, contentDict = {}):
88
83
for k , v in contentDict .items ():
89
84
self .write_file (noteFullPath , v , os .path .splitext (k )[1 ])
90
85
else :
91
- if not os . path . exists (os . path . join (nbName , nName )): os .mkdir (os . path . join (nbName , nName ))
86
+ if not exists (join (nbName , nName )): os .mkdir (join (nbName , nName ))
92
87
for k , v in contentDict .iteritems ():
93
88
self .write_file (noteFullPath + '/' + k , v , '' ) # ok, this looks strange
94
89
else :
95
90
if contentDict : # create folder
96
- if not os . path . exists (self .__str_c2l (noteFullPath )): os .mkdir (self .__str_c2l (noteFullPath ))
91
+ if not exists (self .__str_c2l (noteFullPath )): os .mkdir (self .__str_c2l (noteFullPath ))
97
92
else : # delete folder
98
93
noteFullPath = self .__str_c2l (noteFullPath )
99
- if os . path . exists (noteFullPath ):
94
+ if exists (noteFullPath ):
100
95
for fName in os .walk (noteFullPath ).next ()[2 ]:
101
- os .remove (os . path . join (noteFullPath , fName ))
96
+ os .remove (join (noteFullPath , fName ))
102
97
for fName in os .walk (noteFullPath ).next ()[1 ]:
103
98
for dName in os .walk (noteFullPath , fName ).next ()[2 ]:
104
- os .remove (os . path . join (noteFullPath , fName , dName ))
105
- os .rmdir (os . path . join (noteFullPath , fName ))
99
+ os .remove (join (noteFullPath , fName , dName ))
100
+ os .rmdir (join (noteFullPath , fName ))
106
101
os .rmdir (noteFullPath )
107
102
def write_file (self , noteFullPath , content , postfix = '.md' ):
108
103
if len (noteFullPath .split ('/' )) < 1 : return False
109
- if not os . path . exists (self .__str_c2l (noteFullPath .split ('/' )[0 ])):
104
+ if not exists (self .__str_c2l (noteFullPath .split ('/' )[0 ])):
110
105
os .mkdir (self .__str_c2l (noteFullPath .split ('/' )[0 ]))
111
106
try :
112
107
noteFullPath += postfix
113
- with open (self .__str_c2l (os . path . join (* noteFullPath .split ('/' ))), 'wb' ) as f : f .write (content )
108
+ with open (self .__str_c2l (join (* noteFullPath .split ('/' ))), 'wb' ) as f : f .write (content )
114
109
return True
115
110
except :
116
111
return False
@@ -120,7 +115,7 @@ def get_file_dict(self):
120
115
nbNameUtf8 = self .__str_l2c (nbName )
121
116
fileDict [nbNameUtf8 ] = []
122
117
for nName in reduce (lambda x ,y : x + y , os .walk (nbName ).next ()[1 :]): # get folders and files
123
- filePath = os . path . join (nbName , nName )
118
+ filePath = join (nbName , nName )
124
119
if os .path .isdir (nName ):
125
120
fileDict [nbNameUtf8 ].append ((self .__str_l2c (nName ), os .stat (filePath ).st_mtime ))
126
121
else :
0 commit comments