TI-52: The 'refresh' scripts overwrites previous years data

- Thanks to m8r.
This commit is contained in:
Paul Beckingham 2016-11-06 08:20:47 -05:00
parent ccfa91e572
commit 6ccbf141de
4 changed files with 43 additions and 35 deletions

View file

@ -43,3 +43,4 @@ suggestions:
Georg Sauthoff
Josh Proehl
Mattia Rizzolo
m8r

View file

@ -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

View file

@ -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

View file

@ -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: