mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Optimize imports, indent with 4 spaces (PEP8)
This commit is contained in:
parent
fdd40c484a
commit
6272f1ad5d
1 changed files with 71 additions and 63 deletions
|
@ -25,91 +25,99 @@
|
|||
#
|
||||
###############################################################################
|
||||
|
||||
import os, sys, re, time, datetime, json, argparse
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
|
||||
if sys.version_info >= (3,0):
|
||||
import datetime
|
||||
|
||||
if sys.version_info >= (3, 0):
|
||||
from urllib.request import urlopen
|
||||
from urllib.error import HTTPError
|
||||
else:
|
||||
from urllib2 import urlopen, HTTPError
|
||||
|
||||
def enumerate(path):
|
||||
if not os.path.exists(path):
|
||||
raise Exception("Directory '%s' does not exist" % path)
|
||||
|
||||
found = []
|
||||
for path, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=False):
|
||||
found.extend(map(lambda x: os.path.join(path, x), files))
|
||||
return found
|
||||
def enumerate(path):
|
||||
if not os.path.exists(path):
|
||||
raise Exception("Directory '%s' does not exist" % path)
|
||||
|
||||
found = []
|
||||
for path, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=False):
|
||||
found.extend(map(lambda x: os.path.join(path, x), files))
|
||||
return found
|
||||
|
||||
|
||||
def holidata(locale, year):
|
||||
return "http://holidata.net/%s/%d.json" % (locale, year)
|
||||
return "http://holidata.net/%s/%d.json" % (locale, year)
|
||||
|
||||
|
||||
def update_locales(locales, regions, years):
|
||||
if years == []:
|
||||
years = [datetime.datetime.now().year, datetime.datetime.now().year + 1]
|
||||
if not years:
|
||||
years = [datetime.datetime.now().year, datetime.datetime.now().year + 1]
|
||||
|
||||
for locale in locales:
|
||||
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')
|
||||
for locale in locales:
|
||||
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')
|
||||
|
||||
for year in years:
|
||||
url = holidata(locale, year)
|
||||
print(url)
|
||||
try:
|
||||
lines = urlopen(url).read().decode('utf-8')
|
||||
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'))
|
||||
fh.write('\n')
|
||||
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, args.year)
|
||||
else:
|
||||
# Enumerate all holiday files in the current directory.
|
||||
locales = []
|
||||
re_holiday_file = re.compile("\/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))
|
||||
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("\/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))
|
||||
|
||||
update_locales(locales, args.region, args.year)
|
||||
update_locales(locales, args.region, args.year)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
usage = """See http://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.')
|
||||
parser.add_argument('--year', nargs='+', help='Specific year to fetch.', type=int, default=[])
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
main(args)
|
||||
except Exception as msg:
|
||||
print('Error:', msg)
|
||||
usage = """See http://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.')
|
||||
parser.add_argument('--year', nargs='+', help='Specific year to fetch.', type=int, default=[])
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
main(args)
|
||||
except Exception as msg:
|
||||
print('Error:', msg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue