Skip to content

Commit

Permalink
Merge pull request #31 from ailintom/ailintom-patch-1
Browse files Browse the repository at this point in the history
Adapt to new structure of better-bibtex.sqlite
  • Loading branch information
plandes authored Jan 2, 2024
2 parents 04acb2a + 3d3e4d9 commit f40c08e
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/python/zensols/zotsite/betterbib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,20 @@ class BetterBibtexMapper(object):
"""
def __init__(self, lib: Library):
self.lib = lib

@property
def data(self) -> Dict[str, Any]:
path = self.lib.data_dir / 'better-bibtex.sqlite'
lib_id = lib.library_id
path = lib.data_dir / 'better-bibtex.sqlite'
logger.info(f'reading bibtex DB at {path}')
conn = sqlite3.connect(':memory:')
conn.execute('ATTACH DATABASE ? AS betterbibtex', (str(path),))
try:
rows = tuple(conn.execute("""select * from betterbibtex.`better-bibtex`"""))
assert len(rows) == 3
rows = tuple(filter(lambda r: r[0] == 'better-bibtex.citekey', rows))
assert len(rows) == 1
jstr = rows[0][1]
return json.loads(jstr)
rows = tuple(conn.execute("""select itemID, citationKey from betterbibtex.`citationkey` where libraryID = ?""", (int(lib_id),)))
self.data = rows
finally:
conn.close()

@property
@persisted('_mapping')
def mapping(self) -> Dict[str, Any]:
lib_id = self.lib.library_id
data = self.data['data']
data = filter(lambda x: x['libraryID'] == lib_id, data)
return {x['itemID']: x['citekey'] for x in data}

return {x[0]: x[1] for x in self.data}

class BetterBibtexVisitor(Visitor):
"""Use the ``BetterBibtexMapper`` to change the keys in mapped items to the
Expand Down

0 comments on commit f40c08e

Please sign in to comment.