-
Notifications
You must be signed in to change notification settings - Fork 1
/
Backend.py
66 lines (59 loc) · 2.08 KB
/
Backend.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
import os
import datetime, pytz
import HumanTime
import osm.changeset as oc
import pprint
class Backend(object):
def __init__(self, config, subcfg):
self.debug = False
self.generation = -1 # DB might start out with 'None'
self.config = config
self.subcfg = subcfg
def build_filename(self, globalconfig, subcfg, filename_key='filename'):
if filename_key:
filename = subcfg[filename_key]
else:
filename = ''
if 'path' in subcfg:
filename = os.path.join(subcfg['path'], filename)
return os.path.join(globalconfig.getpath('path', 'tracker'), filename)
def print_state(self, state, update_reason):
if update_reason!='new_generation.osmtracker':
return
time = state.generation_timestamp.strftime('%Y:%m:%d %H:%M:%S')
print 'Generation {} @ {}'.format(state.generation, time)
if self.generation != state.generation:
self.generation = state.generation
self.print_chgsets(state.area_chgsets,
state.area_chgsets_info)
def _pluS(self, num):
'''Return plural s'''
if num==1:
return ''
return 's'
def _i2s(self, num):
'''Int to string using human rounding'''
sign = ''
if num < 0:
sign = '-'
num = -num
ss = str(num)
txt = None
if len(ss) <=8:
for pp in range(0, len(ss), 3):
if txt:
txt = ss[-3:]+','+txt
else:
txt = ss[-3:]
ss = ss[:-3]
return sign+txt
else:
return sign+str(num) # TODO: Better rounding for large numbers
def merge_int_dict(self, a, b):
'''Recursively merge dictionaries where values are either numbers or other dicts'''
for k,v in b.iteritems():
if type(v) is int or type(v) is float:
a[k] = a.get(k, 0)+v
else:
a[k] = self.merge_int_dict(a.get(k, dict()), v)
return a