2 from datetime import datetime, timedelta
4 def d_from_l(l, key, using=None, key_as=str, using_as=None):
7 if not str(obj[key]) in d:
9 d[key_as(obj[key])] = obj
12 d[key_as(obj[key])] = obj[using]
14 d[key_as(obj[key])] = using_as(obj[using])
16 print "Two objects have the same %s key %s!" % (key, obj[key])
20 def dpcus_from_lpcus(l_pcus):
21 d_pcus = d_from_l(l_pcus, 'pcu_id')
24 def dnodes_from_lnodes(l_nodes):
25 d_nodes = d_from_l(l_nodes, 'hostname')
28 def dsites_from_lsites(l_sites):
29 d_sites = d_from_l(l_sites, 'login_base')
32 def dsites_from_lsites_id(l_sites):
33 d_sites = d_from_l(l_sites, 'login_base')
34 id2lb = d_from_l(l_sites, 'site_id', 'login_base', int, str)
35 return (d_sites, id2lb)
37 def dsn_from_dsln(d_sites, id2lb, l_nodes):
43 if id2lb[id] not in lb2hn:
47 # this won't reach sites without nodes, which I guess isn't a problem.
48 if node['site_id'] in id2lb.keys():
49 login_base = id2lb[node['site_id']]
51 print >>sys.stderr, "%s has a foreign site_id %s" % (node['hostname'], node['site_id'])
52 exclude.append(node['hostname'])
55 print i, " ", id2lb[i]
56 raise Exception, "Node has missing site id!! %s %d" %(node['hostname'], node['site_id'])
57 if not login_base in dsn:
58 lb2hn[login_base] = []
60 dsn[login_base]['plc'] = d_sites[login_base]
61 dsn[login_base]['monitor'] = {} # event log, or something
63 hostname = node['hostname']
64 lb2hn[login_base].append(node)
65 dsn[login_base][hostname] = {}
66 dsn[login_base][hostname]['plc'] = node
67 dsn[login_base][hostname]['comon'] = {}
68 dsn[login_base][hostname]['monitor'] = {}
70 hn2lb[hostname] = login_base
71 return (dsn, hn2lb, lb2hn, exclude)
76 def dt_to_ts(cls, dt):
77 t = time.mktime(dt.timetuple())
81 def ts_to_dt(cls, ts):
82 d = datetime.fromtimestamp(ts)
86 def str_to_dt(cls, date_str, format="%Y-%m-%d %H:%M:%S"):
87 dt = datetime.strptime(date_str[:date_str.find('.')], format)
91 def str_to_ts(cls, date_str, format="%Y-%m-%d %H:%M:%S"):
92 ts = time.mktime(time.strptime(date_str[:date_str.find('.')], format))