-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
executable file
·111 lines (108 loc) · 2.89 KB
/
convert.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
from model import *
from configobj import ConfigObj
def save(config):
config = ConfigObj(config)
config['config'] = {}
config['config']['wfact'] = conf().wfact
config['config']['start'] = conf().start.strftime('%y%m%d%H:%M')
config['config']['naith'] = conf().naith
config['config']['figs'] = conf().figs
bs = Base.query.all()
if bs:
config['bases'] = {}
for b in bs:
config['bases'][b.name] = [b.ref(), b.h]
rs = Route.query.all()
if rs:
config['routes'] = {}
for r in rs:
config['routes'][r.name] = [b.name for b in r.bases]
ls = Leg.query.all()
if ls:
config['legs'] = {}
for l in ls:
id = '-'.join([l.start.name, l.end.name])
config['legs'][id] = [l.dist, l.gain]
ts = Team.query.all()
if ts:
config['teams'] = {}
for t in ts:
time = t.start.strftime('%H:%M')
config['teams'][t.name] = [t.route.name, time]
config.write()
def load(hike):
'''Create the hike definition if the config exists'''
config = ConfigObj(hike)
if 'config' in config:
c = config['config']
if 'start' in c:
start = c['start']
conf().start = mkdt(start[-5:], start[:-5])
if 'wfact' in c: conf().wfact = float(c['wfact'])
if 'naith' in c: conf().naith = float(c['naith'])
if 'figs' in c: conf().figs = int(c['figs'])
if 'bases' in config:
for b in config['bases']:
Base(b, *config['bases'][b])
if 'routes' in config:
for r in config['routes']:
Route(r, config['routes'][r])
if 'legs' in config:
c = config['legs']
def auto(route, *dgs):
route = Route.get(route)
for i in range(len(dgs)):
dg = dgs[i].split('/')
d = dg[0]
if len(dg) == 2: g = dg[1]
else: g = None
base, next = route.base_refs[i:i+2]
Leg.setup(base, next, d, g)
if 'routes' in c:
if 'routes' in config:
for r in c['routes']:
auto(r, *c['routes'][r])
c.pop('routes')
for l in c:
start, end = l.split('-')
Leg.setup(start, end, *c[l])
if 'teams' in config:
c = config['teams']
def auto(route, prefix, first, last, interval=None, offset=0):
from datetime import timedelta
if not interval:
interval = 5
interval = timedelta(minutes=int(interval))
offset = timedelta(minutes=int(offset))
st = conf().start + offset - int(first)*interval
for i in range(int(first),int(last)+1):
start = st + i*interval
name = prefix + str(i).rjust(2,'0')
Team(name, route, start.time())
if 'routes' in c:
if 'routes' in config:
for r in c['routes']:
auto(r, *c['routes'][r])
c.pop('routes')
for t in c:
Team(t, *c[t])
db.session.commit()
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
config = argv[1]
if '.conf' in config:
hike = config.replace('.conf', '.hike')
else:
hike = config + '.hike'
dest = None
else:
hike = argv[1]
dest = argv[2]
db.Entity.metadata.bind = 'sqlite:///%s' % hike
if dest:
save(dest)
else:
db.Entity.metadata.create_all()
load(config)