class EvervimEditor(object): """ editing buffertext """ _instance = None def __init__(self): if EvervimEditor._instance is not None: raise RuntimeError("EvervimPref must be one object!!!") self.api = None @classmethod def getInstance(self): if EvervimEditor._instance is None: EvervimEditor._instance = EvervimEditor() return EvervimEditor._instance def setAPI(self): pref = EvervimPref.getInstance() if EvervimPref.getInstance().username is None: raise AttributeError("username must be set!!") if EvervimPref.getInstance().password is None: raise AttributeError("password must be set!!") self.api = EvernoteAPI(pref.username, pref.password) def note2buffer(self, note): """ return strings array for buffer from note. """ """ note has attribute title, tagNames, content """ bufStrings = [] pref = EvervimPref.getInstance() doc = minidom.parseString(note.content) ennote = doc.getElementsByTagName("en-note")[0] if pref.usemarkdown == '0': bufStrings.append(note.title) bufStrings.append("Tags:" + ",".join(note.tagNames)) contentxml = ennote.toprettyxml(indent=pref.xmlindent, encoding='utf-8') contentxml = re.sub('^' + pref.xmlindent, '', contentxml, flags=re.MULTILINE) bufStrings.extend([line for line in contentxml.splitlines()[1:-1] if line.strip()]) else: bufStrings.append('# ' + note.title) bufStrings.append("Tags:" + ",".join(note.tagNames)) content = markdownAndENML.parseENML(ennote).encode('utf-8') bufStrings.extend(content.splitlines()) return bufStrings def buffer2note(self, note, buflines): """ return note that set title, tags, content from buftext """ pref = EvervimPref.getInstance() if pref.usemarkdown == '0': note.title = buflines[0] note = self.api.editTag(note, buflines[1].replace('Tags:', '')) note.content = EvernoteAPI.NOTECONTENT_HEADER + "\n".join(buflines[2:]) + EvernoteAPI.NOTECONTENT_FOOTER else: note.title = re.sub(r'^#', '',buflines[0]).strip() note = self.api.editTag(note, buflines[1].replace('Tags:', '')) parsedContent = markdownAndENML.parseMarkdown("\n".join(buflines[2:])) note.content = EvernoteAPI.NOTECONTENT_HEADER + parsedContent.encode('utf-8') + EvernoteAPI.NOTECONTENT_FOOTER return note
class EvervimEditor(object): """ editing buffertext """ _instance = None def __init__(self): if EvervimEditor._instance is not None: raise RuntimeError("EvervimPref must be one object!!!") self.api = None @classmethod def getInstance(self): if EvervimEditor._instance is None: EvervimEditor._instance = EvervimEditor() return EvervimEditor._instance def setAPI(self): pref = EvervimPref.getInstance() if EvervimPref.getInstance().devtoken is None: raise AttributeError("devtoken must be set!!") self.api = EvernoteAPI(self.devtoken) def note2buffer(self, note): """ return strings array for buffer from note. """ """ note has attribute title, tagNames, content """ bufStrings = [] pref = EvervimPref.getInstance() doc = minidom.parseString(note.content) ennote = doc.getElementsByTagName("en-note")[0] if pref.usemarkdown == '0': bufStrings.append(note.title) bufStrings.append("Tags:" + ",".join(note.tagNames)) contentxml = ennote.toprettyxml(indent=pref.xmlindent, encoding='utf-8') contentxml = re.sub('^' + pref.xmlindent, '', contentxml, flags=re.MULTILINE) bufStrings.extend([line for line in contentxml.splitlines()[1:-1] if line.strip()]) else: bufStrings.append('# ' + note.title) bufStrings.append("Tags:" + ",".join(note.tagNames)) content = markdownAndENML.parseENML(ennote).encode('utf-8') bufStrings.extend(content.splitlines()) return bufStrings def buffer2note(self, note, buflines): """ return note that set title, tags, content from buftext """ pref = EvervimPref.getInstance() if pref.usemarkdown == '0': note.title = buflines[0] note = self.api.editTag(note, buflines[1].replace('Tags:', '')) note.content = EvernoteAPI.NOTECONTENT_HEADER + "\n".join(buflines[2:]) + EvernoteAPI.NOTECONTENT_FOOTER else: note.title = re.sub(r'^#', '',buflines[0]).strip() note = self.api.editTag(note, buflines[1].replace('Tags:', '')) parsedContent = markdownAndENML.parseMarkdown("\n".join(buflines[2:])) note.content = EvernoteAPI.NOTECONTENT_HEADER + parsedContent + EvernoteAPI.NOTECONTENT_FOOTER return note
class TestEvernoteAPI(unittest.TestCase): """ doc """ def setUp(self): # {{{ self.api = EvernoteAPI(USERNAME, PASSWORD) #}}} def testAuth(self): # {{{ """ test auth """ self.api = EvernoteAPI(USERNAME, PASSWORD) self.api.auth() self.assertIsNotNone(self.api.user) self.assertIsNotNone(self.api.refreshAuthDataTime) self.assertIsNotNone(self.api.expirationDataTime) #}}} def testRefreshAuth(self): # {{{ self.api = EvernoteAPI(USERNAME, PASSWORD) self.api.auth() token = self.api.authToken refreshAuthDataTime = self.api.refreshAuthDataTime expirationDataTime = self.api.expirationDataTime self.api.refreshAuth() self.assertNotEqual(token , self.api.authToken) self.assertNotEqual(refreshAuthDataTime , self.api.refreshAuthDataTime) self.assertNotEqual(expirationDataTime , self.api.expirationDataTime) #}}} def testAuthFairueByUsername(self): # {{{ api = EvernoteAPI('wrong_user_name_xxxxxxxx', PASSWORD) self.assertRaises(StandardError, lambda: {api.auth()}) #}}} def testAuthFairueByPassword(self): # {{{ api = EvernoteAPI(USERNAME, 'wrong_user_name_xxxxxxxx') self.assertRaises(StandardError, lambda: {api.auth()}) #}}} def testListNoteBooks(self): # {{{ notebooks = self.api.listNotebooks() self.assertIsInstance(notebooks, list) self.assertNotEquals(0, len(notebooks)) for notebook in notebooks: self.assertTrue(hasattr(notebook, 'guid')) self.assertTrue(hasattr(notebook, 'name')) #}}} def testListTags(self): # {{{ tags = self.api.listTags() self.assertIsInstance(tags , list) self.assertNotEquals(0, len(tags)) for tag in tags: self.assertTrue(hasattr(tag, 'guid')) self.assertTrue(hasattr(tag, 'name')) #}}} def testNotesByTag(self): # {{{ tags = self.api.listTags() notes = [] for tag in tags: [notes.append(note) for note in self.api.notesByTag(tag).elem] # less more 1 notes self.assertNotEquals(0, len(notes)) for note in notes: self.assertTrue(hasattr(note, 'guid')) #}}} def testNotesByNotebook(self): # {{{ notebooks = self.api.listNotebooks() notes = [] for notebook in notebooks: [notes.append(note) for note in self.api.notesByNotebook(notebook).elem] # less more 1 notes self.assertNotEquals(0, len(notes)) for note in notes: self.assertTrue(hasattr(note, 'guid')) #}}} def testNotesByQuery(self): # {{{ notes = self.api.notesByQuery('日本語').elem # less more 1 notes self.assertNotEquals(0, len(notes)) for note in notes: self.assertTrue(hasattr(note, 'guid')) #}}} def testGetNote(self): # {{{ note = self.__getOneNote() self.assertIsNone(note.content) notewithContent = self.api.getNote(note) self.assertIsNotNone(notewithContent.content) notewithContent.tagNames.append('google') self.api.updateNote(notewithContent) #}}} def testCreateNote(self): # {{{ note = Types.Note() editText = NOTECONTENT_HEADER + """this is content 日本語""" + NOTECONTENT_FOOTER note.title = "createNote" note.content = editText note = self.api.editTag(note, "tag1, newTag") createdNote = self.api.createNote(note) self.assertIsNotNone(createdNote.guid, None) getnote = Types.Note() getnote.guid = createdNote.guid self.assertIsNotNone(self.api.getNote(getnote)) #}}} def testWhenUpdateNoteTagDelete(self): # {{{ note = Types.Note() editText = NOTECONTENT_HEADER + """this is content 日本語""" + NOTECONTENT_FOOTER note.title = "createNote" note.content = editText note = self.api.editTag(note, "tag1, newTag") createdNote = self.api.createNote(note) editTextNoTag = NOTECONTENT_HEADER + """this is content updated""" + NOTECONTENT_FOOTER createdNote.title = 'createNoteUpdated' createdNote.content = editTextNoTag createdNote = self.api.editTag(createdNote, "") self.api.updateNote(createdNote) getnote = Types.Note() getnote.guid = createdNote.guid getnote = self.api.getNote(getnote) self.assertIsNotNone(getnote) self.assertIsNone(getnote.tagGuids) #}}} def __getOneNote(self): # {{{ notes = self.api.notesByQuery('日本語').elem self.assertNotEquals(0, len(notes)) for note in notes: return note #}}} def testEvernoteList(self): # {{{ evernoteList = EvernoteList() self.assertTrue(hasattr(evernoteList, 'elem')) self.assertTrue(hasattr(evernoteList, 'maxcount')) self.assertTrue(hasattr(evernoteList, 'maxpages')) self.assertTrue(hasattr(evernoteList, 'currentpage')) #}}} def testNoteList2EvernoteList(self): # {{{ class dummy(object): pass noteList = dummy() setattr(noteList , 'notes', []) setattr(noteList , 'totalNotes', 1) setattr(noteList , 'startIndex', 0) # test of private method! evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 0) self.assertEqual(evernoteList.currentpage, 0) setattr(noteList , 'totalNotes', 50) # 0 - 50 -> 0 ( 0 is none ) evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 0) setattr(noteList , 'totalNotes', 51) # 51 - 100 -> 1 evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 1) setattr(noteList , 'totalNotes', 100) # 101 - 150 -> 2 evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 1) setattr(noteList , 'totalNotes', 151) evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 3) setattr(noteList , 'startIndex', 49) # 0 - 49 -> 0index is start from 0 evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.currentpage, 0) setattr(noteList , 'startIndex', 50) # 50 - 99 -> 1 evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.currentpage, 1) setattr(noteList , 'startIndex', 100) evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.currentpage, 2) setattr(noteList , 'startIndex', 101) evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.currentpage, 2)
class TestEvernoteAPI(unittest.TestCase): """ doc """ def setUp(self): # {{{ self.api = EvernoteAPI(DEVELOPER_TOKEN) #}}} def testAuthFairueWrongToken(self): # {{{ self.api = EvernoteAPI("dummy tokens") self.assertRaises(Errors.EDAMUserException, lambda: {self.api.auth()}) #}}} def testListNoteBooks(self): # {{{ notebooks = self.api.listNotebooks() self.assertIsInstance(notebooks, list) self.assertNotEquals(0, len(notebooks)) for notebook in notebooks: self.assertTrue(hasattr(notebook, 'guid')) self.assertTrue(hasattr(notebook, 'name')) #}}} def testListTags(self): # {{{ tags = self.api.listTags() self.assertIsInstance(tags, list) self.assertNotEquals(0, len(tags)) for tag in tags: self.assertTrue(hasattr(tag, 'guid')) self.assertTrue(hasattr(tag, 'name')) #}}} def testNotesByTag(self): # {{{ tags = self.api.listTags() notes = [] for tag in tags: [notes.append(note) for note in self.api.notesByTag(tag).elem] # less more 1 notes self.assertNotEquals(0, len(notes)) for note in notes: self.assertTrue(hasattr(note, 'guid')) #}}} def testNotesByNotebook(self): # {{{ notebooks = self.api.listNotebooks() notes = [] for notebook in notebooks: [ notes.append(note) for note in self.api.notesByNotebook(notebook).elem ] # less more 1 notes self.assertNotEquals(0, len(notes)) for note in notes: self.assertTrue(hasattr(note, 'guid')) #}}} def testNotesByQuery(self): # {{{ notes = self.api.notesByQuery('日本語').elem # less more 1 notes self.assertNotEquals(0, len(notes)) for note in notes: self.assertTrue(hasattr(note, 'guid')) #}}} def testGetNote(self): # {{{ note = self.__getOneNote() self.assertIsNone(note.content) notewithContent = self.api.getNote(note) self.assertIsNotNone(notewithContent.content) notewithContent.tagNames.append('google') self.api.updateNote(notewithContent) #}}} def testCreateNote(self): # {{{ note = Types.Note() editText = NOTECONTENT_HEADER + """this is content 日本語""" + NOTECONTENT_FOOTER note.title = "createNote" note.content = editText note = self.api.editTag(note, "tag1, newTag") createdNote = self.api.createNote(note) self.assertIsNotNone(createdNote.guid, None) getnote = Types.Note() getnote.guid = createdNote.guid self.assertIsNotNone(self.api.getNote(getnote)) #}}} def testWhenUpdateNoteTagDelete(self): # {{{ note = Types.Note() editText = NOTECONTENT_HEADER + """this is content 日本語""" + NOTECONTENT_FOOTER note.title = "createNote" note.content = editText note = self.api.editTag(note, "tag1, newTag") createdNote = self.api.createNote(note) editTextNoTag = NOTECONTENT_HEADER + """this is content updated""" + NOTECONTENT_FOOTER createdNote.title = 'createNoteUpdated' createdNote.content = editTextNoTag createdNote = self.api.editTag(createdNote, "") self.api.updateNote(createdNote) getnote = Types.Note() getnote.guid = createdNote.guid getnote = self.api.getNote(getnote) self.assertIsNotNone(getnote) self.assertIsNone(getnote.tagGuids) #}}} def __getOneNote(self): # {{{ notes = self.api.notesByQuery('日本語').elem self.assertNotEquals(0, len(notes)) for note in notes: return note #}}} def testEvernoteList(self): # {{{ evernoteList = EvernoteList() self.assertTrue(hasattr(evernoteList, 'elem')) self.assertTrue(hasattr(evernoteList, 'maxcount')) self.assertTrue(hasattr(evernoteList, 'maxpages')) self.assertTrue(hasattr(evernoteList, 'currentpage')) #}}} def testNoteList2EvernoteList(self): # {{{ class dummy(object): pass noteList = dummy() setattr(noteList, 'notes', []) setattr(noteList, 'totalNotes', 1) setattr(noteList, 'startIndex', 0) # test of private method! evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 0) self.assertEqual(evernoteList.currentpage, 0) setattr(noteList, 'totalNotes', 50) # 0 - 50 -> 0 ( 0 is none ) evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 0) setattr(noteList, 'totalNotes', 51) # 51 - 100 -> 1 evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 1) setattr(noteList, 'totalNotes', 100) # 101 - 150 -> 2 evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 1) setattr(noteList, 'totalNotes', 151) evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.maxpages, 3) setattr(noteList, 'startIndex', 49) # 0 - 49 -> 0index is start from 0 evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.currentpage, 0) setattr(noteList, 'startIndex', 50) # 50 - 99 -> 1 evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.currentpage, 1) setattr(noteList, 'startIndex', 100) evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.currentpage, 2) setattr(noteList, 'startIndex', 101) evernoteList = self.api._EvernoteAPI__NoteList2EvernoteList(noteList) self.assertEqual(evernoteList.currentpage, 2)
class EvervimEditor(object): """ editing buffertext """ _instance = None def __init__(self): if EvervimEditor._instance is not None: raise RuntimeError("EvervimPref must be one object!!!") self.api = None @classmethod def getInstance(self): if EvervimEditor._instance is None: EvervimEditor._instance = EvervimEditor() return EvervimEditor._instance def setAPI(self): pref = EvervimPref.getInstance() if EvervimPref.getInstance().devtoken is None: raise AttributeError("devtoken must be set!!") self.api = EvernoteAPI(pref.devtoken) def note2buffer(self, note): """ return strings array for buffer from note. """ """ note has attribute title, tagNames, content """ bufStrings = [] pref = EvervimPref.getInstance() doc = minidom.parseString(note.content) ennote = doc.getElementsByTagName("en-note")[0] if pref.usemarkdown == '0': bufStrings.append(note.title) bufStrings.append("Tags:" + ",".join(note.tagNames)) contentxml = ennote.toprettyxml(indent=pref.xmlindent, encoding='utf-8') contentxml = re.sub('^' + pref.xmlindent, '', contentxml, flags=re.MULTILINE) bufStrings.extend([line for line in contentxml.splitlines()[1:-1] if line.strip()]) else: bufStrings.append('# ' + note.title) bufStrings.append("Tags:" + ",".join(note.tagNames)) # content = markdownAndENML.parseENML(ennote).encode('utf-8') try: divs = doc.getElementsByTagName("div") original_content = base64.b64decode(divs[-1].firstChild.nodeValue).split("\n") bufStrings.extend(original_content) except: self.__appendRestoreErrorMessage(bufStrings) contentxml = ennote.toprettyxml(indent=pref.xmlindent, encoding='utf-8') contentxml = re.sub('^' + pref.xmlindent, '', contentxml, flags=re.MULTILINE) bufStrings.extend([line for line in contentxml.splitlines()[1:-1] if line.strip()]) return bufStrings def __appendRestoreErrorMessage(self, bufStrings): bufStrings.append('*****************************************************') bufStrings.append('* THIS NOTE IS BROKEN OR IS NOT CREATED BY evervim. *') bufStrings.append('* YOU MUST NOT EDIT THIS NOTE !! *') bufStrings.append('*****************************************************') def buffer2note(self, note, buflines): """ return note that set title, tags, content from buftext """ pref = EvervimPref.getInstance() if pref.usemarkdown == '0': note.title = buflines[0] note = self.api.editTag(note, buflines[1].replace('Tags:', '')) note.content = EvernoteAPI.NOTECONTENT_HEADER + "\n".join(buflines[2:]) + EvernoteAPI.NOTECONTENT_FOOTER else: note.title = re.sub(r'^#', '',buflines[0]).strip() note = self.api.editTag(note, buflines[1].replace('Tags:', '')) parsedContent = markdownAndENML.parseMarkdown("\n".join(buflines[2:])) contents = buflines[2:] encoded_contents = base64.b64encode("\n".join(contents)) parsedContent = markdownAndENML.parseMarkdown("\n".join(contents)) parsedContent += ('\n<div style="display: none;">' + encoded_contents + '</div>') with open(os.path.dirname(__file__) + '/../../css/default.css', "r") as f: css = f.read() parsedContent = emailipy.inline_css(parsedContent, css) """ html tag and class attribute is not permitted in ENML """ parsedContent = re.sub('</?html[^>]*>', '', parsedContent) parsedContent = re.sub('class="[^"]*"', '', parsedContent) note.content = EvernoteAPI.NOTECONTENT_HEADER + parsedContent.encode('utf-8') + EvernoteAPI.NOTECONTENT_FOOTER return note