Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to new structure of better-bibtex.sqlite #31

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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