Some small pylint fixes
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Thu, 30 Dec 2010 16:15:18 +0000 (16:15 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Thu, 30 Dec 2010 16:15:18 +0000 (16:15 +0000)
bin/common.py
bin/db.py

index 962f1e51256fc99f9a7e696c8d3933f7bad2e566..a8d9f46b036599b3e997bc16c5dc252951a18acd 100755 (executable)
@@ -27,10 +27,6 @@ __all__ = [
     #'_hash3_pathfilename',
     'db_bydate_addrecord',
     'db_lastinfo_setrecord_ifnewer',
-    #'aivdm_record123_format',
-    #'aivdm_record123_length',
-    #'aivdm_record5_format',
-    #'aivdm_record5_length',
     'add_nmea1',
     'add_nmea5_full',
     'add_nmea5_partial',
@@ -613,10 +609,10 @@ def _sql_add_nmea5(strmmsi, timestamp, imo, name, callsign, type, \
 
 
 
-aivdm_record123_format = 'IBbhiiII4s'
-aivdm_record123_length = struct.calcsize(aivdm_record123_format)
-aivdm_record5_format = 'II20s7sBHHBBBBBBH20s4s'
-aivdm_record5_length = struct.calcsize(aivdm_record5_format)
+AIVDM_RECORD123_FORMAT = 'IBbhiiII4s'
+AIVDM_RECORD123_LENGTH = struct.calcsize(AIVDM_RECORD123_FORMAT)
+AIVDM_RECORD5_FORMAT = 'II20s7sBHHBBBBBBH20s4s'
+AIVDM_RECORD5_LENGTH = struct.calcsize(AIVDM_RECORD5_FORMAT)
 
 
 def add_nmea1(strmmsi, timestamp, status, rot, sog, \
@@ -625,7 +621,7 @@ def add_nmea1(strmmsi, timestamp, status, rot, sog, \
     Input is raw data, unscaled
     FIXME: lat & lon are inverted compared to raw aivdm structure
     '''
-    record = struct.pack(aivdm_record123_format, timestamp, status, rot, sog, latitude, longitude, cog, heading, source)
+    record = struct.pack(AIVDM_RECORD123_FORMAT, timestamp, status, rot, sog, latitude, longitude, cog, heading, source)
     #print repr(record)
     filename = strmmsi+'.nmea1'
     db_bydate_addrecord(filename, record, timestamp)
@@ -641,7 +637,7 @@ def add_nmea5_full(strmmsi, timestamp, imo, name, callsign, type, \
     All fields are set, and can be upgraded if the record is newer
     FIXME: name & callsign are inverted compared to raw aivdm structure
     '''
-    record = struct.pack(aivdm_record5_format, timestamp, imo, name, callsign, \
+    record = struct.pack(AIVDM_RECORD5_FORMAT, timestamp, imo, name, callsign, \
                          type, dim_bow, dim_stern, dim_port, dim_starboard, \
                          eta_M, eta_D, eta_h, eta_m, draught, destination, source)
     #print repr(record)
@@ -661,7 +657,7 @@ def add_nmea5_partial(strmmsi, timestamp, imo, name, callsign, type, \
     Input is raw data, unscaled
     All fields are not set. Only some of them can be upgraded, if they're newer
     '''
-    record = struct.pack(aivdm_record5_format, \
+    record = struct.pack(AIVDM_RECORD5_FORMAT, \
                          timestamp, imo, name, callsign, type, \
                          dim_bow, dim_stern, dim_port, dim_starboard, \
                          eta_M, eta_D, eta_h, eta_m, draught, destination, \
@@ -685,12 +681,12 @@ def add_nmea5_partial(strmmsi, timestamp, imo, name, callsign, type, \
         updated = True
     else:
         lockf(f, LOCK_EX)
-        oldrecord = f.read(aivdm_record5_length)
+        oldrecord = f.read(AIVDM_RECORD5_LENGTH)
         oldtimestamp, oldimo, oldname, oldcallsign, oldtype, \
         olddim_bow, olddim_stern, olddim_port, olddim_starboard, \
         oldeta_M, oldeta_D, oldeta_h, oldeta_m, \
         olddraught, olddestination, oldsource \
-                  = struct.unpack(aivdm_record5_format, oldrecord)
+                  = struct.unpack(AIVDM_RECORD5_FORMAT, oldrecord)
         if timestamp > oldtimestamp:
             # we have incoming recent information
             if imo == 0:
@@ -718,7 +714,7 @@ def add_nmea5_partial(strmmsi, timestamp, imo, name, callsign, type, \
                 destination = olddestination
             if draught == 0:
                 draught = olddraught
-            record = struct.pack(aivdm_record5_format, \
+            record = struct.pack(AIVDM_RECORD5_FORMAT, \
                                  timestamp, imo, name, callsign, type, \
                                  dim_bow, dim_stern, dim_port, dim_starboard, \
                                  eta_M, eta_D, eta_h, eta_m, draught, \
@@ -766,7 +762,7 @@ def add_nmea5_partial(strmmsi, timestamp, imo, name, callsign, type, \
                 updated = True
             if updated:
                 oldsource = source
-                record = struct.pack(aivdm_record5_format, \
+                record = struct.pack(AIVDM_RECORD5_FORMAT, \
                                      oldtimestamp, oldimo, oldname, \
                                      oldcallsign, oldtype, \
                                      olddim_bow, olddim_stern, \
@@ -849,24 +845,24 @@ class Nmea1:
         return self.timestamp_1, self.status, self.rot, self.sog, self.latitude, self.longitude, self.cog, self.heading, self.source_1
 
     def from_record(self, record):
-        values = struct.unpack(aivdm_record123_format, record)
+        values = struct.unpack(AIVDM_RECORD123_FORMAT, record)
         Nmea1.__init__(self, *values)
 
     @staticmethod
     def new_from_record(record):
-        values = struct.unpack(aivdm_record123_format, record)
+        values = struct.unpack(AIVDM_RECORD123_FORMAT, record)
         return Nmea1(*values)
 
     def to_record(self):
-        return struct.pack(aivdm_record123_format, *Nmea1.to_values())
+        return struct.pack(AIVDM_RECORD123_FORMAT, *Nmea1.to_values())
         
     def from_file(self, file):
-        record = file.read(aivdm_record123_length)
+        record = file.read(AIVDM_RECORD123_LENGTH)
         Nmea1.from_record(self, record)
 
     @staticmethod
     def new_from_file(file):
-        record = file.read(aivdm_record123_length)
+        record = file.read(AIVDM_RECORD123_LENGTH)
         return Nmea1.new_from_record(record)
 
     def from_lastinfo(self, strmmsi):
@@ -889,7 +885,7 @@ class Nmea1:
             logging.debug("file %s doesn't exists" % filename_nmea1)
             return None
         lockf(f, LOCK_SH)
-        record = f.read(aivdm_record123_length)
+        record = f.read(AIVDM_RECORD123_LENGTH)
         f.close()
         return Nmea1.new_from_record(record)
 
@@ -992,7 +988,7 @@ class Nmea5:
 
     from_values = __init__
 
-    def merge_from_values(self, timestamp, imo=0, name='', callsign='', type=0, dim_bow=0, dim_stern=0, dim_port=0, dim_starboard=0, eta_M=0, eta_D=0, eta_h=24, eta_m=60, draught=0, destination='', source=''):
+    def merge_from_values(self, timestamp, imo=0, name='', callsign='', type_=0, dim_bow=0, dim_stern=0, dim_port=0, dim_starboard=0, eta_M=0, eta_D=0, eta_h=24, eta_m=60, draught=0, destination='', source=''):
         updated = False
         if self.imo == 0 or imo != 0:
             self.imo = imo
@@ -1003,8 +999,8 @@ class Nmea5:
         if self.callsign == '' or callsign != '':
             self.callsign = callsign
             updated = True
-        if self.type == 0 or type != 0:
-            self.type = type
+        if self.type == 0 or type_ != 0:
+            self.type = type_
             updated = True
         if self.dim_bow == 0 or dim_bow != 0:
             self.dim_bow = dim_bow
@@ -1039,24 +1035,24 @@ class Nmea5:
         return self.timestamp_5, self.imo, self.name, self.callsign, self.type, self.dim_bow, self.dim_stern, self.dim_port, self.dim_starboard, self.eta_M, self.eta_D, self.eta_h, self.eta_m, self.draught, self.destination, self.source_5
 
     def from_record(self, record):
-        values = struct.unpack(aivdm_record5_format, record)
+        values = struct.unpack(AIVDM_RECORD5_FORMAT, record)
         Nmea5.__init__(self, *values)
 
     @staticmethod
     def new_from_record(record):
-        values = struct.unpack(aivdm_record5_format, record)
+        values = struct.unpack(AIVDM_RECORD5_FORMAT, record)
         return Nmea5(*values)
 
     def to_record(self):
-        return struct.pack(aivdm_record5_format, *Nmea5.to_values(self))
+        return struct.pack(AIVDM_RECORD5_FORMAT, *Nmea5.to_values(self))
         
     def from_file(self, file):
-        record = file.read(aivdm_record5_length)
+        record = file.read(AIVDM_RECORD5_LENGTH)
         Nmea5.from_record(self, record)
 
     @staticmethod
     def new_from_file(file):
-        record = file.read(aivdm_record5_length)
+        record = file.read(AIVDM_RECORD5_LENGTH)
         return Nmea5.new_from_record(record)
 
     def from_lastinfo(self, strmmsi):
@@ -1081,7 +1077,7 @@ class Nmea5:
             logging.debug("file %s doesn't exists" % filename_nmea5)
             return None
         lockf(f, LOCK_SH)
-        record = f.read(aivdm_record5_length)
+        record = f.read(AIVDM_RECORD5_LENGTH)
         f.close()
         return Nmea5.new_from_record(record)
 
@@ -1348,7 +1344,7 @@ class BankNmea1(list):
         File must be locked before call
         '''
         while True:
-            record = file.read(aivdm_record123_length)
+            record = file.read(AIVDM_RECORD123_LENGTH)
             if not record:
                 break
             self.append(Nmea1.new_from_record(record))
@@ -1530,7 +1526,7 @@ class BankNmea5(list):
         File must be locked before call
         '''
         while True:
-            record = file.read(aivdm_record5_length)
+            record = file.read(AIVDM_RECORD5_LENGTH)
             if not record:
                 break
             self.append(Nmea5.new_from_record(record))
@@ -1947,7 +1943,7 @@ def main():
             dt_end = datetime.strptime(options.sdt_end, '%Y%m%d%H%M%S')
         elif len(options.sdt_end)==8:
             dt_end = datetime.strptime(options.sdt_end, '%Y%m%d')
-            dt_end = datetime.combine(dt_end.date(), time(23,59,59))
+            dt_end = datetime.combine(dt_end.date(), time(23, 59, 59))
         else:
             print >> sys.stderr, "Invalid format for --end option"
             sys.exit(1)
@@ -2021,6 +2017,7 @@ def main():
             print >> sys.stderr, "removemanual action doesn't support filters"
             sys.exit(1)
 
+        # TODO: dates = range dt_start, dt_end
         for dt in dates:
             logging.info("Processing date %s", dt)
             for mmsi in target_mmsi_iterator:
index 0890ab3f214adbdff23ecb02c9f289630e7f51be..768820280db2a3e87f56cab6822191befc99b93c 100644 (file)
--- a/bin/db.py
+++ b/bin/db.py
@@ -1,15 +1,27 @@
-#!/usr/bin/env python
 # -*- encoding: utf-8 -*-
-
+'''
+database library.
+'''
 from __future__ import division
-import psycopg2, psycopg2.extensions
 
-from ais.ntools import read_cfg
+__all__ = [
+    'get_common_db',
+    'get_common_cursor',
+    'sql_setdebug',
+    'sqlexec',
+    'dbcommit',
+    ]
 
 DATABASE_CONFIG_FILE = '/etc/ais/database'
 DUMP_SQL_QUERIES = False
 
-def get_connect_str():
+def _get_connect_str():
+    '''
+    Returns a connection string suitable for psycopg
+    based on the content of DATABASE_CONFIG_FILE.
+    '''
+    from ais.ntools import read_cfg
+
     cfg = read_cfg(DATABASE_CONFIG_FILE)
 
     dbname = cfg.get('dbname', None)
@@ -27,32 +39,51 @@ def get_connect_str():
         connectstr += u' password=' + password
     return connectstr
     
-__db = None
+__db__ = None
 def get_common_db():
-    global __db
-    if not __db:
-        psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
-        __db=psycopg2.connect(get_connect_str())
-        __db.set_client_encoding('UTF8')
-        __db.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
-    return __db
-
-__cursor = None
+    '''
+    Returns a singleton on a psycoPG database connection
+    '''
+    global __db__
+    if not __db__:
+        import psycopg2
+        import psycopg2.extensions as psyext
+
+        psyext.register_type(psyext.UNICODE)
+        __db__ = psycopg2.connect(_get_connect_str())
+        __db__.set_client_encoding('UTF8')
+        __db__.set_isolation_level(psyext.ISOLATION_LEVEL_AUTOCOMMIT)
+    return __db__
+
+__cursor__ = None
 def get_common_cursor():
-    global __cursor
-    if not __cursor:
-        __cursor = get_common_db().cursor()
-    return __cursor
+    '''
+    Returns a singleton on a psycoPG database cursor
+    '''
+    global __cursor__
+    if not __cursor__:
+        __cursor__ = get_common_db().cursor()
+    return __cursor__
 
-def sql_setdebug(b):
+def sql_setdebug(isdebug):
+    '''
+    Turns on/off sql loggin to stdout
+    '''
     global DUMP_SQL_QUERIES
-    DUMP_SQL_QUERIES = b
+    DUMP_SQL_QUERIES = isdebug
 
 def sqlexec(sql, *args, **kargs):
+    '''
+    Execute an sql statement, using the common cursor
+    '''
     cursor = get_common_cursor()
     if DUMP_SQL_QUERIES:
         print cursor.mogrify(sql.encode('utf8'), *args, **kargs)
     cursor.execute(sql, *args, **kargs)
 
 def dbcommit():
+    '''
+    Commit singleton cursor.
+    NOTE: This is not necessary as transaction are disabled.
+    '''
     get_common_db().commit()