Replace the next_sunday.py script with a more comprehensive weeknotes script
This commit is contained in:
		
							parent
							
								
									1c897e4518
								
							
						
					
					
						commit
						bee8800070
					
				
					 3 changed files with 122 additions and 36 deletions
				
			
		| 
						 | 
				
			
			@ -1,32 +0,0 @@
 | 
			
		|||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
import datetime
 | 
			
		||||
import time
 | 
			
		||||
import zoneinfo
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    today = datetime.date.today()
 | 
			
		||||
 | 
			
		||||
    current_weekday = today.weekday()
 | 
			
		||||
    if current_weekday == 6:
 | 
			
		||||
        days_to_next_sunday = 6
 | 
			
		||||
    else:
 | 
			
		||||
        days_to_next_sunday = 6 - current_weekday
 | 
			
		||||
 | 
			
		||||
    delta = datetime.timedelta(days=days_to_next_sunday)
 | 
			
		||||
 | 
			
		||||
    noon = datetime.time(hour=12)
 | 
			
		||||
 | 
			
		||||
    timezone = datetime.datetime.now().astimezone().tzinfo
 | 
			
		||||
    next_sunday_noon = datetime.datetime.combine(
 | 
			
		||||
        today + delta,
 | 
			
		||||
        noon,
 | 
			
		||||
        tzinfo=timezone
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    print(next_sunday_noon.isoformat())
 | 
			
		||||
 | 
			
		||||
    return 0
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    main()
 | 
			
		||||
							
								
								
									
										118
									
								
								scripts/weeknotes
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										118
									
								
								scripts/weeknotes
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,118 @@
 | 
			
		|||
#!/usr/bin/env python3.12
 | 
			
		||||
# Eryn Wells <eryn@erynwells.me>
 | 
			
		||||
 | 
			
		||||
import argparse
 | 
			
		||||
import datetime
 | 
			
		||||
import os.path
 | 
			
		||||
import subprocess
 | 
			
		||||
from typing import Optional
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def content_path() -> str:
 | 
			
		||||
    from os.path import abspath, dirname, join
 | 
			
		||||
    return abspath(join(dirname(__file__), '..', 'content'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def next_sunday_noon() -> datetime.datetime:
 | 
			
		||||
    today = datetime.date.today()
 | 
			
		||||
 | 
			
		||||
    current_weekday = today.weekday()
 | 
			
		||||
    if current_weekday == 6:
 | 
			
		||||
        days_to_next_sunday = 6
 | 
			
		||||
    else:
 | 
			
		||||
        days_to_next_sunday = 6 - current_weekday
 | 
			
		||||
 | 
			
		||||
    delta = datetime.timedelta(days=days_to_next_sunday)
 | 
			
		||||
 | 
			
		||||
    noon = datetime.time(hour=12)
 | 
			
		||||
 | 
			
		||||
    timezone = datetime.datetime.now().astimezone().tzinfo
 | 
			
		||||
    next_sunday_noon = datetime.datetime.combine(
 | 
			
		||||
        today + delta,
 | 
			
		||||
        noon,
 | 
			
		||||
        tzinfo=timezone
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    return next_sunday_noon
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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['EDITOR'] or '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)
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue