Skip to content

Commit a7b843c

Browse files
committed
Finished update_file
1 parent e85ef25 commit a7b843c

File tree

3 files changed

+101
-87
lines changed

3 files changed

+101
-87
lines changed

LocalNote/controllers.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os, time
22

3+
import chardet
4+
from markdown2 import markdown
5+
36
from local.storage import Storage as LocalStorage
47
from evernoteapi.storage import Storage as EvernoteStorage
58
from evernoteapi.controller import EvernoteController
@@ -115,28 +118,41 @@ def _download_note(noteFullPath):
115118
return True
116119
def upload_file(self, update = True):
117120
if not self.available: return False
118-
noteDict = self.es.get_note_dict()
119-
for fileFullPath, status in self.__get_changes(update):
121+
def encode_content(content):
122+
try:
123+
content.decode('utf8')
124+
except:
125+
try:
126+
content = content.decode(chardet.detect(content)).encode('utf8')
127+
except:
128+
# DEBUG
129+
print('Encode failed')
130+
content = 'Upload encode failed, I\'m sorry! Please contact [email protected] with this file.'
131+
return content
132+
def _upload_file(noteFullPath, attachmentDict):
133+
nbName, nName = noteFullPath.split('/')
134+
if not attachmentDict:
135+
self.ec.delete_note(noteFullPath)
136+
elif nName + '.md' in attachmentDict.keys():
137+
content = encode_content(attachmentDict[nName+'.md'])
138+
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']
142+
self.ec.update_note(noteFullPath, content, attachmentDict)
143+
for noteFullPath, status in self.__get_changes(update):
120144
if status not in (1, 0): continue
121-
if '/' in fileFullPath:
122-
nbName, nName = fileFullPath.split('/')
123-
for postfix in ('.md', '.txt', ''):
124-
content = get_file(fileFullPath + '.md')
125-
if content is None: continue
126-
if postfix == '.md':
127-
self.ec.update_note(fileFullPath, content, os.path.join(nbName, nName+'.md'))
128-
else:
129-
self.ec.update_note(fileFullPath, content)
130-
else:
131-
self.ec.delete_note(fileFullPath)
145+
if '/' in noteFullPath:
146+
attachmentDict = self.ls.read_note(noteFullPath)
147+
_upload_file(noteFullPath, attachmentDict)
132148
else:
133-
if os.path.exists(fileFullPath):
134-
self.ec.create_notebook(fileFullPath)
135-
for note in self.ls.get_file_dict()[fileFullPath]:
136-
content, isMd = self.ls.get_file(fileFullPath+'/'+note[0])
137-
self.ec.update_note(fileFullPath+'/'+note[0], content, fileFullPath+'/'+note[0] if isMd else None)
149+
if os.path.exists(noteFullPath):
150+
self.ec.create_notebook(noteFullPath)
151+
for note in self.ls.get_file_dict()[noteFullPath]:
152+
attachmentDict = self.ls.read_note(noteFullPath+'/'+note[0])
153+
_upload_file(noteFullPath+'/'+note[0], attachmentDict)
138154
else:
139-
self.ec.delete_notebook(fileFullPath)
155+
self.ec.delete_notebook(noteFullPath)
140156
self.__get_changes(update = True)
141157
return True
142158

