gronk/gronk_add_uuid.py

58 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import pathlib
import sys
import uuid
2023-09-20 21:17:06 +00:00
import editfrontmatter
import frontmatter
2024-01-02 03:51:45 +00:00
def get_args():
""" Get command line arguments """
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=pathlib.Path)
2024-01-02 03:51:45 +00:00
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 """
2024-01-02 03:51:45 +00:00
template_str = "\n".join([
2023-09-20 21:17:06 +00:00
"author: {{ author }}"
"date: {{ date }}"
"title: {{ title }}"
"tags: {{ tags }}"
"uuid: {{ uuid }}"
2024-01-02 03:51:45 +00:00
])
with open(args.filename) as fp:
fm_pre = frontmatter.load(fp)
2024-01-02 03:51:45 +00:00
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)