Skip to content

Commit

Permalink
More verbose output
Browse files Browse the repository at this point in the history
  • Loading branch information
scotthartley committed Apr 8, 2020
1 parent d84bd58 commit 5eb2407
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
34 changes: 31 additions & 3 deletions litdb/DB_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,27 @@ def flatten(l):
flat_list.append(item)
return flat_list

def diff_dict(d1, d2):
"""Compares two dictionaries and returns the list of keys
that differentiate them.
"""
different_keys = set()

for key in d1:
if key in d2:
if d1[key] != d2[key]:
different_keys.add(key)
else:
different_keys.add(key)
for key in d2:
if key in d1:
if d1[key] != d2[key]:
different_keys.add(key)
else:
different_keys.add(key)

return different_keys

if config:
if 'journal-blacklist' in config:
journal_blacklist = config['journal-blacklist']
Expand All @@ -253,7 +274,7 @@ def flatten(l):
strict_list.append(orcid)

additions = []
updates = []
updates = {}
for doi in new_records:
if doi not in db:
# First check to make sure that the article satisfies
Expand Down Expand Up @@ -283,9 +304,16 @@ def flatten(l):
if correct_affiliation and correct_journal:
db[doi] = DB_dict()
db[doi][DB_dict.CR_KEY] = new_records[doi][DB_dict.CR_KEY]
additions.append(db[doi])
additions.append(doi)
elif new_records[doi][DB_dict.CR_KEY] != db[doi][DB_dict.CR_KEY]:
# Update if record has changed.
changed_field_keys = diff_dict(
db[doi][DB_dict.CR_KEY],
new_records[doi][DB_dict.CR_KEY])
changes = {}
for key in changed_field_keys:
changes[key] = (db[doi][DB_dict.CR_KEY].get(key),
new_records[doi][DB_dict.CR_KEY].get(key))
db[doi][DB_dict.CR_KEY] = new_records[doi][DB_dict.CR_KEY]
updates.append(db[doi])
updates[doi] = changes
return db, additions, updates
2 changes: 1 addition & 1 deletion litdb/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0"
__version__ = "1.1"
14 changes: 8 additions & 6 deletions litdb/litdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,19 @@ def litdb():
updated_records = get_doi(dois_to_check, configuration)
db, _, updates_old = DB_dict.merge_dbs(
updated_records, db, configuration)
updates = updates_new + updates_old
updates = {**updates_old, **updates_new}

with open(args.db_file, 'w') as db_file:
print(yaml.dump(db), file=db_file)

print(f"{len(additions)} records added, {len(updates)} records updated:")
print(f"{len(additions)} records added, {len(updates)} records updated.")
if additions:
print("Additions:")
for record in additions:
print(f" {record.doi}")
for doi in additions:
print(f" {doi}")
if updates:
print("Updates:")
for record in updates:
print(f" {record.doi}")
for doi in updates:
print(f" https://dx.doi.org/{doi}")
for field in updates[doi]:
print(f" {field}: {updates[doi][field]}")

0 comments on commit 5eb2407

Please sign in to comment.