Skip to content

Commit

Permalink
Merge pull request #10 from dag-hammarskjold-library/jb/9
Browse files Browse the repository at this point in the history
Allow previewing changes when outputting to mrk
  • Loading branch information
jbukhari authored Jul 2, 2024
2 parents b93b080 + ac30f78 commit aa86a84
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions batch_edits/scripts/batch_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def get_args():
parser.add_argument('--output', required=True, choices=['db', 'mrk'], help='')
parser.add_argument('--output_file', help='File to write output to if output is mrk')
parser.add_argument('--skip_confirm', action='store_true', help='')
parser.add_argument('--view_changes', action='store_true', help='')

return parser.parse_args()

Expand All @@ -26,10 +27,7 @@ def run(**kwargs):
sys.argv = [sys.argv[0]]

for param, arg in kwargs.items():
if isinstance(arg, bool):
sys.argv.append(f'--{param}')
else:
sys.argv.append(f'--{param}={arg}')
sys.argv.append(f'--{param}' if isinstance(arg, bool) else f'--{param}={arg}')

args = get_args()

Expand All @@ -46,7 +44,6 @@ def run(**kwargs):
OUT = sys.stdout

query = Query.from_string(args.querystring) if args.querystring else {}

bibs = BibSet.from_query(query, limit=args.limit)
edits = [f for name, f in inspect.getmembers(sys.modules[__name__], inspect.isfunction) if name[:5] == 'edit_']
i, status = 0, ''
Expand All @@ -67,6 +64,9 @@ def run(**kwargs):
bib = edit(bib)

if changes := '\n'.join([f.to_mrk() for f in Diff(before_edits, bib).a]):
if args.view_changes:
OUT.write(f'--> record id {bib.id}\nFields changed:\n{changes}\n\nRecord with changes:\n')

if args.output == 'mrk':
OUT.write(bib.to_mrk() + '\n')
elif args.output == 'db':
Expand All @@ -75,14 +75,14 @@ def run(**kwargs):
status = ('\b' * len(status)) + f'Records updated: {i}'
print(status, end='', flush=True)
else:
x = input(f'--> record id {bib.id}\nFields changed:\n{changes}\n\nRecord with changes:\n{bib.to_mrk()}\nCommit changes? (y/n): ')
x = input(f'{bib.to_mrk()}\nCommit changes? (y/n): ')

if x.lower() != 'y':
print('Changes disregarded\n')
time.sleep(1)
continue

bib.commit(user='batch edit 1')
bib.commit(user='batch_edit_1')
print(f'OK. Updated {bib.id}\n')
time.sleep(1)
else:
Expand Down

0 comments on commit aa86a84

Please sign in to comment.