gronk/gronk_add_uuid.py
2024-01-02 18:23:25 +00:00

58 lines
1.3 KiB
Python

#!/usr/bin/env python3
import pathlib
import sys
import uuid
import editfrontmatter
import frontmatter
def get_args():
""" Get command line arguments """
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=pathlib.Path)
parser.add_argument('-w',
'--write',
action='store_true',
help='write to file instead of stdout')
return parser.parse_args()
def main(args):
""" Entry point for script """
template_str = "\n".join([
"author: {{ author }}"
"date: {{ date }}"
"title: {{ title }}"
"tags: {{ tags }}"
"uuid: {{ uuid }}"
])
with open(args.filename) as fp:
fm_pre = frontmatter.load(fp)
processor = editfrontmatter.EditFrontMatter(file_path=args.filename,
template_str=template_str)
fm_data = fm_pre.metadata
if 'uuid' not in fm_data.keys():
fm_data['uuid'] = str(uuid.uuid4())
processor.run(fm_data)
if args.write:
with open(args.filename, 'w') as fp:
fp.write(processor.dumpFileData())
else:
print(processor.dumpFileData())
return 0
if __name__ == '__main__':
try:
sys.exit(main(get_args()))
except KeyboardInterrupt:
sys.exit(0)