mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Refactor holidays/refresh script
- Use double quotes only - Optimize imports - Extract function gather_locales - Update help text Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
This commit is contained in:
parent
40e6c4adaf
commit
fd9ee0f780
2 changed files with 71 additions and 47 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
###############################################################################
|
||||
#
|
||||
# Copyright 2016, 2018 - 2021, Thomas Lauf, Paul Beckingham, Federico Hernandez.
|
||||
# Copyright 2016, 2018 - 2023, Gothenburg Bit Factory
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -26,96 +26,119 @@
|
|||
#
|
||||
###############################################################################
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from textwrap import dedent
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import urlopen
|
||||
|
||||
import argparse
|
||||
|
||||
def gather_locale_files(path):
|
||||
"""Enumerate all holiday files in the current directory."""
|
||||
|
||||
locale_file_map = {}
|
||||
re_holiday_file = re.compile(r"/holidays.([a-z]{2}-[A-Z]{2})$")
|
||||
|
||||
for file in enumerate(path):
|
||||
result = re_holiday_file.search(file)
|
||||
if result:
|
||||
# Extract the locale name.
|
||||
locale_file_map[result.group(1)] = file
|
||||
|
||||
return locale_file_map
|
||||
|
||||
|
||||
def enumerate(path):
|
||||
if not os.path.exists(path):
|
||||
raise Exception("Directory '{}' does not exist".format(path))
|
||||
raise Exception(f"Directory '{path}' does not exist")
|
||||
|
||||
found = []
|
||||
|
||||
for path, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=False):
|
||||
found.extend([os.path.join(path, x) for x in files])
|
||||
|
||||
return found
|
||||
|
||||
|
||||
def holidata(locale, year):
|
||||
return "https://holidata.net/{}/{}.json".format(locale, year)
|
||||
def create_locale_files(path, locales):
|
||||
locale_file_map = {}
|
||||
|
||||
for locale in locales:
|
||||
locale_file_map[locale] = os.path.join(path, f"holidays.{locale}")
|
||||
|
||||
return locale_file_map
|
||||
|
||||
|
||||
def update_locales(locales, regions, years):
|
||||
def update_locale_files(locales, regions, years):
|
||||
now = datetime.datetime.now()
|
||||
|
||||
if not years:
|
||||
years = [now.year, now.year + 1]
|
||||
|
||||
for locale in locales:
|
||||
with open("holidays.{}".format(locale), "w") as fh:
|
||||
fh.write("# Holiday data provided by holidata.net\n")
|
||||
fh.write("# Generated {:%Y-%m-%dT%H:%M:%S}\n\n".format(now))
|
||||
fh.write("define holidays:\n")
|
||||
fh.write(" {}:\n".format(locale))
|
||||
for locale, file in locales.items():
|
||||
with open(file, "w") as fh:
|
||||
fh.write(dedent(f"""\
|
||||
# Holiday data provided by holidata.net
|
||||
# Generated {now:%Y-%m-%dT%H:%M:%S}
|
||||
|
||||
define holidays:
|
||||
{locale}:
|
||||
"""))
|
||||
|
||||
for year in years:
|
||||
holidays = dict()
|
||||
url = holidata(locale, year)
|
||||
print(url)
|
||||
try:
|
||||
lines = urlopen(url).read().decode("utf-8")
|
||||
|
||||
for line in lines.split('\n'):
|
||||
if line:
|
||||
j = json.loads(line)
|
||||
if not j['region'] or not regions or j['region'] in regions:
|
||||
day = j['date'].replace("-", "_")
|
||||
desc = j['description']
|
||||
holidays[day] = desc
|
||||
holidays = get_holidata(locale, regions, year)
|
||||
|
||||
for date, desc in holidays.items():
|
||||
fh.write(" {} = {}\n".format(date, desc))
|
||||
fh.write(f" {date} = {desc}\n")
|
||||
|
||||
fh.write('\n')
|
||||
fh.write("\n")
|
||||
|
||||
except HTTPError as e:
|
||||
if e.code == 404:
|
||||
print("holidata.net does not have data for {}, for {}.".format(locale, year))
|
||||
print(f"holidata.net does not have data for {locale}, for {year}.")
|
||||
else:
|
||||
print(e.code, e.read())
|
||||
|
||||
|
||||
def main(args):
|
||||
if args.locale:
|
||||
update_locales(args.locale, args.region, args.year)
|
||||
else:
|
||||
# Enumerate all holiday files in the current directory.
|
||||
locales = []
|
||||
re_holiday_file = re.compile(r"/holidays.([a-z]{2}-[A-Z]{2}$)")
|
||||
for file in enumerate('.'):
|
||||
result = re_holiday_file.search(file)
|
||||
if result:
|
||||
# Extract the locale name.
|
||||
locales.append(result.group(1))
|
||||
def get_holidata(locale, regions, year):
|
||||
url = f"https://holidata.net/{locale}/{year}.json"
|
||||
print(url)
|
||||
holidays = dict()
|
||||
lines = urlopen(url).read().decode("utf-8")
|
||||
|
||||
update_locales(locales, args.region, args.year)
|
||||
for line in lines.split("\n"):
|
||||
if line:
|
||||
j = json.loads(line)
|
||||
|
||||
if not j["region"] or not regions or j["region"] in regions:
|
||||
day = j["date"].replace("-", "_")
|
||||
desc = j["description"]
|
||||
holidays[day] = desc
|
||||
|
||||
return holidays
|
||||
|
||||
|
||||
def main(args):
|
||||
locale_files = create_locale_files(args.path, args.locale) if args.locale else gather_locale_files(args.path)
|
||||
update_locale_files(locale_files, args.region, args.year)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
usage = """See https://holidata.net for details of supported locales and regions."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Update holiday data files. Simply run 'refresh' to update all of them.")
|
||||
parser.add_argument('--locale', nargs='+', help='Specific locale to update.')
|
||||
parser.add_argument('--region', nargs='+', help='Specific locale region to update.', default=[])
|
||||
parser.add_argument('--year', nargs='+', help='Specific year to fetch.', type=int, default=[])
|
||||
args = parser.parse_args()
|
||||
description="Update holiday data files. Simply run 'refresh' to update all of them.",
|
||||
usage="refresh [-h] [path] [--locale LOCALE [LOCALE ...]] [--region REGION [REGION ...]] [--year YEAR [YEAR ...]]"
|
||||
)
|
||||
parser.add_argument("--locale", nargs="+", help="specify locale to update")
|
||||
parser.add_argument("--region", nargs="+", help="specify locale region to update", default=[])
|
||||
parser.add_argument("--year", nargs="+", help="specify year to fetch (defaults to current and next year)", type=int, default=[])
|
||||
parser.add_argument("path", nargs="?", help="base path to search for locales (defaults to current directory)", default=".")
|
||||
|
||||
try:
|
||||
main(args)
|
||||
main(parser.parse_args())
|
||||
except Exception as msg:
|
||||
print('Error:', msg)
|
||||
print("Error:", msg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue