Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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, notebook, tagNames, content """
        bufStrings = []
        pref = EvervimPref.getInstance()
        doc = minidom.parseString(note.content)
        ennote = doc.getElementsByTagName("en-note")[0]

        notebooks = self.api.listNotebooks()
        notebook = next((n for n in notebooks if n.guid == note.notebookGuid), None)
        notebookName = notebook.name if notebook is not None else ""

        if pref.usemarkdown == '0':
            bufStrings.append(note.title)
            bufStrings.append("Notebook:" + notebookName)
            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("Notebook:" + notebookName)
            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, notebook, tags, content from buftext """
        pref = EvervimPref.getInstance()
        if pref.usemarkdown == '0':
            note.title = buflines[0]
            note.content  = EvernoteAPI.NOTECONTENT_HEADER + "\n".join(buflines[3:]) + EvernoteAPI.NOTECONTENT_FOOTER
        else:
            note.title = re.sub(r'^#', '',buflines[0]).strip()
            parsedContent = markdownAndENML.parseMarkdown("\n".join(buflines[3:]))
            note.content  = EvernoteAPI.NOTECONTENT_HEADER + parsedContent.encode('utf-8') + EvernoteAPI.NOTECONTENT_FOOTER

        notebookName = buflines[1].replace('Notebook:', '').strip()
        if notebookName == "" and pref.defaultnotebook is not None:
            notebookName = pref.defaultnotebook

        notebooks = self.api.listNotebooks()
        notebook = next((n for n in notebooks if n.name == notebookName), None)
        if notebook is not None:
            note.notebookGuid = notebook.guid

        note = self.api.editTag(note, buflines[2].replace('Tags:', ''))

        return note