88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
#!/usr/bin/env python3.12
|
|
# Eryn Wells <eryn@erynwells.me>
|
|
|
|
import argparse
|
|
import datetime
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from typing import Optional
|
|
from . import blog
|
|
from .dates import next_sunday_noon
|
|
from .scripting import Command
|
|
|
|
|
|
class WeeknotesCommand(Command):
|
|
@staticmethod
|
|
def weeknotes_filename(year: str, week: str) -> str:
|
|
return f'weeknotes-{year}w{week}.md'
|
|
|
|
@staticmethod
|
|
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')
|
|
|
|
return blog.post_path(WeeknotesCommand.weeknotes_filename(
|
|
str(year),
|
|
week_number_str
|
|
))
|
|
|
|
@property
|
|
def help(self) -> str:
|
|
return 'Work with weeknotes posts'
|
|
|
|
def add_arguments(self, parser: argparse.ArgumentParser):
|
|
commands = parser.add_subparsers(title='Weeknotes', 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=self.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=self.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=self.handle_show_command)
|
|
|
|
def handle_convert_command(self, _: argparse.Namespace):
|
|
raise NotImplementedError()
|
|
|
|
def handle_edit_command(self, args: argparse.Namespace):
|
|
path = WeeknotesCommand.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(self, args: argparse.Namespace):
|
|
if args.date:
|
|
if args.week:
|
|
raise NotImplementedError('Cannot print date with specified week number')
|
|
print(next_sunday_noon().isoformat())
|
|
else:
|
|
print(WeeknotesCommand.weeknotes_path(week_number=args.week))
|