Skip to content

Commit 124d0b0

Browse files
committed
Update replaced functions
1 parent a7b843c commit 124d0b0

File tree

3 files changed

+42
-58
lines changed

3 files changed

+42
-58
lines changed

LocalNote/controllers.py

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ def __get_changes(self, update = False): # -1 for need download, 1 for need uplo
6666
if self.ls.lastUpdate < eNote[1]:
6767
r.append((nbName+'/'+lNote[0], -1))
6868
else:
69+
pass
6970
# debug
70-
r.append((nbName+'/'+lNote[0], 2))
71+
# r.append((nbName+'/'+lNote[0], 2))
7172
delIndex.append(i)
7273
break
7374
else: # note exists locally not online
@@ -91,14 +92,14 @@ def _download_note(noteFullPath):
9192
return
9293
contentDict = self.ec.get_attachment(noteFullPath)
9394
if contentDict.get(noteFullPath.split('/')[1]+'.md') is None:
94-
if contentDict.get(noteFullPath.split('/')[1]+'.txt') is None:
95-
contentDict[noteFullPath.split('/')[1]+'.txt'] = self.ec.get_content(noteFullPath)
95+
if contentDict.get(noteFullPath.split('/')[1]+'.html') is None:
96+
contentDict[noteFullPath.split('/')[1]+'.html'] = self.ec.get_content(noteFullPath)
9697
else: # avoid mistaken overwrite of attachment
9798
fileNum = 1
9899
while 1:
99-
if contentDict.get(noteFullPath.split('/')[1]+'(%s).txt'%fileNum) is None:
100-
contentDict[noteFullPath.split('/')[1]+'(%s).txt'%fileNum] = contentDict[noteFullPath.split('/')[1]+'.txt']
101-
contentDict[noteFullPath.split('/')[1]+'.txt'] = self.ec.get_content(noteFullPath)
100+
if contentDict.get(noteFullPath.split('/')[1]+'(%s).html'%fileNum) is None:
101+
contentDict[noteFullPath.split('/')[1]+'(%s).html'%fileNum] = contentDict[noteFullPath.split('/')[1]+'.html']
102+
contentDict[noteFullPath.split('/')[1]+'.html'] = self.ec.get_content(noteFullPath)
102103
break
103104
else:
104105
fileNum += 1
@@ -116,7 +117,7 @@ def _download_note(noteFullPath):
116117
for note in notes: _download_note(noteFullPath+'/'+note[0])
117118
self.ls.update_config(lastUpdate = time.time())
118119
return True
119-
def upload_file(self, update = True):
120+
def upload_files(self, update = True):
120121
if not self.available: return False
121122
def encode_content(content):
122123
try:
@@ -125,52 +126,34 @@ def encode_content(content):
125126
try:
126127
content = content.decode(chardet.detect(content)).encode('utf8')
127128
except:
128-
# DEBUG
129-
print('Encode failed')
130129
content = 'Upload encode failed, I\'m sorry! Please contact [email protected] with this file.'
131130
return content
132-
def _upload_file(noteFullPath, attachmentDict):
131+
def _upload_files(noteFullPath, attachmentDict):
133132
nbName, nName = noteFullPath.split('/')
134133
if not attachmentDict:
135134
self.ec.delete_note(noteFullPath)
136135
elif nName + '.md' in attachmentDict.keys():
137136
content = encode_content(attachmentDict[nName+'.md'])
138137
self.ec.update_note(noteFullPath, markdown(content), attachmentDict)
139-
elif nName + '.txt' in attachmentDict.keys():
140-
content = encode_content(attachmentDict[nName+'.txt'])
141-
del attachmentDict[nName + '.txt']
138+
elif nName + '.html' in attachmentDict.keys():
139+
content = encode_content(attachmentDict[nName+'.html'])
140+
del attachmentDict[nName + '.html']
142141
self.ec.update_note(noteFullPath, content, attachmentDict)
143142
for noteFullPath, status in self.__get_changes(update):
144143
if status not in (1, 0): continue
145144
if '/' in noteFullPath:
146145
attachmentDict = self.ls.read_note(noteFullPath)
147-
_upload_file(noteFullPath, attachmentDict)
146+
_upload_files(noteFullPath, attachmentDict)
148147
else:
149-
if os.path.exists(noteFullPath):
148+
lns = self.ls.get_file_dict().get(noteFullPath)
149+
ens = self.es.get_note_dict().get(noteFullPath)
150+
if lns is None:
151+
for note in ens or []: self.ec.delete_note(noteFullPath+'/'+note[0])
152+
self.ec.delete_notebook(noteFullPath)
153+
else:
150154
self.ec.create_notebook(noteFullPath)
151-
for note in self.ls.get_file_dict()[noteFullPath]:
155+
for note in lns:
152156
attachmentDict = self.ls.read_note(noteFullPath+'/'+note[0])
153-
_upload_file(noteFullPath+'/'+note[0], attachmentDict)
154-
else:
155-
self.ec.delete_notebook(noteFullPath)
157+
_upload_files(noteFullPath+'/'+note[0], attachmentDict)
156158
self.__get_changes(update = True)
157159
return True
158-
159-
def __check_file(self, noteName, notebookName, note):
160-
# -1 for need download, 1 for need upload, 0 for can be uploaded and downloaded, 2 for updated
161-
if os.path.exists(self.__str_c2l(os.path.join(notebookName, noteName + '.md'))):
162-
fileDir = self.__str_c2l(os.path.join(notebookName, noteName + '.md'))
163-
elif os.path.exists(os.path.join(notebookName, noteName + '.txt')):
164-
fileDir = self.__str_c2l(os.path.join(notebookName, noteName + '.txt'))
165-
else:
166-
return -1
167-
if self.lastUpdate < note.updated / 1000:
168-
if self.lastUpdate < os.stat(fileDir).st_mtime:
169-
return 0
170-
else:
171-
return -1
172-
else:
173-
if self.lastUpdate < os.stat(fileDir).st_mtime:
174-
return 1
175-
else:
176-
return 2

LocalNote/evernoteapi/controller.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@ def create_notebook(self, title):
2424
if self.get(title): return False
2525
notebook = Types.Notebook()
2626
notebook.name = title
27-
notebook = self.noteStore.createNotebook(notebook)
28-
self.storage.create_notebook(notebook)
29-
return True
27+
try:
28+
notebook = self.noteStore.createNotebook(notebook)
29+
except EDAMUserException, e:
30+
if e.errorCode == 10 and e.parameter == 'Notebook.name':
31+
return True
32+
else:
33+
raise e
34+
else:
35+
self.storage.create_notebook(notebook)
36+
return True
3037
def create_note(self, noteFullPath, content = None, fileDict = {}):
3138
if self.get(noteFullPath): return False
3239
if '/' in noteFullPath:
@@ -39,6 +46,7 @@ def create_note(self, noteFullPath, content = None, fileDict = {}):
3946
note.title = title
4047
note.content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
4148
note.content += '<en-note>'
49+
content = re.sub('<en-media.*?/>', '', content)
4250
note.content += content or ''
4351
if self.get(notebook) is None: self.create_notebook(notebook)
4452
note.notebookGuid = self.get(notebook).guid
@@ -107,13 +115,16 @@ def get_content(self, noteFullPath):
107115
if note is None: return
108116
r = self.noteStore.getNoteContent(note.guid)
109117
try:
110-
content = re.compile('.*?<en-note>(.*?)</en-note>').findall(r)[0]
118+
content = re.compile('[\s\S]*?<en-note>([\s\S]*?)</en-note>').findall(r)[0]
111119
except:
112120
content = ''
113121
return content
114122
def get_attachment(self, noteFullPath):
115123
note = self.get(noteFullPath)
116-
return {resource.attributes.fileName: self.noteStore.getResourceData(resource.guid) for resource in (note.resources or {})}
124+
attachmentDict = {}
125+
for resource in (self.noteStore.getNote(note.guid, False, True, False, False).resources or {}):
126+
attachmentDict[resource.attributes.fileName] = resource.data.body
127+
return attachmentDict
117128
def move_note(self, noteFullPath, _to):
118129
if self.get(noteFullPath) is None: return False
119130
if type(self.get(noteFullPath)) != type(Types.Note()) or type(self.get(_to)) != type(Types.Notebook()): raise Exception('Type Error')
@@ -134,7 +145,7 @@ def delete_note(self, noteFullPath):
134145
self.storage.delete_note(noteFullPath)
135146
return True
136147
def delete_notebook(self, notebook):
137-
if self.get(notebook) or not self.isSpecialToken: return False
148+
if not self.get(notebook) or not self.isSpecialToken: return False
138149
if type(self.get(notebook)) != type(Types.Notebook()): raise Exception('Types Error')
139150
self.noteStore.expungeNotebook(self.token, self.get(notebook).guid)
140151
self.storage.delete_notebook(notebook)

LocalNote/local/storage.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ def read_note(self, noteFullPath):
5757
attachmentDict[self.__str_l2c(attachment)] = f.read()
5858
else:
5959
fileList = os.walk(join(*self.__str_c2l(noteFullPath).split('/')[:-1])).next()[2]
60-
for postfix in ('.md', '.txt'):
61-
fName = noteFullPath.split('/')[:-1] + postfix
60+
for postfix in ('.md', '.html'):
61+
fName = noteFullPath.split('/')[-1] + postfix
6262
if self.__str_c2l(fName) in fileList:
63-
with open(join(*self.__str_c2l(noteFullPath+postfix))) as f:
63+
with open(join(*self.__str_c2l(fName))) as f:
6464
attachmentDict[fName] = f.read()
6565
return attachmentDict
6666
def write_note(self, noteFullPath, contentDict = {}):
6767
if '/' in noteFullPath:
6868
nbName, nName = self.__str_c2l(noteFullPath).split('/')
6969
# clear environment
7070
if exists(nbName):
71-
for postfix in ('.md', '.txt'):
71+
for postfix in ('.md', '.html'):
7272
if exists(join(nbName, nName+postfix)): os.remove(join(nbName, nName+postfix))
7373
if exists(join(nbName, nName)):
7474
for fName in os.walk(join(nbName, nName)).next()[2]:
@@ -99,16 +99,6 @@ def write_note(self, noteFullPath, contentDict = {}):
9999
os.remove(join(noteFullPath, fName, dName))
100100
os.rmdir(join(noteFullPath, fName))
101101
os.rmdir(noteFullPath)
102-
def write_file(self, noteFullPath, content, postfix = '.md'):
103-
if len(noteFullPath.split('/')) < 1: return False
104-
if not exists(self.__str_c2l(noteFullPath.split('/')[0])):
105-
os.mkdir(self.__str_c2l(noteFullPath.split('/')[0]))
106-
try:
107-
noteFullPath += postfix
108-
with open(self.__str_c2l(join(*noteFullPath.split('/'))), 'wb') as f: f.write(content)
109-
return True
110-
except:
111-
return False
112102
def get_file_dict(self):
113103
fileDict = {}
114104
for nbName in os.walk('.').next()[1]: # get folders

0 commit comments

Comments
 (0)