Cleaning slabslices and slapostgres.
[sfa.git] / sfa / senslab / slabpostgres.py
index b49416e..412d066 100644 (file)
@@ -1,5 +1,3 @@
-import sys
-
 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
 
@@ -7,32 +5,31 @@ from sfa.util.config import Config
 from sfa.util.sfalogging import logger
 
 from sqlalchemy import Column, Integer, String
-from sqlalchemy import Table, Column, MetaData
+from sqlalchemy import Table, MetaData
 from sqlalchemy.ext.declarative import declarative_base
 
 from sqlalchemy.dialects import postgresql
 
-from sqlalchemy import MetaData, Table
 from sqlalchemy.exc import NoSuchTableError
 
-from sqlalchemy import String
 
 #Dict holding the columns names of the table as keys
 #and their type, used for creation of the table
-slice_table = {'record_id_user':'integer PRIMARY KEY references X ON DELETE \
+slice_table = {'record_id_user': 'integer PRIMARY KEY references X ON DELETE \
 CASCADE ON UPDATE CASCADE','oar_job_id':'integer DEFAULT -1',  \
 'record_id_slice':'integer', 'slice_hrn':'text NOT NULL'}
 
 #Dict with all the specific senslab tables
 tablenames_dict = {'slice_senslab': slice_table}
 
-##############################
-
-
 
 SlabBase = declarative_base()
 
 class SliceSenslab (SlabBase):
+    """ SQL alchemy class to manipulate slice_senslab table in 
+    slab_sfa database.
+    
+    """
     __tablename__ = 'slice_senslab' 
     #record_id_user = Column(Integer, primary_key=True)
 
@@ -54,7 +51,7 @@ class SliceSenslab (SlabBase):
         if slice_hrn:
             self.slice_hrn = slice_hrn
         if record_id_user: 
-            self.record_id_user= record_id_user
+            self.record_id_user = record_id_user
         if peer_authority:
             self.peer_authority = peer_authority
             
@@ -83,16 +80,19 @@ class SliceSenslab (SlabBase):
           
           
 class SlabDB:
+    """ SQL Aclehmy connection class.
+    From alchemy.py
+    """
     def __init__(self,config, debug = False):
         self.sl_base = SlabBase
-        dbname="slab_sfa"
+        dbname = "slab_sfa"
         if debug == True :
             l_echo_pool = True
             l_echo=True 
         else :
             l_echo_pool = False
             l_echo = False 
-        # will be created lazily on-demand
+
         self.slab_session = None
         # the former PostgreSQL.py used the psycopg2 directly and was doing
         #self.connection.set_client_encoding("UNICODE")
@@ -125,8 +125,11 @@ class SlabDB:
     
     
     def check (self):
-        self.slab_engine.execute ("select 1").scalar()
+        """ Cehck if a table exists by trying a selection
+        on the table. 
         
+        """
+        self.slab_engine.execute ("select 1").scalar()
         
         
     def session (self):
@@ -160,9 +163,9 @@ class SlabDB:
        
         try:
             metadata = MetaData (bind=self.slab_engine)
-            table=Table (tablename, metadata, autoload=True)
-           
+            table = Table (tablename, metadata, autoload=True)
             return True
+        
         except NoSuchTableError:
             logger.log_exc("SLABPOSTGRES tablename %s does not exists" \
                             %(tablename))
@@ -184,6 +187,6 @@ class SlabDB:
 
 from sfa.util.config import Config
 
-slab_alchemy= SlabDB(Config())
-slab_engine=slab_alchemy.slab_engine
-slab_dbsession=slab_alchemy.session()
+slab_alchemy = SlabDB(Config())
+slab_engine = slab_alchemy.slab_engine
+slab_dbsession = slab_alchemy.session()