1 # -*- coding: utf-8 -*-
2 from __future__ import division
10 'datetime_to_timestamp',
14 'clean_alnum_unicode',
20 'str_split_column_ipv6',
27 IPV4_IN_IPV6_PREFIX = '::ffff:'
29 def datetime_to_timestamp(dt):
30 return calendar.timegm(dt.utctimetuple())
32 def read_cfg(filename):
34 Function that reads a file in the form
36 and returns the resulting dictionary
39 for line in file(filename).readlines():
40 line = line.rstrip('\r\n\0')
41 line = unicode(line, 'utf-8')
42 if line.startswith(u'#'):
43 continue # skip comments
44 spl = line.split(u'=', 1)
52 def clean_ais_charset(txt):
53 assert isinstance(txt, str)
57 if oc < 32 or oc > 95:
64 assert isinstance(txt, str)
67 if ( c >= '0' and c <= '9' ) or ( c >= 'A' and c <= 'Z' ):
71 def clean_alnum_unicode(txt):
72 assert isinstance(txt, unicode)
73 return unicode(clean_alnum(txt.encode('ascii7', 'replace')))
76 def open_with_mkdirs(filename, mode):
78 return file(filename, mode)
79 except IOError, ioerr:
80 logging.warning("file(%s,%s): errno %s %s", filename, mode, ioerr.errno, ioerr.strerror)
81 # FIXME only if doesn't exists ...
82 #print 'Creating directory', os.path.dirname(filename)
83 os.makedirs(os.path.dirname(filename))
84 return file(filename, mode)
88 def logliner(filename):
89 for line in file(filename).readlines():
93 # debug/display wraper source
94 def dumpsource(source):
96 while line and line[-1] in '\r\n\0':
102 return txt.replace(u'&', u'&').replace(u'<', u'<')
105 os.system('touch /home/nirgal/kod/ais/alarm &')
108 def str_split_column_ipv6(txt):
110 Helper function that will split a column separated string of tokens.
111 It will take care not to ignore : in [] for ipv6 support.
116 for i in range(len(txt)):
119 res.append(txt[iprev:i])
126 res.append(txt[iprev:])
130 def formataddr(addr):
131 if addr.startswith(IPV4_IN_IPV6_PREFIX):
141 class LatLonFormatError(ValueError):
145 def clean_latitude(data):
147 Convert a string latitude into a float.
148 Raises LatLonFormatError on error.
150 data = data.replace(u"''", u'"') # common mistake
151 data = data.replace(u' ', u'') # remove spaces
155 tmp, side = data[:-1], data[-1]
158 elif side == sides[1]:
161 raise LatLonFormatError(u'Last character must be either %s or %s.', sides[0], sides[1])
162 spl = tmp.split(u'°')
164 raise LatLonFormatError(u'You need to use the ° character.')
169 raise LatLonFormatError(u'Degrees must be an number. It\'s %s.', d)
170 spl = tmp.split(u"'", 1)
172 # no ' sign: ok only if there is nothing but the side after °
173 # we don't accept seconds if there is no minutes:
174 # It might be an entry mistake
179 raise LatLonFormatError(u'You must use the \' character between ° and %s.', data[-1])
185 raise LatLonFormatError(u'Minutes must be an number. It\'s %s.', m)
190 raise LatLonFormatError(u'You must use the " character between seconds and %s.', data[-1])
195 raise LatLonFormatError(u'Seconds must be an number. It\'s %s.', s)
196 data = side * ( d + m / 60 + s / 3600)
198 if data < -90 or data > 90:
199 raise LatLonFormatError(u'%s in not in -90..90 range', data)
204 def clean_longitude(data):
206 Convert a string latitude into a float.
207 Raises LatLonFormatError on error.
209 data = data.replace(u"''", u'"') # common mistake
210 data = data.replace(u' ', u'') # remove spaces
214 tmp, side = data[:-1], data[-1]
217 elif side == sides[1]:
220 raise LatLonFormatError(u'Last character must be either %s or %s.', sides[0], sides[1])
221 spl = tmp.split(u'°')
223 raise LatLonFormatError(u'You need to use the ° character.')
228 raise LatLonFormatError(u'Degrees must be an number. It\'s %s.', d)
229 spl = tmp.split(u"'", 1)
231 # no ' sign: ok only if there is nothing but the side after °
232 # we don't accept seconds if there is no minutes:
233 # It might be an entry mistake
238 raise LatLonFormatError(u'You must use the \' character between ° and %s.', data[-1])
244 raise LatLonFormatError(u'Minutes must be an number. It\'s %s.', m)
249 raise LatLonFormatError(u'You must use the " character between seconds and %s.', data[-1])
254 raise LatLonFormatError(u'Seconds must be an number. It\'s %s.', s)
255 data = side * ( d + m / 60 + s / 3600)
257 if data < -180 or data > 180:
258 raise LatLonFormatError(u'%s in not in -180..180 range', data)
261 #if __name__ == '__main__':
262 # for test in ('12:34:56:78',
264 # '1:2:3:abc:[bd:ef::]:45',
265 # '1:2:3:abc:[bd:ef:]:]:45'):
266 # print test, str_split_column_ipv6(test)