Meta: Port several things common to various Python scripts to a new website module
This commit is contained in:
parent
a30903c8cf
commit
9ce6362402
6 changed files with 70 additions and 34 deletions
|
@ -9,20 +9,16 @@ import argparse
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from PIL.ExifTags import TAGS
|
from PIL.ExifTags import TAGS
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from website.metadata import slugify
|
||||||
|
|
||||||
|
|
||||||
PHOTOS_CONTENT_DIR = 'content/photos'
|
PHOTOS_CONTENT_DIR = 'content/photos'
|
||||||
|
|
||||||
def slugify(s):
|
|
||||||
s = s.strip().lower()
|
|
||||||
s = re.sub(r'\s+', '-', s)
|
|
||||||
s = re.sub(r'[‘’“”"\'()]', '', s)
|
|
||||||
return s
|
|
||||||
|
|
||||||
def parse_args(argv, *a, **kw):
|
def parse_args(argv, *a, **kw):
|
||||||
parser = argparse.ArgumentParser(*a, **kw)
|
parser = argparse.ArgumentParser(*a, **kw)
|
||||||
|
@ -34,6 +30,7 @@ def parse_args(argv, *a, **kw):
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
args = parse_args(argv[1:], prog=argv[0])
|
args = parse_args(argv[1:], prog=argv[0])
|
||||||
|
|
||||||
|
@ -145,6 +142,7 @@ def main(argv):
|
||||||
print(f'Failed to copy {photo}', file=sys.stderr)
|
print(f'Failed to copy {photo}', file=sys.stderr)
|
||||||
return -2
|
return -2
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
result = main(sys.argv)
|
result = main(sys.argv)
|
||||||
|
|
0
scripts/website/__init__.py
Normal file
0
scripts/website/__init__.py
Normal file
33
scripts/website/dates.py
Normal file
33
scripts/website/dates.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
def next_sunday_noon(from_date: Optional[datetime.date] = None) -> datetime.datetime:
|
||||||
|
'''
|
||||||
|
Return a `datetime` for noon on the upcoming Sunday. Use today if no
|
||||||
|
`from_date` is given.
|
||||||
|
'''
|
||||||
|
|
||||||
|
today = from_date or 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
|
12
scripts/website/metadata.py
Normal file
12
scripts/website/metadata.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def slugify(s: str) -> str:
|
||||||
|
'''Process a string into something suitable to be a page slug.'''
|
||||||
|
s = s.strip().lower()
|
||||||
|
s = re.sub(r'\s+', '-', s)
|
||||||
|
s = re.sub(r'[‘’“”"\'()]', '', s)
|
||||||
|
return s
|
19
scripts/website/paths.py
Normal file
19
scripts/website/paths.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
|
import os.path as osp
|
||||||
|
|
||||||
|
|
||||||
|
def content_path() -> str:
|
||||||
|
'''Return the path to the content directory.'''
|
||||||
|
path = osp.abspath(osp.join(osp.dirname(__file__), '..', '..', 'content'))
|
||||||
|
assert osp.isdir(path)
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def blog_path() -> str:
|
||||||
|
return osp.join(content_path(), 'blog')
|
||||||
|
|
||||||
|
|
||||||
|
def photos_path() -> str:
|
||||||
|
return osp.join(content_path(), 'photos')
|
|
@ -6,34 +6,8 @@ import datetime
|
||||||
import os.path
|
import os.path
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from website.dates import next_sunday_noon
|
||||||
|
from website.paths import content_path
|
||||||
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):
|
def weeknotes_path(*, week_number: Optional[int] = None):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue