Example #1
0
 def add_log_entry(title, content, filename=None, prefix_content=True, title_pad=16, **kw):
     names_padded = u''.join(map(lambda x: (x+':').ljust(33) + ' ', names[1:-1])) + names[-1]
     fmts = dict(levels_pad=u'\t' * level, levels=u'.'.join(map(str, levels)),
                 num_levels=len(levels), names=u': '.join(names[1:]).ljust(20),
                 names_padded=names_padded)
     fmts['levels_str'] = (fmts['levels'] + ':').ljust(6)
     if prefix_content:
         fmts['content'] = content
         content = u'{levels_pad}{levels_str} {content}'
     if isinstance(lst_items, Tag) and lst_items.name in list_tag_names:
         fmts['list_name'] = list_tag_names[lst_items.name]
     content = fmt(content, 0, fmts)
     if title:
         title = (fmt(title, 0, fmts) + u': ').ljust(title_pad)
     l.go(title + content, filename, **kw)
Example #2
0
 def extract_links_from_toc(self):
     db = ankDB(TABLES.SEE_ALSO)
     db.setrowfactory()
     toc_entries = db.all("SELECT * FROM {n} WHERE tagNames LIKE '{t_toc}' ORDER BY title ASC")
     db.execute("DELETE FROM {t} WHERE from_toc = 1")
     log = Logger('See Also\\4-extract_links_from_toc\\', timestamp=False, crosspost_to_default=False, rm_path=True)
     tmr = stopwatch.Timer(toc_entries, 20, infoStr='Extracting Links', label=log.base_path)
     tmr.info.BannerHeader('error')
     toc_guids = []
     for toc_entry in toc_entries:
         toc_evernote_guid, toc_link_title = toc_entry['guid'], toc_entry['title']
         toc_guids.append("'%s'" % toc_evernote_guid)
         # toc_link_html = generate_evernote_span(toc_link_title, 'Links', 'TOC')
         enLinks = find_evernote_links(toc_entry['content'])
         tmr.increment(toc_link_title)
         for enLink in enLinks:
             target_evernote_guid = enLink.Guid
             if not check_evernote_guid_is_valid(target_evernote_guid):
                 log.go("Invalid Target GUID for %-70s %s" % (toc_link_title + ':', target_evernote_guid), 'error')
                 continue
             base = {
                 'child_guid': target_evernote_guid, 'uid': enLink.Uid,
                 'shard':    enLink.Shard, 'toc_guid': toc_evernote_guid, 'l1': 'source', 'l2': 'source',
                 'from_toc': 0, 'is_toc': 0
             }
             query_count = "select COUNT(*) from {t} WHERE source_evernote_guid = '{%s_guid}'"
             toc = {
                 'num':      1 + db.scalar(fmt(query_count % 'toc', base)),
                 'html':     enLink.HTML.replace(u'\'', u'\'\''),
                 'title':    enLink.FullTitle.replace(u'\'', u'\'\''),
                 'l1':       'target',
                 'from_toc': 1
             }
             # child = {1 + db.scalar(fmt(query_count % 'child', base)),
             # 'html': toc_link_html.replace(u'\'', u'\'\''),
             # 'title': toc_link_title.replace(u'\'', u'\'\''),
             # 'l2': 'target',
             # 'is_toc': 1
             # }
             query = (u"INSERT OR REPLACE INTO `{t}`(`{l1}_evernote_guid`, `number`, `uid`, `shard`, "
                      u"`{l2}_evernote_guid`, `html`, `title`, `from_toc`, `is_toc`) "
                      u"VALUES('{child_guid}', {num}, {uid}, '{shard}', "
                      u"'{toc_guid}', '{html}', '{title}', {from_toc}, {is_toc})")
             query_toc = fmt(query, base, toc)
             db.execute(query_toc)
         log.go("\t\t - Added %2d child link(s) from TOC %s" % (len(enLinks), encode(toc_link_title)))
     db.update("is_toc = 1", where="target_evernote_guid IN (%s)" % ', '.join(toc_guids))
     db.commit()
Example #3
0
def execute_sqlite_query(sql, data, in_query=False, **kwargs):
    queries = generate_sqlite_in_predicate(data, **kwargs) if in_query else generate_sqlite_predicate(data, **kwargs)
    results = []
    db = ankDB()
    for query, data in queries:
        sql = fmt(sql, pred=query)
        result = db.list(sql, *data)
        log_sql('FROM execute_sqlite_query ' + sql,
                ['Data [%d]: ' % len(data), data,result[:3]])
        results += result
    return results
Example #4
0
 def _fmt_query_(self, sql, **kw):
     formats = dict(table=self.table, where='1', columns='*')
     override = dict(n=TABLES.EVERNOTE.NOTES, s=TABLES.SEE_ALSO, a=TABLES.TOC_AUTO,
                     nv=TABLES.NOTE_VALIDATION_QUEUE, nb=TABLES.EVERNOTE.NOTEBOOKS, tt=TABLES.EVERNOTE.TAGS,
                     t_toc='%%,%s,%%' % TAGS.TOC, t_tauto='%%,%s,%%' % TAGS.TOC_AUTO,
                     t_out='%%,%s,%%' % TAGS.OUTLINE, anki_guid='{guid_prefix}{guid}%',
                     guid_prefix=FIELDS.EVERNOTE_GUID_PREFIX)
     keys = formats.keys()
     formats.update(kw)
     formats['t'] = formats['table']
     formats.update(override)
     sql = fmt(sql, formats)
     for key in keys:
         if key in kw:
             del kw[key]
     return sql
Example #5
0
 def extract_links_from_toc(self):
     db = ankDB(TABLES.SEE_ALSO)
     db.setrowfactory()
     toc_entries = db.all(
         "SELECT * FROM {n} WHERE tagNames LIKE '{t_toc}' ORDER BY title ASC"
     )
     db.execute("DELETE FROM {t} WHERE from_toc = 1")
     log = Logger('See Also\\4-extract_links_from_toc\\',
                  timestamp=False,
                  crosspost_to_default=False,
                  rm_path=True)
     tmr = stopwatch.Timer(toc_entries,
                           20,
                           infoStr='Extracting Links',
                           label=log.base_path)
     tmr.info.BannerHeader('error')
     toc_guids = []
     for toc_entry in toc_entries:
         toc_evernote_guid, toc_link_title = toc_entry['guid'], toc_entry[
             'title']
         toc_guids.append("'%s'" % toc_evernote_guid)
         # toc_link_html = generate_evernote_span(toc_link_title, 'Links', 'TOC')
         enLinks = find_evernote_links(toc_entry['content'])
         tmr.increment(toc_link_title)
         for enLink in enLinks:
             target_evernote_guid = enLink.Guid
             if not check_evernote_guid_is_valid(target_evernote_guid):
                 log.go(
                     "Invalid Target GUID for %-70s %s" %
                     (toc_link_title + ':', target_evernote_guid), 'error')
                 continue
             base = {
                 'child_guid': target_evernote_guid,
                 'uid': enLink.Uid,
                 'shard': enLink.Shard,
                 'toc_guid': toc_evernote_guid,
                 'l1': 'source',
                 'l2': 'source',
                 'from_toc': 0,
                 'is_toc': 0
             }
             query_count = "select COUNT(*) from {t} WHERE source_evernote_guid = '{%s_guid}'"
             toc = {
                 'num': 1 + db.scalar(fmt(query_count % 'toc', base)),
                 'html': enLink.HTML.replace(u'\'', u'\'\''),
                 'title': enLink.FullTitle.replace(u'\'', u'\'\''),
                 'l1': 'target',
                 'from_toc': 1
             }
             # child = {1 + db.scalar(fmt(query_count % 'child', base)),
             # 'html': toc_link_html.replace(u'\'', u'\'\''),
             # 'title': toc_link_title.replace(u'\'', u'\'\''),
             # 'l2': 'target',
             # 'is_toc': 1
             # }
             query = (
                 u"INSERT OR REPLACE INTO `{t}`(`{l1}_evernote_guid`, `number`, `uid`, `shard`, "
                 u"`{l2}_evernote_guid`, `html`, `title`, `from_toc`, `is_toc`) "
                 u"VALUES('{child_guid}', {num}, {uid}, '{shard}', "
                 u"'{toc_guid}', '{html}', '{title}', {from_toc}, {is_toc})"
             )
             query_toc = fmt(query, base, toc)
             db.execute(query_toc)
         log.go("\t\t - Added %2d child link(s) from TOC %s" %
                (len(enLinks), encode(toc_link_title)))
     db.update("is_toc = 1",
               where="target_evernote_guid IN (%s)" % ', '.join(toc_guids))
     db.commit()