Split utility functions, fixing 06 a bit.
[cnetworkmanager.git] / util.py
bloba70fe0c8da6deaa463fbc7a56e2ad7b655694a34
1 import string
2 import re
3 import time
5 def bitmask_str(map, value):
6 ret = []
7 for mask in sorted(map.keys()):
8 if value & mask: ret.append(map[mask])
9 return ",".join(ret)
11 def ssid_str(array):
12 s = ""
13 for b in array:
14 s = s + ("%c" % b)
15 return s
17 def opath_validchar(c):
18 # _ is also escaped even though it is valid
19 return \
20 string.ascii_letters.find(c) != -1 or \
21 string.digits.find(c) != -1
23 def opath_escape(s):
24 r = ""
25 for c in s:
26 # TODO find a more elegant way
27 if not opath_validchar(c):
28 # "-" -> "_2d_"
29 c = "_%2x_" % ord(c)
30 r = r + c
31 return r
33 def opath_unescape(s):
34 # "2d" -> "-"
35 unhex = lambda xx: chr(eval("0x"+xx))
36 # all "_2d_" -> "-"
37 return re.sub("_.._", lambda p: unhex(p.group()[1:3]), s)
39 def dump_time(unixtime):
40 return time.asctime(time.localtime(unixtime))