示例#1
0
def generate_diff(value_original, value):
    try:
        return '\n'.join(
            list(
                difflib.unified_diff(diffify(value_original),
                                     diffify(value),
                                     lineterm='')))
    except Exception:
        pass
    try:
        return '\n'.join(
            list(
                difflib.unified_diff(diffify(decode(value_original)),
                                     diffify(value),
                                     lineterm='')))
    except Exception:
        pass
    try:
        return '\n'.join(
            list(
                difflib.unified_diff(diffify(value_original),
                                     diffify(decode(value)),
                                     lineterm='')))
    except Exception:
        pass
    try:
        return '\n'.join(
            list(
                difflib.unified_diff(diffify(decode(value_original)),
                                     diffify(decode(value)),
                                     lineterm='')))
    except Exception:
        raise
示例#2
0
def try_print(full_path, content, prefix='', line_prefix=u'\n ', attempt=0, clear=False, timestamp=True, **kwargs):
    try:
        st = '[%s]: ' % datetime.now().strftime(ANKNOTES.DATE_FORMAT) if timestamp else ''
        print_content = line_prefix + (u' <%d>' % attempt if attempt > 0 else u'') + u' ' + st
        if attempt is 0:
            print_content += content
        elif attempt is 1:
            print_content += decode(content)
        elif attempt is 2:
            print_content += encode(content)
        elif attempt is 3:
            print_content = encode(print_content) + encode(content)
        elif attempt is 4:
            print_content = decode(print_content) + decode(content)
        elif attempt is 5:
            print_content += "Error printing content: " + str_safe(content)
        elif attempt is 6:
            print_content += "Error printing content: " + content[:10]
        elif attempt is 7:
            print_content += "Unable to print content."
        with open(full_path, 'w+' if clear else 'a+') as fileLog:
            print>> fileLog, print_content
    except Exception as e:
        if attempt < 8:
            try_print(full_path, content, prefix=prefix, line_prefix=line_prefix, attempt=attempt + 1,
                                  clear=clear)
        else:
            log("Try print error to %s: %s" % (os.path.split(full_path)[1], str(e)))
    def __init__(self,
                 title=None,
                 content=None,
                 guid=None,
                 tags=None,
                 notebookGuid=None,
                 updateSequenceNum=None,
                 whole_note=None,
                 db_note=None):
        """

        :type whole_note: evernote.edam.type.ttypes.Note
        :type db_note: sqlite3.dbapi2.Row
        """

        self.Status = EvernoteAPIStatus.Uninitialized
        self.TagNames = tags
        if whole_note is not None:
            if self.TagNames is None:
                self.TagNames = whole_note.tagNames
            self.Title = EvernoteNoteTitle(whole_note)
            self.Content = whole_note.content
            self.Guid = whole_note.guid
            self.NotebookGuid = whole_note.notebookGuid
            self.UpdateSequenceNum = whole_note.updateSequenceNum
            self.Status = EvernoteAPIStatus.Success
            return
        if db_note is not None:
            self.Title = EvernoteNoteTitle(db_note)
            db_note_keys = db_note.keys()
            for key in [
                    'content', 'guid', 'notebookGuid', 'updateSequenceNum',
                    'tagNames', 'tagGuids'
            ]:
                if not key in db_note_keys:
                    log_error(
                        "FATAL ERROR: Unable to find key %s in db note %s! \n%s"
                        % (key, self.FullTitle, db_note_keys))
                    log(
                        "Values: \n\n" +
                        str({k: db_note[k]
                             for k in db_note_keys}),
                        'EvernoteNotePrototypeInit')
                else:
                    setattr(self, upperFirst(key), db_note[key])
            self.TagNames = decode(self.TagNames)
            self.Content = decode(self.Content)
            self.process_tags()
            self.Status = EvernoteAPIStatus.Success
            return
        self.Title = EvernoteNoteTitle(title)
        self.Content = content
        self.Guid = guid
        self.NotebookGuid = notebookGuid
        self.UpdateSequenceNum = updateSequenceNum
        self.Status = EvernoteAPIStatus.Manual
示例#4
0
    def add_evernote_notes(self, evernote_notes, update=False, log_update_if_unchanged=True):
        """
        Add Notes to or Update Notes in Anki Database
        :param evernote_notes:
        :param update:
        :param log_update_if_unchanged:
        :type evernote_notes: list[EvernoteNotePrototype.EvernoteNotePrototype]
        :type update: bool
        :return: Count of notes successfully added or updated
        """
        new_nids=[]
        action_str_base = ['Add', 'Update'][update]
        action_str = ['Adding', 'Updating'][update]
        action_preposition = ['To', 'In'][update]
        info = stopwatch.ActionInfo(action_str + ' Of', 'Evernote Notes', action_preposition + ' Anki', report_if_empty=False)
        tmr = stopwatch.Timer(evernote_notes, 10, info=info,
                              label='Add\\Anki-%sEvernoteNotes' % action_str_base)

        for ankiNote in evernote_notes:
            try:
                title = ankiNote.FullTitle
                content = decode(ankiNote.Content)
                anki_field_info = {
                    FIELDS.TITLE:               title,
                    FIELDS.CONTENT:             content,
                    FIELDS.EVERNOTE_GUID:       FIELDS.EVERNOTE_GUID_PREFIX + ankiNote.Guid,
                    FIELDS.UPDATE_SEQUENCE_NUM: str(ankiNote.UpdateSequenceNum),
                    FIELDS.SEE_ALSO:            u''
                }
            except Exception:
                log_error("Unable to set field info for: Note '%s': '%s'" % (ankiNote.FullTitle, ankiNote.Guid))
                log_dump(ankiNote.Content, " NOTE CONTENTS ")
                # log_dump(encode(ankiNote.Content), " NOTE CONTENTS ")
                raise
            tmr.step(title)
            baseNote = None
            if update:
                baseNote = self.get_anki_note_from_evernote_guid(ankiNote.Guid)
                if not baseNote:
                    log_error('Updating note %s: COULD NOT FIND BASE NOTE FOR ANKI NOTE ID' % ankiNote.Guid)
                    tmr.reportStatus(EvernoteAPIStatus.MissingDataError)
                    continue
            if ankiNote.Tags is None:
                log_error("Could note find tags object for note %s: %s. " % (ankiNote.Guid, ankiNote.FullTitle))
                tmr.reportStatus(EvernoteAPIStatus.MissingDataError)
                continue
            anki_note_prototype = AnkiNotePrototype(self, anki_field_info, ankiNote.TagNames, baseNote,
                                                    notebookGuid=ankiNote.NotebookGuid, count=tmr.count,
                                                    count_update=tmr.counts.updated.completed.val, max_count=tmr.max)
            anki_note_prototype._log_update_if_unchanged_ = log_update_if_unchanged
            nid = tmr.autoStep(anki_note_prototype.update_note() if update else anki_note_prototype.add_note(),
                               ankiNote.FullTitle, update)
            if tmr.status.IsSuccess and not update:
                new_nids.append([nid, ankiNote.Guid])
            elif tmr.status.IsError:
                log("ANKI ERROR WHILE %s EVERNOTE NOTES: " % action_str.upper() + str(tmr.status), tmr.label + '-Error')
        tmr.Report()
        if new_nids:
            ankDB().executemany("UPDATE {n} SET nid = ? WHERE guid = ?", new_nids)
        return tmr.counts.success
示例#5
0
def log_dump(obj, title="Object", filename='', crosspost_to_default=True, **kwargs):
    content = pprint.pformat(obj, indent=4, width=ANKNOTES.FORMATTING.PPRINT_WIDTH)
    try:
        content = decode(content, errors='ignore')
    except Exception:
        pass
    content = content.replace("\\n", '\n').replace('\\r', '\r')
    if filename and filename[0] is '+':
        summary = " ** CROSS-POST TO %s: " % filename[1:] + content
        log(summary[:200])
    full_path = get_log_full_path(filename, prefix='dump', **kwargs)
    if full_path is False:
        return
    if not title:
        title = "<%s>" % obj.__class__.__name__
    if title.startswith('-'):
        crosspost_to_default = False; title = title[1:]
    prefix = " **** Dumping %s" % title
    if crosspost_to_default:
        log(prefix + + " to " + os.path.splitext(full_path.replace(FOLDERS.LOGS + os.path.sep, ''))[0])

    content = encode_log_text(content)

    try:
        prefix += '\r\n'
        content = prefix + content.replace(', ', ', \n ')
        content = content.replace("': {", "': {\n ")
        content = content.replace('\r', '\r' + ' ' * 30).replace('\n', '\n' + ' ' * 30)
    except Exception:
        pass

    if not os.path.exists(os.path.dirname(full_path)):
        os.makedirs(os.path.dirname(full_path))
    try_print(full_path, content, prefix, **kwargs)
示例#6
0
def pad_lines_regex(content, timestamp=None, replace_newline=None, try_decode=True, **kwargs):
    content = PadLines(content, **kwargs)
    if not (timestamp and replace_newline is not False) and not replace_newline:
        return content
    try:
        return re.sub(r'[\r\n]+', u'\n' + ANKNOTES.FORMATTING.TIMESTAMP_PAD, content)
    except UnicodeDecodeError:
        if not try_decode:
            raise
    return re.sub(r'[\r\n]+', u'\n' + ANKNOTES.FORMATTING.TIMESTAMP_PAD, decode(content))
示例#7
0
def try_print(full_path,
              content,
              prefix='',
              line_prefix=u'\n ',
              attempt=0,
              clear=False,
              timestamp=True,
              **kwargs):
    try:
        st = '[%s]: ' % datetime.now().strftime(
            ANKNOTES.DATE_FORMAT) if timestamp else ''
        print_content = line_prefix + (u' <%d>' % attempt
                                       if attempt > 0 else u'') + u' ' + st
        if attempt is 0:
            print_content += content
        elif attempt is 1:
            print_content += decode(content)
        elif attempt is 2:
            print_content += encode(content)
        elif attempt is 3:
            print_content = encode(print_content) + encode(content)
        elif attempt is 4:
            print_content = decode(print_content) + decode(content)
        elif attempt is 5:
            print_content += "Error printing content: " + str_safe(content)
        elif attempt is 6:
            print_content += "Error printing content: " + content[:10]
        elif attempt is 7:
            print_content += "Unable to print content."
        with open(full_path, 'w+' if clear else 'a+') as fileLog:
            print >> fileLog, print_content
    except Exception as e:
        if attempt < 8:
            try_print(full_path,
                      content,
                      prefix=prefix,
                      line_prefix=line_prefix,
                      attempt=attempt + 1,
                      clear=clear)
        else:
            log("Try print error to %s: %s" %
                (os.path.split(full_path)[1], str(e)))
示例#8
0
def generate_diff(value_original, value):
    try:
        return '\n'.join(list(difflib.unified_diff(diffify(value_original), diffify(value), lineterm='')))
    except Exception:
        pass
    try:
        return '\n'.join(
        list(difflib.unified_diff(diffify(decode(value_original)), diffify(value), lineterm='')))
    except Exception:
        pass
    try:
        return '\n'.join(
        list(difflib.unified_diff(diffify(value_original), diffify(decode(value)), lineterm='')))
    except Exception:
        pass
    try:
        return '\n'.join(list(
        difflib.unified_diff(diffify(decode(value_original)), diffify(decode(value)), lineterm='')))
    except Exception:
        raise
    def __init__(self, title=None, content=None, guid=None, tags=None, notebookGuid=None, updateSequenceNum=None,
                 whole_note=None, db_note=None):
        """

        :type whole_note: evernote.edam.type.ttypes.Note
        :type db_note: sqlite3.dbapi2.Row
        """

        self.Status = EvernoteAPIStatus.Uninitialized
        self.TagNames = tags
        if whole_note is not None:
            if self.TagNames is None:
                self.TagNames = whole_note.tagNames
            self.Title = EvernoteNoteTitle(whole_note)
            self.Content = whole_note.content
            self.Guid = whole_note.guid
            self.NotebookGuid = whole_note.notebookGuid
            self.UpdateSequenceNum = whole_note.updateSequenceNum
            self.Status = EvernoteAPIStatus.Success
            return
        if db_note is not None:
            self.Title = EvernoteNoteTitle(db_note)
            db_note_keys = db_note.keys()
            for key in ['content', 'guid', 'notebookGuid', 'updateSequenceNum', 'tagNames', 'tagGuids']:
                if not key in db_note_keys:
                    log_error(
                        "FATAL ERROR: Unable to find key %s in db note %s! \n%s" % (key, self.FullTitle, db_note_keys))
                    log("Values: \n\n" + str({k: db_note[k] for k in db_note_keys}), 'EvernoteNotePrototypeInit')
                else:
                    setattr(self, upperFirst(key), db_note[key])
            self.TagNames = decode(self.TagNames)
            self.Content = decode(self.Content)
            self.process_tags()
            self.Status = EvernoteAPIStatus.Success
            return
        self.Title = EvernoteNoteTitle(title)
        self.Content = content
        self.Guid = guid
        self.NotebookGuid = notebookGuid
        self.UpdateSequenceNum = updateSequenceNum
        self.Status = EvernoteAPIStatus.Manual
示例#10
0
def pf(obj, title='', pf_replace_newline=True, pf_encode_text=True, pf_decode_text=False, *a, **kw):
    content = pprint.pformat(obj, indent=4, width=ANKNOTES.FORMATTING.PPRINT_WIDTH)
    content = content.replace(', ', ', \n ')
    if pf_replace_newline:
        content = content.replace('\r', '\r' + ' ' * 30).replace('\n', '\n' + ' ' * 30)
    if pf_encode_text:
        content = encode_log_text(content)
    elif pf_decode_text:
        content = decode(content, errors='ignore')
    if title:
        content = title + ": " + content
    return content
示例#11
0
def unescape_text(title, try_decoding=False):
    title_orig = title
    global __text_escape_phrases
    if try_decoding:
        title = decode(title)
    try:
        for i in range(0, len(__text_escape_phrases), 2):
            title = title.replace(__text_escape_phrases[i + 1], __text_escape_phrases[i])
        title = title.replace(u"&nbsp;", u" ")
    except Exception:
        if try_decoding:
            raise UnicodeError
        title_new = unescape_text(title, True)
        log(title + '\n' + title_new + '\n\n', 'unicode')
        return title_new
    return title
示例#12
0
def unescape_text(title, try_decoding=False):
    title_orig = title
    global __text_escape_phrases
    if try_decoding:
        title = decode(title)
    try:
        for i in range(0, len(__text_escape_phrases), 2):
            title = title.replace(__text_escape_phrases[i + 1],
                                  __text_escape_phrases[i])
        title = title.replace(u"&nbsp;", u" ")
    except Exception:
        if try_decoding:
            raise UnicodeError
        title_new = unescape_text(title, True)
        log(title + '\n' + title_new + '\n\n', 'unicode')
        return title_new
    return title
示例#13
0
def pad_lines_regex(content,
                    timestamp=None,
                    replace_newline=None,
                    try_decode=True,
                    **kwargs):
    content = PadLines(content, **kwargs)
    if not (timestamp
            and replace_newline is not False) and not replace_newline:
        return content
    try:
        return re.sub(r'[\r\n]+', u'\n' + ANKNOTES.FORMATTING.TIMESTAMP_PAD,
                      content)
    except UnicodeDecodeError:
        if not try_decode:
            raise
    return re.sub(r'[\r\n]+', u'\n' + ANKNOTES.FORMATTING.TIMESTAMP_PAD,
                  decode(content))
示例#14
0
def log_dump(obj,
             title="Object",
             filename='',
             crosspost_to_default=True,
             **kwargs):
    content = pprint.pformat(obj,
                             indent=4,
                             width=ANKNOTES.FORMATTING.PPRINT_WIDTH)
    try:
        content = decode(content, errors='ignore')
    except Exception:
        pass
    content = content.replace("\\n", '\n').replace('\\r', '\r')
    if filename and filename[0] is '+':
        summary = " ** CROSS-POST TO %s: " % filename[1:] + content
        log(summary[:200])
    full_path = get_log_full_path(filename, prefix='dump', **kwargs)
    if full_path is False:
        return
    if not title:
        title = "<%s>" % obj.__class__.__name__
    if title.startswith('-'):
        crosspost_to_default = False
        title = title[1:]
    prefix = " **** Dumping %s" % title
    if crosspost_to_default:
        log(prefix + + " to " +
            os.path.splitext(full_path.replace(FOLDERS.LOGS +
                                               os.path.sep, ''))[0])

    content = encode_log_text(content)

    try:
        prefix += '\r\n'
        content = prefix + content.replace(', ', ', \n ')
        content = content.replace("': {", "': {\n ")
        content = content.replace('\r', '\r' + ' ' * 30).replace(
            '\n', '\n' + ' ' * 30)
    except Exception:
        pass

    if not os.path.exists(os.path.dirname(full_path)):
        os.makedirs(os.path.dirname(full_path))
    try_print(full_path, content, prefix, **kwargs)
示例#15
0
def pf(obj,
       title='',
       pf_replace_newline=True,
       pf_encode_text=True,
       pf_decode_text=False,
       *a,
       **kw):
    content = pprint.pformat(obj,
                             indent=4,
                             width=ANKNOTES.FORMATTING.PPRINT_WIDTH)
    content = content.replace(', ', ', \n ')
    if pf_replace_newline:
        content = content.replace('\r', '\r' + ' ' * 30).replace(
            '\n', '\n' + ' ' * 30)
    if pf_encode_text:
        content = encode_log_text(content)
    elif pf_decode_text:
        content = decode(content, errors='ignore')
    if title:
        content = title + ": " + content
    return content
示例#16
0
 def addNoteFromServerToDB(self, whole_note=None, tag_names=None):
     """
     Adds note to Anknote DB from an Evernote Note object provided by the Evernote API
     :type whole_note : evernote.edam.type.ttypes.Note
     """
     if whole_note:
         self.whole_note = whole_note
     if tag_names:
         self.tagNames = tag_names
     log('Adding  %s: %s' % (self.whole_note.guid, self.whole_note.title), 'ankDB')
     if not self.tagGuids:
         self.tagGuids = self.whole_note.tagGuids
     auto_columns = ['guid', 'title', 'content', 'updated', 'created', 'updateSequenceNum', 'notebookGuid']
     columns = {key: getattr(self.whole_note, key) for key in auto_columns}
     columns.update({key: getattr(self, key) for key in ['tagNames', 'tagGuids']})
     for key, value in columns.items():
         if isinstance(value, list):
             columns[key] = u',' + u','.join(map(decode, value)) + u','
         elif isinstance(value, str):
             columns[key] = decode(value)
     db = ankDB()
     db.insert_or_replace(columns)
     db.insert(columns, table=TABLES.EVERNOTE.NOTES_HISTORY)
     db.commit()
示例#17
0
def clean_title(title):
    title = unescape_text(decode(title))
    title = re.sub(r'( |\xa0)+', ' ', decode(title))
    return title
示例#18
0
    def insert_toc_and_outline_contents_into_notes(self):
        linked_notes_fields = {}
        db = ankDB(TABLES.SEE_ALSO)
        source_guids = db.list(
            "SELECT DISTINCT s.source_evernote_guid FROM {s} s, {n} n WHERE (s.is_toc = 1 OR "
            "s.is_outline = 1) AND s.source_evernote_guid = n.guid ORDER BY n.title ASC"
        )
        info = stopwatch.ActionInfo('Insertion of', 'TOC/Outline Contents',
                                    'Into Target Anki Notes')
        log = Logger('See Also\\8-insert_toc_contents\\',
                     rm_path=True,
                     timestamp=False)
        tmr = stopwatch.Timer(source_guids, 25, info=info, label=log.base_path)
        tmr.info.BannerHeader('error')
        for source_guid in source_guids:
            note = self.get_anki_note_from_evernote_guid(source_guid)
            if not note:
                tmr.reportStatus(EvernoteAPIStatus.NotFoundError)
                log.error("Could not find note for %s for %s" %
                          (note.guid, tmr.base_name))
                continue
            # if TAGS.TOC in note.tags:
            # tmr.reportSkipped()
            # continue
            for fld in note._model['flds']:
                if FIELDS.TITLE in fld.get('name'):
                    note_title = note.fields[fld.get('ord')]
                    continue
            if not note_title:
                tmr.reportStatus(EvernoteAPIStatus.NotFoundError)
                log.error("Could not find note title for %s for %s" %
                          (note.guid, tmr.base_name))
                continue
            tmr.step(note_title)
            note_toc = ""
            note_outline = ""
            toc_header = ""
            outline_header = ""
            toc_count = 0
            outline_count = 0
            toc_and_outline_links = db.execute(
                "source_evernote_guid = '%s' AND (is_toc = 1 OR is_outline = 1) "
                "ORDER BY number ASC" % source_guid,
                columns='target_evernote_guid, is_toc, is_outline')
            for target_evernote_guid, is_toc, is_outline in toc_and_outline_links:
                if target_evernote_guid in linked_notes_fields:
                    linked_note_contents = linked_notes_fields[
                        target_evernote_guid][FIELDS.CONTENT]
                    linked_note_title = linked_notes_fields[
                        target_evernote_guid][FIELDS.TITLE]
                else:
                    linked_note = self.get_anki_note_from_evernote_guid(
                        target_evernote_guid)
                    if not linked_note:
                        continue
                    linked_note_contents = u""
                    for fld in linked_note._model['flds']:
                        if FIELDS.CONTENT in fld.get('name'):
                            linked_note_contents = linked_note.fields[fld.get(
                                'ord')]
                        elif FIELDS.TITLE in fld.get('name'):
                            linked_note_title = linked_note.fields[fld.get(
                                'ord')]
                    if linked_note_contents:
                        linked_notes_fields[target_evernote_guid] = {
                            FIELDS.TITLE: linked_note_title,
                            FIELDS.CONTENT: linked_note_contents
                        }
                if linked_note_contents:
                    linked_note_contents = decode(linked_note_contents)
                    if is_toc:
                        toc_count += 1
                        if toc_count is 1:
                            toc_header = "<span class='header'>TABLE OF CONTENTS</span>: 1. <span class='header'>%s</span>" % linked_note_title
                        else:
                            note_toc += "<br><hr>"
                            toc_header += "<span class='See_Also'> | </span> %d. <span class='header'>%s</span>" % (
                                toc_count, linked_note_title)
                        note_toc += linked_note_contents
                    else:
                        outline_count += 1
                        if outline_count is 1:
                            outline_header = "<span class='header'>OUTLINE</span>: 1. <span class='header'>%s</span>" % linked_note_title
                        else:
                            note_outline += "<BR><HR>"
                            outline_header += "<span class='See_Also'> | </span> %d. <span class='header'>%s</span>" % (
                                outline_count, linked_note_title)
                        note_outline += linked_note_contents
            if outline_count + toc_count is 0:
                tmr.reportError(EvernoteAPIStatus.MissingDataError)
                log.error(" No Valid TOCs or Outlines Found: %s" % note_title)
                continue
            tmr.reportSuccess()

            def makestr(title, count):
                return '' if not count else 'One %s ' % title if count is 1 else '%s %ss' % (
                    str(count).center(3), title)

            toc_str = makestr('TOC', toc_count).rjust(8)  #if toc_count else ''
            outline_str = makestr('Outline', outline_count).ljust(
                12)  #if outline_count else ''
            toc_str += ' &  ' if toc_count and outline_count else '    '

            log.go(" [%4d/%4d]  +   %s   for Note %s:   %s" %
                   (tmr.count, tmr.max, toc_str + outline_str,
                    source_guid.split('-')[0], note_title))

            if outline_count > 1:
                note_outline = "<span class='Outline'>%s</span><BR><BR>" % outline_header + note_outline
            if toc_count > 1:
                note_toc = "<span class='TOC'>%s</span><BR><BR>" % toc_header + note_toc
            for fld in note._model['flds']:
                if FIELDS.TOC in fld.get('name'):
                    note.fields[fld.get('ord')] = note_toc
                elif FIELDS.OUTLINE in fld.get('name'):
                    note.fields[fld.get('ord')] = note_outline
            # log.go(' '*16 + "> Flushing Note \r\n")
            note.flush(intTime())
        tmr.Report()
示例#19
0
    def add_evernote_notes(self,
                           evernote_notes,
                           update=False,
                           log_update_if_unchanged=True):
        """
        Add Notes to or Update Notes in Anki Database
        :param evernote_notes:
        :param update:
        :param log_update_if_unchanged:
        :type evernote_notes: list[EvernoteNotePrototype.EvernoteNotePrototype]
        :type update: bool
        :return: Count of notes successfully added or updated
        """
        new_nids = []
        action_str_base = ['Add', 'Update'][update]
        action_str = ['Adding', 'Updating'][update]
        action_preposition = ['To', 'In'][update]
        info = stopwatch.ActionInfo(action_str + ' Of',
                                    'Evernote Notes',
                                    action_preposition + ' Anki',
                                    report_if_empty=False)
        tmr = stopwatch.Timer(evernote_notes,
                              10,
                              info=info,
                              label='Add\\Anki-%sEvernoteNotes' %
                              action_str_base)

        for ankiNote in evernote_notes:
            try:
                title = ankiNote.FullTitle
                content = decode(ankiNote.Content)
                anki_field_info = {
                    FIELDS.TITLE: title,
                    FIELDS.CONTENT: content,
                    FIELDS.EVERNOTE_GUID:
                    FIELDS.EVERNOTE_GUID_PREFIX + ankiNote.Guid,
                    FIELDS.UPDATE_SEQUENCE_NUM:
                    str(ankiNote.UpdateSequenceNum),
                    FIELDS.SEE_ALSO: u''
                }
            except Exception:
                log_error("Unable to set field info for: Note '%s': '%s'" %
                          (ankiNote.FullTitle, ankiNote.Guid))
                log_dump(ankiNote.Content, " NOTE CONTENTS ")
                # log_dump(encode(ankiNote.Content), " NOTE CONTENTS ")
                raise
            tmr.step(title)
            baseNote = None
            if update:
                baseNote = self.get_anki_note_from_evernote_guid(ankiNote.Guid)
                if not baseNote:
                    log_error(
                        'Updating note %s: COULD NOT FIND BASE NOTE FOR ANKI NOTE ID'
                        % ankiNote.Guid)
                    tmr.reportStatus(EvernoteAPIStatus.MissingDataError)
                    continue
            if ankiNote.Tags is None:
                log_error("Could note find tags object for note %s: %s. " %
                          (ankiNote.Guid, ankiNote.FullTitle))
                tmr.reportStatus(EvernoteAPIStatus.MissingDataError)
                continue
            anki_note_prototype = AnkiNotePrototype(
                self,
                anki_field_info,
                ankiNote.TagNames,
                baseNote,
                notebookGuid=ankiNote.NotebookGuid,
                count=tmr.count,
                count_update=tmr.counts.updated.completed.val,
                max_count=tmr.max)
            anki_note_prototype._log_update_if_unchanged_ = log_update_if_unchanged
            nid = tmr.autoStep(
                anki_note_prototype.update_note() if update else
                anki_note_prototype.add_note(), ankiNote.FullTitle, update)
            if tmr.status.IsSuccess and not update:
                new_nids.append([nid, ankiNote.Guid])
            elif tmr.status.IsError:
                log(
                    "ANKI ERROR WHILE %s EVERNOTE NOTES: " %
                    action_str.upper() + str(tmr.status), tmr.label + '-Error')
        tmr.Report()
        if new_nids:
            ankDB().executemany("UPDATE {n} SET nid = ? WHERE guid = ?",
                                new_nids)
        return tmr.counts.success
示例#20
0
def clean_title(title):
    title = unescape_text(decode(title))
    title = re.sub(r'( |\xa0)+', ' ', decode(title))
    return title
示例#21
0
    def insert_toc_and_outline_contents_into_notes(self):
        linked_notes_fields = {}
        db = ankDB(TABLES.SEE_ALSO)
        source_guids = db.list("SELECT DISTINCT s.source_evernote_guid FROM {s} s, {n} n WHERE (s.is_toc = 1 OR "
                               "s.is_outline = 1) AND s.source_evernote_guid = n.guid ORDER BY n.title ASC")
        info = stopwatch.ActionInfo('Insertion of', 'TOC/Outline Contents', 'Into Target Anki Notes')
        log = Logger('See Also\\8-insert_toc_contents\\', rm_path=True, timestamp=False)
        tmr = stopwatch.Timer(source_guids, 25, info=info, label=log.base_path)
        tmr.info.BannerHeader('error')
        for source_guid in source_guids:
            note = self.get_anki_note_from_evernote_guid(source_guid)
            if not note:
                tmr.reportStatus(EvernoteAPIStatus.NotFoundError)
                log.error("Could not find note for %s for %s" % (note.guid, tmr.base_name))
                continue
            # if TAGS.TOC in note.tags:
                # tmr.reportSkipped()
                # continue
            for fld in note._model['flds']:
                if FIELDS.TITLE in fld.get('name'):
                    note_title = note.fields[fld.get('ord')]
                    continue
            if not note_title:
                tmr.reportStatus(EvernoteAPIStatus.NotFoundError)
                log.error("Could not find note title for %s for %s" % (note.guid, tmr.base_name))
                continue
            tmr.step(note_title)
            note_toc = ""
            note_outline = ""
            toc_header = ""
            outline_header = ""
            toc_count = 0
            outline_count = 0
            toc_and_outline_links = db.execute("source_evernote_guid = '%s' AND (is_toc = 1 OR is_outline = 1) "
                                               "ORDER BY number ASC" % source_guid,
                                               columns='target_evernote_guid, is_toc, is_outline')
            for target_evernote_guid, is_toc, is_outline in toc_and_outline_links:
                if target_evernote_guid in linked_notes_fields:
                    linked_note_contents = linked_notes_fields[target_evernote_guid][FIELDS.CONTENT]
                    linked_note_title = linked_notes_fields[target_evernote_guid][FIELDS.TITLE]
                else:
                    linked_note = self.get_anki_note_from_evernote_guid(target_evernote_guid)
                    if not linked_note:
                        continue
                    linked_note_contents = u""
                    for fld in linked_note._model['flds']:
                        if FIELDS.CONTENT in fld.get('name'):
                            linked_note_contents = linked_note.fields[fld.get('ord')]
                        elif FIELDS.TITLE in fld.get('name'):
                            linked_note_title = linked_note.fields[fld.get('ord')]
                    if linked_note_contents:
                        linked_notes_fields[target_evernote_guid] = {
                            FIELDS.TITLE:   linked_note_title,
                            FIELDS.CONTENT: linked_note_contents
                        }
                if linked_note_contents:
                    linked_note_contents = decode(linked_note_contents)
                    if is_toc:
                        toc_count += 1
                        if toc_count is 1:
                            toc_header = "<span class='header'>TABLE OF CONTENTS</span>: 1. <span class='header'>%s</span>" % linked_note_title
                        else:
                            note_toc += "<br><hr>"; toc_header += "<span class='See_Also'> | </span> %d. <span class='header'>%s</span>" % (
                            toc_count, linked_note_title)
                        note_toc += linked_note_contents
                    else:
                        outline_count += 1
                        if outline_count is 1:
                            outline_header = "<span class='header'>OUTLINE</span>: 1. <span class='header'>%s</span>" % linked_note_title
                        else:
                            note_outline += "<BR><HR>";  outline_header += "<span class='See_Also'> | </span> %d. <span class='header'>%s</span>" % (
                            outline_count, linked_note_title)
                        note_outline += linked_note_contents
            if outline_count + toc_count is 0:
                tmr.reportError(EvernoteAPIStatus.MissingDataError)
                log.error(" No Valid TOCs or Outlines Found: %s" % note_title)
                continue
            tmr.reportSuccess()

            def makestr(title, count):
                return '' if not count else 'One %s ' % title if count is 1 else '%s %ss' % (str(count).center(3), title)

            toc_str = makestr('TOC', toc_count).rjust(8) #if toc_count else ''
            outline_str = makestr('Outline', outline_count).ljust(12) #if outline_count else ''
            toc_str += ' &  ' if toc_count and outline_count else '    '

            log.go(" [%4d/%4d]  +   %s   for Note %s:   %s" % (
                            tmr.count, tmr.max, toc_str + outline_str, source_guid.split('-')[0], note_title))

            if outline_count > 1:
                note_outline = "<span class='Outline'>%s</span><BR><BR>" % outline_header + note_outline
            if toc_count > 1:
                note_toc = "<span class='TOC'>%s</span><BR><BR>" % toc_header + note_toc
            for fld in note._model['flds']:
                if FIELDS.TOC in fld.get('name'):
                    note.fields[fld.get('ord')] = note_toc
                elif FIELDS.OUTLINE in fld.get('name'):
                    note.fields[fld.get('ord')] = note_outline
            # log.go(' '*16 + "> Flushing Note \r\n")
            note.flush(intTime())
        tmr.Report()