Replace indexing into the os.environ dictionary with a .get() call (and a default) to avoid an IndexError
92 lines
2.5 KiB
Python
Executable file
92 lines
2.5 KiB
Python
Executable file
#!/usr/bin/env python3.12
|
|
# Eryn Wells <eryn@erynwells.me>
|
|
|
|
import argparse
|
|
import datetime
|
|
import os.path
|
|
import subprocess
|
|
from typing import Optional
|
|
from website.dates import next_sunday_noon
|
|
from website.paths import content_path
|
|
|
|
|
|
def weeknotes_path(*, week_number: Optional[int] = None):
|
|
if week_number:
|
|
year = datetime.datetime.now().year
|
|
week_number_str = str(week_number)
|
|
else:
|
|
next_sunday = next_sunday_noon()
|
|
year = next_sunday.year
|
|
week_number_str = next_sunday.strftime('%V')
|
|
|
|
weeknotes_filename = f'weeknotes-{year}w{week_number_str}.md'
|
|
return os.path.join(content_path(), 'blog', str(year), weeknotes_filename)
|
|
|
|
|
|
def parse_args(argv, *a, **kw):
|
|
parser = argparse.ArgumentParser(*a, **kw)
|
|
commands = parser.add_subparsers(title='Commands', required=True)
|
|
|
|
convert_command = commands.add_parser(
|
|
'convert',
|
|
help='Convert a post from a single file to a page bundle and vice versa'
|
|
)
|
|
convert_command.set_defaults(handler=handle_convert_command)
|
|
|
|
edit_command = commands.add_parser(
|
|
'edit',
|
|
help="Edit the current week's weeknotes post"
|
|
)
|
|
edit_command.add_argument(
|
|
'--editor', '-e',
|
|
default=os.environ.get('EDITOR', 'nvim'),
|
|
)
|
|
edit_command.add_argument('--week', '-w')
|
|
edit_command.set_defaults(handler=handle_edit_command)
|
|
|
|
show_command = commands.add_parser(
|
|
'show',
|
|
aliases=['print'],
|
|
help="Print a path to the current week's weeknotes post",
|
|
)
|
|
show_command.add_argument('--week', '-w')
|
|
show_command.add_argument('--date', action='store_true')
|
|
show_command.set_defaults(handler=handle_show_command)
|
|
|
|
args = parser.parse_args(argv)
|
|
return args
|
|
|
|
|
|
def handle_convert_command(args):
|
|
raise NotImplementedError()
|
|
|
|
|
|
def handle_edit_command(args):
|
|
path = weeknotes_path(week_number=args.week)
|
|
subprocess.run(
|
|
f'{args.editor} "{path}"',
|
|
stdin=sys.stdin,
|
|
stdout=sys.stdout,
|
|
stderr=sys.stderr,
|
|
env=os.environ,
|
|
shell=True,
|
|
)
|
|
|
|
|
|
def handle_show_command(args):
|
|
if args.date:
|
|
if args.week:
|
|
raise NotImplementedError('Cannot print date with specified week number')
|
|
print(next_sunday_noon().isoformat())
|
|
else:
|
|
print(weeknotes_path(week_number=args.week))
|
|
|
|
|
|
def main(argv):
|
|
args = parse_args(argv[1:], prog=argv[0])
|
|
args.handler(args)
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
result = main(sys.argv)
|
|
sys.exit(0 if not result else result)
|