diff --git a/litdb/DB_dict.py b/litdb/DB_dict.py index 4041ed5..772ff29 100644 --- a/litdb/DB_dict.py +++ b/litdb/DB_dict.py @@ -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'] @@ -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 @@ -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 diff --git a/litdb/_version.py b/litdb/_version.py index f5fe964..c69a47c 100644 --- a/litdb/_version.py +++ b/litdb/_version.py @@ -1 +1 @@ -__version__ = "1.0" \ No newline at end of file +__version__ = "1.1" \ No newline at end of file diff --git a/litdb/litdb.py b/litdb/litdb.py index 83417a5..39002de 100644 --- a/litdb/litdb.py +++ b/litdb/litdb.py @@ -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]}")