-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathutils.py
93 lines (62 loc) · 2.01 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import argparse
import collections
import datetime
import functools
import json
import os
import time
DATETIME_FMT = "%Y-%m-%dT%H:%M:%SZ"
def strptime(dt):
return datetime.datetime.strptime(dt, DATETIME_FMT)
def strftime(dt):
return dt.strftime(DATETIME_FMT)
def ratelimit(limit, every):
def limitdecorator(fn):
times = collections.deque()
@functools.wraps(fn)
def wrapper(*args, **kwargs):
if len(times) >= limit:
t0 = times.pop()
t = time.time()
sleep_time = every - (t - t0)
if sleep_time > 0:
time.sleep(sleep_time)
times.appendleft(time.time())
return fn(*args, **kwargs)
return wrapper
return limitdecorator
def chunk(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
def get_abs_path(path):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), path)
def load_json(path):
with open(path) as f:
return json.load(f)
def load_schema(entity):
return load_json(get_abs_path("schemas/{}.json".format(entity)))
def update_state(state, entity, dt):
if dt is None:
return
if isinstance(dt, datetime.datetime):
dt = strftime(dt)
if entity not in state:
state[entity] = dt
if dt >= state[entity]:
state[entity] = dt
def parse_args(required_config_keys):
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', help='Config file', required=True)
parser.add_argument('-s', '--state', help='State file')
args = parser.parse_args()
config = load_json(args.config)
check_config(config, required_config_keys)
if args.state:
state = load_json(args.state)
else:
state = {}
return config, state
def check_config(config, required_keys):
missing_keys = [key for key in required_keys if key not in config]
if missing_keys:
raise Exception("Config is missing required keys: {}".format(missing_keys))