From 6ccbf141de377be336b5fff77e7ac71bf09ae749 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 6 Nov 2016 08:20:47 -0500 Subject: [PATCH] TI-52: The 'refresh' scripts overwrites previous years data - Thanks to m8r. --- AUTHORS | 1 + ChangeLog | 2 ++ doc/holidays/README | 9 ++++-- doc/holidays/refresh | 66 ++++++++++++++++++++++---------------------- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/AUTHORS b/AUTHORS index 7cd5610b..6a356e6b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -43,3 +43,4 @@ suggestions: Georg Sauthoff Josh Proehl Mattia Rizzolo + m8r diff --git a/ChangeLog b/ChangeLog index 3de66447..f6374392 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,8 @@ (thanks to Georg Sauthoff). - TI-51 in the taskwarrior hook, deleting a task doesn't stop the watch (thanks to Mattia Rizzolo). +- TI-52 The 'refresh' scripts overwrites previous years data + (thanks to m8r). - Fixed Python 3 support of the holdiay/refresh script (thanks to Jelle van der Waa). - Added missing man page link diff --git a/doc/holidays/README b/doc/holidays/README index f7fdcc1e..5eafacfa 100644 --- a/doc/holidays/README +++ b/doc/holidays/README @@ -14,6 +14,11 @@ If you need a specific locale region, do this: $ ./refresh --locale de-CH --region Bern -If the locale is not supported by holidata.net, or there is no data (yet) for -the locale for this year and next year, you will see an error. +By default, the current and next year are updated. If the locale is not +supported by holidata.net, or there is no data (yet) for the locale for this +year and next year, you will see an error. + +To specify a set of years to update, do this: + + $ ./refresh --locale en-US --year 2015 2016 2017 diff --git a/doc/holidays/refresh b/doc/holidays/refresh index e08fde58..aa2d5fc7 100755 --- a/doc/holidays/refresh +++ b/doc/holidays/refresh @@ -47,47 +47,46 @@ def holidata(locale, year): return "http://holidata.net/%s/%d.json" % (locale, year) -def update_locales(locales, regions): - update_single_year(locales, regions, datetime.datetime.now().year) - update_single_year(locales, regions, datetime.datetime.now().year + 1) +def update_locales(locales, regions, years): + if years == []: + years = [datetime.datetime.now().year, datetime.datetime.now().year + 1] - -def update_single_year(locales, regions, year): for locale in locales: - print(holidata(locale, year)) - try: - url = holidata(locale, year) - lines = urlopen(url).read().decode('utf-8') + with open("holidays.%s" % locale, "w") as fh: + fh.write('# Holiday data provided by Holidata.net\n') + fh.write('# Generated ' + time.strftime('%Y-%m-%dT%H:%M:%S') + '\n\n') + fh.write('define holidays:\n') + fh.write(' ' + locale + ':\n') - with open("holidays.%s" % locale, "w") as fh: - fh.write('# Holiday data provided by Holidata.net\n') - fh.write('# ' + url + '\n') - fh.write('# Generated ' + time.strftime('%Y-%m-%dT%H:%M:%S') + '\n\n') - fh.write('define holidays:\n') - fh.write(' ' + locale + ':\n') + for year in years: + 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 j['region'] == '' or regions is None or len(regions) == 0 or j['region'] in regions: - day = j['date'].replace("-", "_") - desc = j['description'] - data = ' %s = %s\n' % (day, desc) - if sys.version_info >= (3,0): - fh.write(data) - else: - fh.write(data.encode('utf-8')) + for line in lines.split('\n'): + if line: + j = json.loads(line) + if j['region'] == '' or regions is None or len(regions) == 0 or j['region'] in regions: + day = j['date'].replace("-", "_") + desc = j['description'] + data = ' %s = %s\n' % (day, desc) + if sys.version_info >= (3,0): + fh.write(data) + else: + fh.write(data.encode('utf-8')) + fh.write('\n') - except HTTPError as e: - if e.code == 404: - print("Holidata.net does not have data for %s, for %d." % (locale, year)) - else: - print(e.code, e.read()) + except HTTPError as e: + if e.code == 404: + print("Holidata.net does not have data for %s, for %d." % (locale, year)) + else: + print(e.code, e.read()) def main(args): if args.locale: - update_locales(args.locale, args.region) + update_locales(args.locale, args.region, args.year) else: # Enumerate all holiday files in the current directory. locales = [] @@ -98,7 +97,7 @@ def main(args): # Extract the locale name. locales.append(result.group(1)) - update_locales(locales, args.region) + update_locales(locales, args.region, args.year) if __name__ == "__main__": @@ -106,6 +105,7 @@ if __name__ == "__main__": 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.') + parser.add_argument('--year', nargs='+', help='Specific year to fetch.', type=int, default=[]) args = parser.parse_args() try: