1 # -*- coding: utf-8 -*-
2 from __future__ import division
9 'datetime_to_timestamp',
13 'clean_alnum_unicode',
19 'str_split_column_ipv6',
23 IPV4_IN_IPV6_PREFIX = '::ffff:'
25 def datetime_to_timestamp(dt):
26 return calendar.timegm(dt.utctimetuple())
28 def read_cfg(filename):
30 Function that reads a file in the form
32 and returns the resulting dictionary
35 for line in file(filename).readlines():
36 line = line.rstrip('\r\n\0')
37 line = unicode(line, 'utf-8')
38 if line.startswith(u'#'):
39 continue # skip comments
40 spl = line.split(u'=', 1)
48 def clean_ais_charset(txt):
49 assert isinstance(txt, str)
53 if oc < 32 or oc > 95:
60 assert isinstance(txt, str)
63 if ( c >= '0' and c <= '9' ) or ( c >= 'A' and c <= 'Z' ):
67 def clean_alnum_unicode(txt):
68 assert isinstance(txt, unicode)
69 return unicode(clean_alnum(txt.encode('ascii7', 'replace')))
72 def open_with_mkdirs(filename, mode):
74 return file(filename, mode)
75 except IOError, ioerr:
76 # FIXME only if doesn't exists ...
77 #print 'Creating directory', os.path.dirname(filename)
78 os.makedirs(os.path.dirname(filename))
79 return file(filename, mode)
83 def logliner(filename):
84 for line in file(filename).readlines():
88 # debug/display wraper source
89 def dumpsource(source):
91 while line and line[-1] in '\r\n\0':
97 return txt.replace(u'&', u'&').replace(u'<', u'<')
100 os.system('touch /home/nirgal/kod/ais/alarm &')
103 def str_split_column_ipv6(txt):
105 Helper function that will split a column separated string of tokens.
106 It will take care not to ignore : in [] for ipv6 support.
111 for i in range(len(txt)):
114 res.append(txt[iprev:i])
121 res.append(txt[iprev:])
125 def formataddr(addr):
126 if addr.startswith(IPV4_IN_IPV6_PREFIX):
134 #if __name__ == '__main__':
135 # for test in ('12:34:56:78',
137 # '1:2:3:abc:[bd:ef::]:45',
138 # '1:2:3:abc:[bd:ef:]:]:45'):
139 # print test, str_split_column_ipv6(test)