예제 #1
0
class Controller:
    evernoteImporter = None
    """:type : EvernoteImporter"""

    def __init__(self):
        self.forceAutoPage = False
        self.auto_page_callback = None
        self.anki = Anki()
        self.anki.deck = SETTINGS.ANKI.DECKS.BASE.fetch()
        self.anki.setup_ancillary_files()
        ankDB().Init()
        self.anki.add_evernote_models()
        self.evernote = Evernote()

    def test_anki(self, title, evernote_guid, filename=""):
        if not filename:
            filename = title
        fields = {
            FIELDS.TITLE:                          title,
            FIELDS.CONTENT:                        file(
                os.path.join(FOLDERS.LOGS, filename.replace('.enex', '') + ".enex"),
                'r').read(), FIELDS.EVERNOTE_GUID: FIELDS.EVERNOTE_GUID_PREFIX + evernote_guid
        }
        tags = ['NoTags', 'NoTagsToRemove']
        return AnkiNotePrototype(self.anki, fields, tags)

    def process_unadded_see_also_notes(self):
        update_regex()
        anki_note_ids = self.anki.get_anknotes_note_ids_with_unadded_see_also()
        self.evernote.getNoteCount = 0
        self.anki.process_see_also_content(anki_note_ids)

    def upload_validated_notes(self, automated=False):
        db = ankDB(TABLES.NOTE_VALIDATION_QUEUE)
        dbRows = db.all("validation_status = 1")
        notes_created, notes_updated, queries1, queries2 = ([] for i in range(4))
        """
        :type: (list[EvernoteNote], list[EvernoteNote], list[str], list[str])
        """
        noteFetcher = EvernoteNoteFetcher()
        tmr = stopwatch.Timer(len(dbRows), 25, infoStr="Upload of Validated Evernote Notes", automated=automated,
                              enabled=EVERNOTE.UPLOAD.ENABLED, max_allowed=EVERNOTE.UPLOAD.MAX,
                              label='Validation\\upload_validated_notes\\', display_initial_info=True)
        if tmr.actionInitializationFailed:
            return tmr.status, 0, 0
        for dbRow in dbRows:
            entry = EvernoteValidationEntry(dbRow)
            evernote_guid, rootTitle, contents, tagNames, notebookGuid, noteType = entry.items()
            tagNames = tagNames.split(',')
            if not tmr.checkLimits():
                break
            whole_note = tmr.autoStep(
                self.evernote.makeNote(rootTitle, contents, tagNames, notebookGuid, guid=evernote_guid,
                                       noteType=noteType, validated=True), rootTitle, evernote_guid)
            if tmr.report_result is False:
                raise ValueError
            if tmr.status.IsDelayableError:
                break
            if not tmr.status.IsSuccess:
                continue
            if not whole_note.tagNames:
                whole_note.tagNames = tagNames
            noteFetcher.addNoteFromServerToDB(whole_note, tagNames)
            note = EvernoteNotePrototype(whole_note=whole_note)
            assert whole_note.tagNames
            assert note.Tags
            if evernote_guid:
                notes_updated.append(note)
                queries1.append([evernote_guid])
            else:
                notes_created.append(note)
                queries2.append([rootTitle, contents])
        else:
            tmr.reportNoBreak()
        tmr.Report(self.anki.add_evernote_notes(notes_created) if tmr.counts.created else 0,
                   self.anki.update_evernote_notes(notes_updated) if tmr.counts.updated else 0)
        if tmr.counts.created.completed.subcount:
            db.executemany("DELETE FROM {t} WHERE title = ? and contents = ? ", queries2)
        if tmr.counts.updated.completed.subcount:
            db.executemany("DELETE FROM {t} WHERE guid = ? ", queries1)
        if tmr.is_success:
            db.commit()
        if tmr.should_retry:
            create_timer(30 if tmr.status.IsDelayableError else EVERNOTE.UPLOAD.RESTART_INTERVAL,
                         self.upload_validated_notes, True)
        return tmr.status, tmr.count, 0

    def create_toc_auto(self):
        db = ankDB()
        def check_old_values():
            old_values = db.first("UPPER(title) = UPPER(?) AND tagNames LIKE '{t_tauto}'",
                                  rootTitle, columns='guid, content')
            if not old_values:
                log.go(rootTitle, 'Add')
                return None, contents
            evernote_guid, old_content = old_values
            noteBodyUnencoded = self.evernote.makeNoteBody(contents, encode=False)
            if type(old_content) != type(noteBodyUnencoded):
                log.go([rootTitle, type(old_content), type(noteBodyUnencoded)], 'Update\\Diffs\\_')
                raise UnicodeWarning
            old_content = old_content.replace('guid-pending', evernote_guid).replace("'", '"')
            noteBodyUnencoded = noteBodyUnencoded.replace('guid-pending', evernote_guid).replace("'", '"')
            if old_content == noteBodyUnencoded:
                log.go(rootTitle, 'Skipped')
                tmr.reportSkipped()
                return None, None
            log.go(noteBodyUnencoded, 'Update\\New\\' + rootTitle, clear=True)
            log.go(generate_diff(old_content, noteBodyUnencoded), 'Update\\Diffs\\' + rootTitle, clear=True)
            return evernote_guid, contents.replace(
                '/guid-pending/', '/%s/' % evernote_guid).replace('/guid-pending/', '/%s/' % evernote_guid)

        update_regex()
        noteType = 'create-toc_auto_notes'
        db.delete("noteType = '%s'" % noteType, table=TABLES.NOTE_VALIDATION_QUEUE)
        NotesDB = EvernoteNotes()
        NotesDB.baseQuery = ANKNOTES.HIERARCHY.ROOT_TITLES_BASE_QUERY
        dbRows = NotesDB.populateAllNonCustomRootNotes()
        notes_created, notes_updated = [], []
        """
        :type: (list[EvernoteNote], list[EvernoteNote])
        """
        info = stopwatch.ActionInfo('Creation of Table of Content Note(s)', row_source='Root Title(s)')
        log = Logger('See Also\\2-%s\\' % noteType, rm_path=True)
        tmr = stopwatch.Timer(len(dbRows), 25, info, max_allowed=EVERNOTE.UPLOAD.MAX,
                              label=log.base_path)
        if tmr.actionInitializationFailed:
            return tmr.status, 0, 0
        for dbRow in dbRows:
            rootTitle, contents, tagNames, notebookGuid = dbRow.items()
            tagNames = (set(tagNames[1:-1].split(',')) | {TAGS.TOC, TAGS.TOC_AUTO} | (
                {"#Sandbox"} if EVERNOTE.API.IS_SANDBOXED else set())) - {TAGS.REVERSIBLE, TAGS.REVERSE_ONLY}
            rootTitle = generateTOCTitle(rootTitle)
            evernote_guid, contents = check_old_values()
            if contents is None:
                continue
            if not tmr.checkLimits():
                break
            if not EVERNOTE.UPLOAD.ENABLED:
                tmr.reportStatus(EvernoteAPIStatus.Disabled, title=rootTitle)
                continue
            whole_note = tmr.autoStep(
                self.evernote.makeNote(rootTitle, contents, tagNames, notebookGuid, noteType=noteType,
                                       guid=evernote_guid), rootTitle, evernote_guid)
            if tmr.report_result is False:
                raise ValueError
            if tmr.status.IsDelayableError:
                break
            if not tmr.status.IsSuccess:
                continue
            (notes_updated if evernote_guid else notes_created).append(EvernoteNotePrototype(whole_note=whole_note))
        tmr.Report(self.anki.add_evernote_notes(notes_created) if tmr.counts.created.completed else 0,
                   self.anki.update_evernote_notes(notes_updated) if tmr.counts.updated.completed else 0)
        if tmr.counts.queued:
            db.commit()
        return tmr.status, tmr.count, tmr.counts.skipped.val

    def update_ancillary_data(self):
        self.evernote.update_ancillary_data()

    def proceed(self, auto_paging=False):
        if not self.evernoteImporter:
            self.evernoteImporter = EvernoteImporter()
            self.evernoteImporter.anki = self.anki
            self.evernoteImporter.evernote = self.evernote
        self.evernoteImporter.forceAutoPage = self.forceAutoPage
        self.evernoteImporter.auto_page_callback = self.auto_page_callback
        if not hasattr(self, 'currentPage'):
            self.currentPage = 1
        self.evernoteImporter.currentPage = self.currentPage
        if hasattr(self, 'ManualGUIDs'):
            self.evernoteImporter.ManualGUIDs = self.ManualGUIDs
        self.evernoteImporter.proceed(auto_paging)

    def resync_with_local_db(self):
        log_banner('Resync With Local DB', clear=False, append_newline=False, prepend_newline=True)
        evernote_guids = get_all_local_db_guids()
        tmr = stopwatch.Timer(evernote_guids, strInfo='Resync Notes From Local DB', label='resync_with_local_db\\')
        results = self.evernote.create_evernote_notes(evernote_guids, use_local_db_only=True)
        """:type: EvernoteNoteFetcherResults"""
        log('  > Finished Creating Evernote Notes: '.ljust(40) + tmr.str_long)
        tmr.reset()
        number = self.anki.update_evernote_notes(results.Notes, log_update_if_unchanged=False)
        log('  > Finished Updating Anki Notes: '.ljust(40) + tmr.str_long)
        tooltip = '%d Evernote Notes Created<BR>%d Anki Notes Successfully Updated' % (results.Local, number)
        show_report('  > Resync with Local DB Complete', tooltip)
예제 #2
0
class Controller:
    evernoteImporter = None
    """:type : EvernoteImporter"""
    def __init__(self):
        self.forceAutoPage = False
        self.auto_page_callback = None
        self.anki = Anki()
        self.anki.deck = SETTINGS.ANKI.DECKS.BASE.fetch()
        self.anki.setup_ancillary_files()
        ankDB().Init()
        self.anki.add_evernote_models()
        self.evernote = Evernote()

    def test_anki(self, title, evernote_guid, filename=""):
        if not filename:
            filename = title
        fields = {
            FIELDS.TITLE:
            title,
            FIELDS.CONTENT:
            file(
                os.path.join(FOLDERS.LOGS,
                             filename.replace('.enex', '') + ".enex"),
                'r').read(),
            FIELDS.EVERNOTE_GUID:
            FIELDS.EVERNOTE_GUID_PREFIX + evernote_guid
        }
        tags = ['NoTags', 'NoTagsToRemove']
        return AnkiNotePrototype(self.anki, fields, tags)

    def process_unadded_see_also_notes(self):
        update_regex()
        anki_note_ids = self.anki.get_anknotes_note_ids_with_unadded_see_also()
        self.evernote.getNoteCount = 0
        self.anki.process_see_also_content(anki_note_ids)

    def upload_validated_notes(self, automated=False):
        db = ankDB(TABLES.NOTE_VALIDATION_QUEUE)
        dbRows = db.all("validation_status = 1")
        notes_created, notes_updated, queries1, queries2 = ([]
                                                            for i in range(4))
        """
        :type: (list[EvernoteNote], list[EvernoteNote], list[str], list[str])
        """
        noteFetcher = EvernoteNoteFetcher()
        tmr = stopwatch.Timer(len(dbRows),
                              25,
                              infoStr="Upload of Validated Evernote Notes",
                              automated=automated,
                              enabled=EVERNOTE.UPLOAD.ENABLED,
                              max_allowed=EVERNOTE.UPLOAD.MAX,
                              label='Validation\\upload_validated_notes\\',
                              display_initial_info=True)
        if tmr.actionInitializationFailed:
            return tmr.status, 0, 0
        for dbRow in dbRows:
            entry = EvernoteValidationEntry(dbRow)
            evernote_guid, rootTitle, contents, tagNames, notebookGuid, noteType = entry.items(
            )
            tagNames = tagNames.split(',')
            if not tmr.checkLimits():
                break
            whole_note = tmr.autoStep(
                self.evernote.makeNote(rootTitle,
                                       contents,
                                       tagNames,
                                       notebookGuid,
                                       guid=evernote_guid,
                                       noteType=noteType,
                                       validated=True), rootTitle,
                evernote_guid)
            if tmr.report_result is False:
                raise ValueError
            if tmr.status.IsDelayableError:
                break
            if not tmr.status.IsSuccess:
                continue
            if not whole_note.tagNames:
                whole_note.tagNames = tagNames
            noteFetcher.addNoteFromServerToDB(whole_note, tagNames)
            note = EvernoteNotePrototype(whole_note=whole_note)
            assert whole_note.tagNames
            assert note.Tags
            if evernote_guid:
                notes_updated.append(note)
                queries1.append([evernote_guid])
            else:
                notes_created.append(note)
                queries2.append([rootTitle, contents])
        else:
            tmr.reportNoBreak()
        tmr.Report(
            self.anki.add_evernote_notes(notes_created)
            if tmr.counts.created else 0,
            self.anki.update_evernote_notes(notes_updated)
            if tmr.counts.updated else 0)
        if tmr.counts.created.completed.subcount:
            db.executemany("DELETE FROM {t} WHERE title = ? and contents = ? ",
                           queries2)
        if tmr.counts.updated.completed.subcount:
            db.executemany("DELETE FROM {t} WHERE guid = ? ", queries1)
        if tmr.is_success:
            db.commit()
        if tmr.should_retry:
            create_timer(
                30 if tmr.status.IsDelayableError else
                EVERNOTE.UPLOAD.RESTART_INTERVAL, self.upload_validated_notes,
                True)
        return tmr.status, tmr.count, 0

    def create_toc_auto(self):
        db = ankDB()

        def check_old_values():
            old_values = db.first(
                "UPPER(title) = UPPER(?) AND tagNames LIKE '{t_tauto}'",
                rootTitle,
                columns='guid, content')
            if not old_values:
                log.go(rootTitle, 'Add')
                return None, contents
            evernote_guid, old_content = old_values
            noteBodyUnencoded = self.evernote.makeNoteBody(contents,
                                                           encode=False)
            if type(old_content) != type(noteBodyUnencoded):
                log.go([rootTitle,
                        type(old_content),
                        type(noteBodyUnencoded)], 'Update\\Diffs\\_')
                raise UnicodeWarning
            old_content = old_content.replace('guid-pending',
                                              evernote_guid).replace("'", '"')
            noteBodyUnencoded = noteBodyUnencoded.replace(
                'guid-pending', evernote_guid).replace("'", '"')
            if old_content == noteBodyUnencoded:
                log.go(rootTitle, 'Skipped')
                tmr.reportSkipped()
                return None, None
            log.go(noteBodyUnencoded, 'Update\\New\\' + rootTitle, clear=True)
            log.go(generate_diff(old_content, noteBodyUnencoded),
                   'Update\\Diffs\\' + rootTitle,
                   clear=True)
            return evernote_guid, contents.replace(
                '/guid-pending/',
                '/%s/' % evernote_guid).replace('/guid-pending/',
                                                '/%s/' % evernote_guid)

        update_regex()
        noteType = 'create-toc_auto_notes'
        db.delete("noteType = '%s'" % noteType,
                  table=TABLES.NOTE_VALIDATION_QUEUE)
        NotesDB = EvernoteNotes()
        NotesDB.baseQuery = ANKNOTES.HIERARCHY.ROOT_TITLES_BASE_QUERY
        dbRows = NotesDB.populateAllNonCustomRootNotes()
        notes_created, notes_updated = [], []
        """
        :type: (list[EvernoteNote], list[EvernoteNote])
        """
        info = stopwatch.ActionInfo('Creation of Table of Content Note(s)',
                                    row_source='Root Title(s)')
        log = Logger('See Also\\2-%s\\' % noteType, rm_path=True)
        tmr = stopwatch.Timer(len(dbRows),
                              25,
                              info,
                              max_allowed=EVERNOTE.UPLOAD.MAX,
                              label=log.base_path)
        if tmr.actionInitializationFailed:
            return tmr.status, 0, 0
        for dbRow in dbRows:
            rootTitle, contents, tagNames, notebookGuid = dbRow.items()
            tagNames = (set(tagNames[1:-1].split(','))
                        | {TAGS.TOC, TAGS.TOC_AUTO} |
                        ({"#Sandbox"} if EVERNOTE.API.IS_SANDBOXED else set())
                        ) - {TAGS.REVERSIBLE, TAGS.REVERSE_ONLY}
            rootTitle = generateTOCTitle(rootTitle)
            evernote_guid, contents = check_old_values()
            if contents is None:
                continue
            if not tmr.checkLimits():
                break
            if not EVERNOTE.UPLOAD.ENABLED:
                tmr.reportStatus(EvernoteAPIStatus.Disabled, title=rootTitle)
                continue
            whole_note = tmr.autoStep(
                self.evernote.makeNote(rootTitle,
                                       contents,
                                       tagNames,
                                       notebookGuid,
                                       noteType=noteType,
                                       guid=evernote_guid), rootTitle,
                evernote_guid)
            if tmr.report_result is False:
                raise ValueError
            if tmr.status.IsDelayableError:
                break
            if not tmr.status.IsSuccess:
                continue
            (notes_updated if evernote_guid else notes_created).append(
                EvernoteNotePrototype(whole_note=whole_note))
        tmr.Report(
            self.anki.add_evernote_notes(notes_created)
            if tmr.counts.created.completed else 0,
            self.anki.update_evernote_notes(notes_updated)
            if tmr.counts.updated.completed else 0)
        if tmr.counts.queued:
            db.commit()
        return tmr.status, tmr.count, tmr.counts.skipped.val

    def update_ancillary_data(self):
        self.evernote.update_ancillary_data()

    def proceed(self, auto_paging=False):
        if not self.evernoteImporter:
            self.evernoteImporter = EvernoteImporter()
            self.evernoteImporter.anki = self.anki
            self.evernoteImporter.evernote = self.evernote
        self.evernoteImporter.forceAutoPage = self.forceAutoPage
        self.evernoteImporter.auto_page_callback = self.auto_page_callback
        if not hasattr(self, 'currentPage'):
            self.currentPage = 1
        self.evernoteImporter.currentPage = self.currentPage
        if hasattr(self, 'ManualGUIDs'):
            self.evernoteImporter.ManualGUIDs = self.ManualGUIDs
        self.evernoteImporter.proceed(auto_paging)

    def resync_with_local_db(self):
        log_banner('Resync With Local DB',
                   clear=False,
                   append_newline=False,
                   prepend_newline=True)
        evernote_guids = get_all_local_db_guids()
        tmr = stopwatch.Timer(evernote_guids,
                              strInfo='Resync Notes From Local DB',
                              label='resync_with_local_db\\')
        results = self.evernote.create_evernote_notes(evernote_guids,
                                                      use_local_db_only=True)
        """:type: EvernoteNoteFetcherResults"""
        log('  > Finished Creating Evernote Notes: '.ljust(40) + tmr.str_long)
        tmr.reset()
        number = self.anki.update_evernote_notes(results.Notes,
                                                 log_update_if_unchanged=False)
        log('  > Finished Updating Anki Notes: '.ljust(40) + tmr.str_long)
        tooltip = '%d Evernote Notes Created<BR>%d Anki Notes Successfully Updated' % (
            results.Local, number)
        show_report('  > Resync with Local DB Complete', tooltip)
예제 #3
0
def main(evernote=None, anki=None):
    # @clockit
    def print_results(log_folder='Diff\\SeeAlso', full=False, final=False):
        if final:
            oldResults = n.old.content.final
            newResults = n.new.content.final
        elif full:
            oldResults = n.old.content.updated
            newResults = n.new.content.updated
        else:
            oldResults = n.old.see_also.updated
            newResults = n.new.see_also.updated
        diff = generate_diff(oldResults, newResults)
        if not 6 in FILES.LOGS.SEE_ALSO_DISABLED:
            log.plain(diff, log_folder + '\\Diff\\%s\\' % n.match_type + enNote.FullTitle, extension='htm', clear=True)
            log.plain(diffify(oldResults, split=False), log_folder + '\\Original\\%s\\' % n.match_type + enNote.FullTitle,
                      extension='htm', clear=True)
            log.plain(diffify(newResults, split=False), log_folder + '\\New\\%s\\' % n.match_type + enNote.FullTitle,
                      extension='htm', clear=True)
            if final:
                log.plain(oldResults, log_folder + '\\Final\\Old\\%s\\' % n.match_type + enNote.FullTitle, extension='htm',
                          clear=True)
                log.plain(newResults, log_folder + '\\Final\\New\\%s\\' % n.match_type + enNote.FullTitle, extension='htm',
                          clear=True)
        log.plain(diff + '\n', log_folder + '\\__All')

    # @clockit
    def process_note():
        n.old.content = notes.version.pstrings(enNote.Content)
        if not n.old.content.regex_original.successful_match:
            if n.new.see_also.original == "":
                n.new.content = notes.version.pstrings(n.old.content.original)
                return False
            n.new.content = notes.version.pstrings(n.old.content.original.replace('</en-note>',
                                                                                  '<div><span><br/></span></div>' + n.new.see_also.original + '\n</en-note>'))
            n.new.see_also.updated = str_process(n.new.content.original)
            n.old.see_also.updated = str_process(n.old.content.original)
            log.plain(enNote.Guid + '<BR>' + ', '.join(
                enNote.TagNames) + '<HR>' + enNote.Content + '<HR>' + n.new.see_also.updated,
                      'SeeAlsoMatchFail\\' + enNote.FullTitle, extension='htm', clear=True)
            n.match_type = 'V1'
        else:
            n.old.see_also = notes.version.pstrings(n.old.content.regex_original.main)
            n.match_type = 'V2'
            if n.old.see_also.regex_processed.successful_match:
                assert True or str_process(n.old.content.regex_original.main) is n.old.content.regex_processed.main
                n.old.content.updated = n.old.content.original.replace(n.old.content.regex_original.main,
                                                                       str_process(n.old.content.regex_original.main))
                n.old.see_also.useProcessed()
                n.match_type += 'V3'
            n.new.see_also.regex_original.subject = n.new.see_also.original + '</en-note>'
            if not n.new.see_also.regex_original.successful_match:
                log.plain(enNote.Guid + '\n' + ', '.join(enNote.TagNames) + '\n' + n.new.see_also.original,
                          'SeeAlsoNewMatchFail\\' + enNote.FullTitle, extension='htm', clear=True)
                # see_also_replace_old = n.old.content.original.match.processed.see_also.processed.content
                n.old.see_also.updated = n.old.content.regex_updated.see_also
                n.new.see_also.updated = n.new.see_also.processed
                n.match_type += 'V4'
            else:
                assert (n.old.content.regex_processed.see_also_content == notes.version.see_also_match(
                    str_process(n.old.content.regex_original.main)).see_also_content)
                n.old.see_also.updated = notes.version.see_also_match(
                    str_process(n.old.content.regex_original.main)).see_also_content
                n.new.see_also.updated = str_process(n.new.see_also.regex_original.see_also_content)
                n.match_type += 'V5'
            n.new.content.updated = n.old.content.updated.replace(n.old.see_also.updated, n.new.see_also.updated)

    def print_results_fail(title, status=None):
        log.go(title + ' for %s' % enNote.FullTitle, 'NoUpdate')
        print_results('NoMatch\\SeeAlso')
        print_results('NoMatch\\Contents', full=True)
        if status is None:
            status = EvernoteAPIStatus.GenericError
        tmr.reportStatus(status)

    noteType = 'SeeAlso-Step6'
    db = ankDB()
    db.delete("noteType = '%s'" % noteType, table=TABLES.NOTE_VALIDATION_QUEUE)
    results = db.all("SELECT DISTINCT s.target_evernote_guid, n.* FROM {s} as s, {n} as n  "
                     "WHERE s.target_evernote_guid = n.guid AND n.tagNames NOT LIKE '{t_toc}' "
                     "AND n.tagNames NOT LIKE '{t_out}' ORDER BY n.title ASC;")
    # count_queued = 0
    log = Logger('See Also\\6-update_see_also_footer_in_evernote_notes\\', rm_path=True)
    tmr = stopwatch.Timer(len(results), 25, infoStr='Updating Evernote See Also Notes',
                          label=log.base_path, do_print=True)
    # log.banner("UPDATING EVERNOTE SEE ALSO CONTENT: %d NOTES" % len(results), do_print=True)
    notes_updated = []
    # number_updated = 0
    for result in results:
        enNote = EvernoteNotePrototype(db_note=result)
        n = notes()
        tmr.step(enNote.FullTitle if enNote.Status.IsSuccess else '(%s)' % enNote.Guid)
        flds = get_anki_fields_from_evernote_guids(enNote.Guid)
        if not flds:
            print_results_fail('No Anki Note Found')
            continue
        flds = flds.split("\x1f")
        n.new.see_also = notes.version.pstrings(flds[FIELDS.ORD.SEE_ALSO])
        result = process_note()
        if result is False:
            print_results_fail('No Match')
            continue
        if n.match_type != 'V1' and str_process(n.old.see_also.updated) == n.new.see_also.updated:
            print_results_fail('Match but contents are the same', EvernoteAPIStatus.RequestSkipped)
            continue
        print_results()
        print_results('Diff\\Contents', final=True)
        enNote.Content = n.new.content.final
        if not EVERNOTE.UPLOAD.ENABLED:
            tmr.reportStatus(EvernoteAPIStatus.Disabled)
            continue
        if not evernote:
            evernote = Evernote()
        whole_note = tmr.autoStep(evernote.makeNote(enNote=enNote, noteType=noteType), update=True)
        if tmr.report_result is False:
            raise ValueError
        if tmr.status.IsDelayableError:
            break
        if tmr.status.IsSuccess:
            notes_updated.append(EvernoteNotePrototype(whole_note=whole_note))
    if tmr.is_success and not anki:
        anki = Anki()
    tmr.Report(0, anki.update_evernote_notes(notes_updated) if tmr.is_success else 0)
예제 #4
0
def main(evernote=None, anki=None):
    # @clockit
    def print_results(log_folder='Diff\\SeeAlso', full=False, final=False):
        if final:
            oldResults = n.old.content.final
            newResults = n.new.content.final
        elif full:
            oldResults = n.old.content.updated
            newResults = n.new.content.updated
        else:
            oldResults = n.old.see_also.updated
            newResults = n.new.see_also.updated
        diff = generate_diff(oldResults, newResults)
        if not 6 in FILES.LOGS.SEE_ALSO_DISABLED:
            log.plain(diff,
                      log_folder + '\\Diff\\%s\\' % n.match_type +
                      enNote.FullTitle,
                      extension='htm',
                      clear=True)
            log.plain(diffify(oldResults, split=False),
                      log_folder + '\\Original\\%s\\' % n.match_type +
                      enNote.FullTitle,
                      extension='htm',
                      clear=True)
            log.plain(diffify(newResults, split=False),
                      log_folder + '\\New\\%s\\' % n.match_type +
                      enNote.FullTitle,
                      extension='htm',
                      clear=True)
            if final:
                log.plain(oldResults,
                          log_folder + '\\Final\\Old\\%s\\' % n.match_type +
                          enNote.FullTitle,
                          extension='htm',
                          clear=True)
                log.plain(newResults,
                          log_folder + '\\Final\\New\\%s\\' % n.match_type +
                          enNote.FullTitle,
                          extension='htm',
                          clear=True)
        log.plain(diff + '\n', log_folder + '\\__All')

    # @clockit
    def process_note():
        n.old.content = notes.version.pstrings(enNote.Content)
        if not n.old.content.regex_original.successful_match:
            if n.new.see_also.original == "":
                n.new.content = notes.version.pstrings(n.old.content.original)
                return False
            n.new.content = notes.version.pstrings(
                n.old.content.original.replace(
                    '</en-note>', '<div><span><br/></span></div>' +
                    n.new.see_also.original + '\n</en-note>'))
            n.new.see_also.updated = str_process(n.new.content.original)
            n.old.see_also.updated = str_process(n.old.content.original)
            log.plain(enNote.Guid + '<BR>' + ', '.join(enNote.TagNames) +
                      '<HR>' + enNote.Content + '<HR>' +
                      n.new.see_also.updated,
                      'SeeAlsoMatchFail\\' + enNote.FullTitle,
                      extension='htm',
                      clear=True)
            n.match_type = 'V1'
        else:
            n.old.see_also = notes.version.pstrings(
                n.old.content.regex_original.main)
            n.match_type = 'V2'
            if n.old.see_also.regex_processed.successful_match:
                assert True or str_process(
                    n.old.content.regex_original.main
                ) is n.old.content.regex_processed.main
                n.old.content.updated = n.old.content.original.replace(
                    n.old.content.regex_original.main,
                    str_process(n.old.content.regex_original.main))
                n.old.see_also.useProcessed()
                n.match_type += 'V3'
            n.new.see_also.regex_original.subject = n.new.see_also.original + '</en-note>'
            if not n.new.see_also.regex_original.successful_match:
                log.plain(enNote.Guid + '\n' + ', '.join(enNote.TagNames) +
                          '\n' + n.new.see_also.original,
                          'SeeAlsoNewMatchFail\\' + enNote.FullTitle,
                          extension='htm',
                          clear=True)
                # see_also_replace_old = n.old.content.original.match.processed.see_also.processed.content
                n.old.see_also.updated = n.old.content.regex_updated.see_also
                n.new.see_also.updated = n.new.see_also.processed
                n.match_type += 'V4'
            else:
                assert (n.old.content.regex_processed.see_also_content ==
                        notes.version.see_also_match(
                            str_process(n.old.content.regex_original.main)
                        ).see_also_content)
                n.old.see_also.updated = notes.version.see_also_match(
                    str_process(
                        n.old.content.regex_original.main)).see_also_content
                n.new.see_also.updated = str_process(
                    n.new.see_also.regex_original.see_also_content)
                n.match_type += 'V5'
            n.new.content.updated = n.old.content.updated.replace(
                n.old.see_also.updated, n.new.see_also.updated)

    def print_results_fail(title, status=None):
        log.go(title + ' for %s' % enNote.FullTitle, 'NoUpdate')
        print_results('NoMatch\\SeeAlso')
        print_results('NoMatch\\Contents', full=True)
        if status is None:
            status = EvernoteAPIStatus.GenericError
        tmr.reportStatus(status)

    noteType = 'SeeAlso-Step6'
    db = ankDB()
    db.delete("noteType = '%s'" % noteType, table=TABLES.NOTE_VALIDATION_QUEUE)
    results = db.all(
        "SELECT DISTINCT s.target_evernote_guid, n.* FROM {s} as s, {n} as n  "
        "WHERE s.target_evernote_guid = n.guid AND n.tagNames NOT LIKE '{t_toc}' "
        "AND n.tagNames NOT LIKE '{t_out}' ORDER BY n.title ASC;")
    # count_queued = 0
    log = Logger('See Also\\6-update_see_also_footer_in_evernote_notes\\',
                 rm_path=True)
    tmr = stopwatch.Timer(len(results),
                          25,
                          infoStr='Updating Evernote See Also Notes',
                          label=log.base_path,
                          do_print=True)
    # log.banner("UPDATING EVERNOTE SEE ALSO CONTENT: %d NOTES" % len(results), do_print=True)
    notes_updated = []
    # number_updated = 0
    for result in results:
        enNote = EvernoteNotePrototype(db_note=result)
        n = notes()
        tmr.step(enNote.FullTitle if enNote.Status.IsSuccess else '(%s)' %
                 enNote.Guid)
        flds = get_anki_fields_from_evernote_guids(enNote.Guid)
        if not flds:
            print_results_fail('No Anki Note Found')
            continue
        flds = flds.split("\x1f")
        n.new.see_also = notes.version.pstrings(flds[FIELDS.ORD.SEE_ALSO])
        result = process_note()
        if result is False:
            print_results_fail('No Match')
            continue
        if n.match_type != 'V1' and str_process(
                n.old.see_also.updated) == n.new.see_also.updated:
            print_results_fail('Match but contents are the same',
                               EvernoteAPIStatus.RequestSkipped)
            continue
        print_results()
        print_results('Diff\\Contents', final=True)
        enNote.Content = n.new.content.final
        if not EVERNOTE.UPLOAD.ENABLED:
            tmr.reportStatus(EvernoteAPIStatus.Disabled)
            continue
        if not evernote:
            evernote = Evernote()
        whole_note = tmr.autoStep(evernote.makeNote(enNote=enNote,
                                                    noteType=noteType),
                                  update=True)
        if tmr.report_result is False:
            raise ValueError
        if tmr.status.IsDelayableError:
            break
        if tmr.status.IsSuccess:
            notes_updated.append(EvernoteNotePrototype(whole_note=whole_note))
    if tmr.is_success and not anki:
        anki = Anki()
    tmr.Report(
        0,
        anki.update_evernote_notes(notes_updated) if tmr.is_success else 0)