forked from idcf/cloudstack-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
83 lines (74 loc) · 2.4 KB
/
Copy pathutils.py
File metadata and controls
83 lines (74 loc) · 2.4 KB
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
# -*- coding: utf-8 -*-
import parsedatetime.parsedatetime as pdt
import parsedatetime.parsedatetime_consts as pdc
import datetime
import time
def safe_option(config, section, option):
retval = config.get(section, option)
if retval:
return retval
else:
raise Exception, "[%s] に [%s] を設定してください。" % (section,option)
class res(object):
def __repr__(self):
reprkeys = sorted(k for k in self.__dict__.keys())
info = []
for k in reprkeys:
val = getattr(self,k)
if isinstance(val,unicode):
val = val.encode("utf-8")
elif isinstance(val,int):
val = str(val)
info.append("%s=%s" % (k,val))
return "<%s %s>" % (self.__class__.__name__, ", ".join(info))
def dict2obj(d,use_key=False):
if not d:
return None
top = res()
if isinstance(d,dict):
for k, v in d.iteritems():
if isinstance(v, dict):
setattr(top, k, dict2obj(v))
elif isinstance(v, list):
tmp_list = []
for vv in v:
if isinstance(vv, dict):
tmp_list.append(dict2obj(vv,True))
else:
tmp_list.append(vv)
if use_key:
list_name = k
else:
list_name = 'list'
setattr(top, list_name,
type(v)(tmp_list))
#type(v)(dict2obj(vv) if isinstance(vv, dict) else vv for vv in v))
else:
setattr(top,k,v)
elif isinstance(d,list):
tmp_list = []
for vv in d:
if isinstance(vv, dict):
tmp_list.append(dict2obj(vv,True))
else:
tmp_list.append(vv)
if use_key:
list_name = k
else:
list_name = 'list'
setattr(top, list_name,type(d)(tmp_list))
if not hasattr(top,"count"):
setattr(top,"count",len(getattr(top,"list")))
return top
def datetimeFromString(s):
c = pdc.Constants()
p = pdt.Calendar(c)
result, what = p.parse(s)
dt = 0
if what in (1,2,3):
dt = datetime.datetime( *result[:6] )
if what == 0:
return ""
return dt
def time_format(epoch):
return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(epoch))