LocalNote/evernoteapi/controller.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#coding=utf8
2-
import sys, hashlib, re, time
2+
import sys, hashlib, re, time, mimetypes
3+
34
import evernote.edam.type.ttypes as Types
45
import evernote.edam.notestore.NoteStore as NoteStore
56
from evernote.api.client import EvernoteClient
@@ -26,7 +27,7 @@ def create_notebook(self, title):
2627
notebook = self.noteStore.createNotebook(notebook)
2728
self.storage.create_notebook(notebook)
2829
return True
29-
def create_note(self, noteFullPath, content = None, fileDir = None):
30+
def create_note(self, noteFullPath, content = None, fileDict = {}):
3031
if self.get(noteFullPath): return False
3132
if '/' in noteFullPath:
3233
notebook = noteFullPath.split('/')[0]
@@ -41,28 +42,29 @@ def create_note(self, noteFullPath, content = None, fileDir = None):
4142
note.content += content or ''
4243
if self.get(notebook) is None: self.create_notebook(notebook)
4344
note.notebookGuid = self.get(notebook).guid
44-
if not fileDir is None:
45-
with open(fileDir, 'rb') as f: fileBytes = f.read()
46-
fileData = Types.Data()
47-
fileData.bodyHash = self._md5(fileBytes)
48-
fileData.size = len(fileBytes)
49-
fileData.body = fileBytes
50-
fileAttr = Types.ResourceAttributes()
51-
fileAttr.fileName = title + '.md'
52-
fileAttr.attachment = True
53-
fileResource = Types.Resource()
54-
fileResource.data = fileData
55-
fileResource.mime = 'application/octet-stream'
56-
fileResource.attributes = fileAttr
57-
note.resources = [fileResource]
58-
note.content += '<en-media type="application/octet-stream" hash="%s"/>'%fileData.bodyHash
45+
if fileDict:
46+
note.resources = []
47+
for fileName, fileBytes in fileDict.iteritems():
48+
fileData = Types.Data()
49+
fileData.bodyHash = self._md5(fileBytes)
50+
fileData.size = len(fileBytes)
51+
fileData.body = fileBytes
52+
fileAttr = Types.ResourceAttributes()
53+
fileAttr.fileName = fileName
54+
fileAttr.attachment = True
55+
fileResource = Types.Resource()
56+
fileResource.data = fileData
57+
fileResource.mime = mimetypes.guess_type(fileName)[0] or 'application/octet-stream'
58+
fileResource.attributes = fileAttr
59+
note.resources.append(fileResource)
60+
note.content += '<en-media type="%s" hash="%s"/>'%(fileResource.mime, fileData.bodyHash)
5961
note.content += '</en-note>'
6062
note = self.noteStore.createNote(note)
6163
self.storage.create_note(note, notebook)
6264
return True
63-
def update_note(self, noteFullPath, content = None, fileDir = None):
65+
def update_note(self, noteFullPath, content = None, fileDict = {}):
6466
note = self.get(noteFullPath)
65-
if note is None: return self.create_note(noteFullPath, content or '', fileDir)
67+
if note is None: return self.create_note(noteFullPath, content or '', fileDict)
6668
if '/' in noteFullPath:
6769
notebook = noteFullPath.split('/')[0]
6870
title = noteFullPath.split('/')[1]
@@ -79,21 +81,22 @@ def update_note(self, noteFullPath, content = None, fileDir = None):
7981
note.content = header
8082
note.content += '<en-note>'
8183
note.content += content or oldContent
82-
if not fileDir is None:
83-
with open(fileDir, 'rb') as f: fileBytes = f.read()
84-
fileData = Types.Data()
85-
fileData.bodyHash = self._md5(fileBytes)
86-
fileData.size = len(fileBytes)
87-
fileData.body = fileBytes
88-
fileAttr = Types.ResourceAttributes()
89-
fileAttr.fileName = title + '.md'
90-
fileAttr.attachment = True
91-
fileResource = Types.Resource()
92-
fileResource.data = fileData
93-
fileResource.mime = 'application/octet-stream'
94-
fileResource.attributes = fileAttr
95-
note.resources = [fileResource]
96-
note.content += '<en-media type="application/octet-stream" hash="%s"/>'%fileData.bodyHash
84+
if fileDict:
85+
note.resources = []
86+
for fileName, fileBytes in fileDict.iteritems():
87+
fileData = Types.Data()
88+
fileData.bodyHash = self._md5(fileBytes)
89+
fileData.size = len(fileBytes)
90+
fileData.body = fileBytes
91+
fileAttr = Types.ResourceAttributes()
92+
fileAttr.fileName = fileName
93+
fileAttr.attachment = True
94+
fileResource = Types.Resource()
95+
fileResource.data = fileData
96+
fileResource.mime = mimetypes.guess_type(fileName)[0] or 'application/octet-stream'
97+
fileResource.attributes = fileAttr
98+
note.resources.append(fileResource)
99+
note.content += '<en-media type="%s" hash="%s"/>'%(fileResource.mime, fileData.bodyHash)
97100
note.content += '</en-note>'
98101
self.noteStore.updateNote(self.token, note)
99102
self.storage.delete_note(noteFullPath)

LocalNote/local/storage.py

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json, os, time, sys
2-
from markdown2 import markdown
2+
from os.path import join, exists
33
import chardet
44

55
from evernoteapi import EvernoteController
@@ -20,7 +20,7 @@ def __init__(self):
2020
self.token, self.isSpecialToken, self.sandbox, self.isInternational, self.expireTime, self.lastUpdate = self.__load_config()
2121
self.encoding = sys.stdin.encoding
2222
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
2424
with open(CONFIG_DIR) as f: r = json.loads(f.read())
2525
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)
2626
def __store_config(self):
@@ -50,35 +50,30 @@ def __str_l2c(self, s):
5050
except:
5151
return s.decode(chardet.detect(s)['encoding'] or 'utf8').encode('utf8')
5252
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()
6058
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
7166
def write_note(self, noteFullPath, contentDict = {}):
7267
if '/' in noteFullPath:
7368
nbName, nName = self.__str_c2l(noteFullPath).split('/')
7469
# clear environment
75-
if os.path.exists(nbName):
70+
if exists(nbName):
7671
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))
8277
else:
8378
os.mkdir(nbName)
8479
# download files
@@ -88,29 +83,29 @@ def write_note(self, noteFullPath, contentDict = {}):
8883
for k, v in contentDict.items():
8984
self.write_file(noteFullPath, v, os.path.splitext(k)[1])
9085
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))
9287
for k, v in contentDict.iteritems():
9388
self.write_file(noteFullPath+'/'+k, v, '') # ok, this looks strange
9489
else:
9590
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))
9792
else: # delete folder
9893
noteFullPath = self.__str_c2l(noteFullPath)
99-
if os.path.exists(noteFullPath):
94+
if exists(noteFullPath):
10095
for fName in os.walk(noteFullPath).next()[2]:
101-
os.remove(os.path.join(noteFullPath, fName))
96+
os.remove(join(noteFullPath, fName))
10297
for fName in os.walk(noteFullPath).next()[1]:
10398
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))
106101
os.rmdir(noteFullPath)
107102
def write_file(self, noteFullPath, content, postfix = '.md'):
108103
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])):
110105
os.mkdir(self.__str_c2l(noteFullPath.split('/')[0]))
111106
try:
112107
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)
114109
return True
115110
except:
116111
return False
@@ -120,7 +115,7 @@ def get_file_dict(self):
120115
nbNameUtf8 = self.__str_l2c(nbName)
121116
fileDict[nbNameUtf8] = []
122117
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)
124119
if os.path.isdir(nName):
125120
fileDict[nbNameUtf8].append((self.__str_l2c(nName), os.stat(filePath).st_mtime))
126121
else:

0 commit comments

Comments
 (0)