-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs-add-tag.py
executable file
·54 lines (43 loc) · 1.77 KB
/
obs-add-tag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
import argparse
import logging
import sys
import obs_document
def main() -> int: # returns Unix exit value
parser = argparse.ArgumentParser(description='Add YAML frontmatter tags to Obsidian-style Markdown files')
parser.add_argument('inpath', help='Input file to add tags to')
parser.add_argument('outpath', nargs='?', default=False,
help='Output file to write to (in-place modification if not given)')
parser.add_argument('--tag', '-t', nargs='+', help='Tag values to add to the document')
parser.add_argument('--debug', help='Enable debug mode (verbose output)', action='store_true')
args = parser.parse_args()
# Set up logging
rootlogger = logging.getLogger()
logger = logging.getLogger(__name__)
log_format: str = "[%(filename)20s,%(lineno)3s:%(funcName)20s] %(message)s"
logging.basicConfig(format=log_format)
if args.debug:
rootlogger.setLevel(logging.DEBUG)
logger.debug('Debug output enabled')
else:
rootlogger.setLevel(logging.INFO)
# Input sanity checks
if not args.tag:
logger.info('No tag(s) specified, exiting')
return 0
if not args.outpath:
logger.debug('No output path specified, performing in-place modification')
args.outpath = args.inpath
obsdoc: obs_document.ObsDocument
obsdoc = obs_document.ObsDocument(args.inpath) # see obs_document.py
t: str
for t in reversed(args.tag):
obsdoc.add_tag(t)
logger.debug(f'Writing to {args.outpath}')
with open(args.outpath, 'w') as outfile:
outfile.writelines(obsdoc.lines)
logger.debug(f'Wrote {len(obsdoc.lines)} lines')
logger.debug('Exiting successfully')
return 0
if __name__ == '__main__':
sys.exit(main())