1 # -*- coding: utf-8 -*-
2 from __future__ import division
9 'datetime_to_timestamp',
13 'clean_alnum_unicode',
19 'str_split_column_ipv6',
26 IPV4_IN_IPV6_PREFIX = '::ffff:'
28 def datetime_to_timestamp(dt):
29 return calendar.timegm(dt.utctimetuple())
31 def read_cfg(filename):
33 Function that reads a file in the form
35 and returns the resulting dictionary
38 for line in file(filename).readlines():
39 line = line.rstrip('\r\n\0')
40 line = unicode(line, 'utf-8')
41 if line.startswith(u'#'):
42 continue # skip comments
43 spl = line.split(u'=', 1)
51 def clean_ais_charset(txt):
52 assert isinstance(txt, str)
56 if oc < 32 or oc > 95:
63 assert isinstance(txt, str)
66 if ( c >= '0' and c <= '9' ) or ( c >= 'A' and c <= 'Z' ):
70 def clean_alnum_unicode(txt):
71 assert isinstance(txt, unicode)
72 return unicode(clean_alnum(txt.encode('ascii7', 'replace')))
75 def open_with_mkdirs(filename, mode):
77 return file(filename, mode)
78 except IOError, ioerr:
79 # FIXME only if doesn't exists ...
80 #print 'Creating directory', os.path.dirname(filename)
81 os.makedirs(os.path.dirname(filename))
82 return file(filename, mode)
86 def logliner(filename):
87 for line in file(filename).readlines():
91 # debug/display wraper source
92 def dumpsource(source):
94 while line and line[-1] in '\r\n\0':
100 return txt.replace(u'&', u'&').replace(u'<', u'<')
103 os.system('touch /home/nirgal/kod/ais/alarm &')
106 def str_split_column_ipv6(txt):
108 Helper function that will split a column separated string of tokens.
109 It will take care not to ignore : in [] for ipv6 support.
114 for i in range(len(txt)):
117 res.append(txt[iprev:i])
124 res.append(txt[iprev:])
128 def formataddr(addr):
129 if addr.startswith(IPV4_IN_IPV6_PREFIX):
139 class LatLonFormatError(ValueError):
143 def clean_latitude(data):
145 Convert a string latitude into a float.
146 Raises LatLonFormatError on error.
148 data = data.replace(u"''", u'"') # common mistake
149 data = data.replace(u' ', u'') # remove spaces
153 tmp, side = data[:-1], data[-1]
156 elif side == sides[1]:
159 raise LatLonFormatError(u'Last character must be either %s or %s.', sides[0], sides[1])
160 spl = tmp.split(u'°')
162 raise LatLonFormatError(u'You need to use the ° character.')
167 raise LatLonFormatError(u'Degrees must be an number. It\'s %s.', d)
168 spl = tmp.split(u"'", 1)
170 # no ' sign: ok only if there is nothing but the side after °
171 # we don't accept seconds if there is no minutes:
172 # It might be an entry mistake
177 raise LatLonFormatError(u'You must use the \' character between ° and %s.', data[-1])
183 raise LatLonFormatError(u'Minutes must be an number. It\'s %s.', m)
188 raise LatLonFormatError(u'You must use the " character between seconds and %s.', data[-1])
193 raise LatLonFormatError(u'Seconds must be an number. It\'s %s.', s)
194 data = side * ( d + m / 60 + s / 3600)
196 if data < -90 or data > 90:
197 raise LatLonFormatError(u'%s in not in -90..90 range', data)
202 def clean_longitude(data):
204 Convert a string latitude into a float.
205 Raises LatLonFormatError on error.
207 data = data.replace(u"''", u'"') # common mistake
208 data = data.replace(u' ', u'') # remove spaces
212 tmp, side = data[:-1], data[-1]
215 elif side == sides[1]:
218 raise LatLonFormatError(u'Last character must be either %s or %s.', sides[0], sides[1])
219 spl = tmp.split(u'°')
221 raise LatLonFormatError(u'You need to use the ° character.')
226 raise LatLonFormatError(u'Degrees must be an number. It\'s %s.', d)
227 spl = tmp.split(u"'", 1)
229 # no ' sign: ok only if there is nothing but the side after °
230 # we don't accept seconds if there is no minutes:
231 # It might be an entry mistake
236 raise LatLonFormatError(u'You must use the \' character between ° and %s.', data[-1])
242 raise LatLonFormatError(u'Minutes must be an number. It\'s %s.', m)
247 raise LatLonFormatError(u'You must use the " character between seconds and %s.', data[-1])
252 raise LatLonFormatError(u'Seconds must be an number. It\'s %s.', s)
253 data = side * ( d + m / 60 + s / 3600)
255 if data < -180 or data > 180:
256 raise LatLonFormatError(u'%s in not in -180..180 range', data)
259 #if __name__ == '__main__':
260 # for test in ('12:34:56:78',
262 # '1:2:3:abc:[bd:ef::]:45',
263 # '1:2:3:abc:[bd:ef:]:]:45'):
264 # print test, str_split_column_ipv6(test)