<s> '''<STR_LIT>''' <EOL> import sqlalchemy <EOL> from sqlalchemy . orm import sessionmaker , scoped_session <EOL> from sqlalchemy . orm import exc <EOL> import logging <EOL> import contextlib <EOL> from model import Base , Device , InterfaceDefinition , LeafSetting <EOL> from common import SingletonBase <EOL> from propLoader import loadLoggingConfig <EOL> from exception import InvalidConfiguration <EOL> moduleName = '<STR_LIT>' <EOL> loadLoggingConfig ( appName = moduleName ) <EOL> logger = logging . getLogger ( moduleName ) <EOL> class AbstractDao ( SingletonBase ) : <EOL> def __init__ ( self ) : <EOL> debugSql = False <EOL> if logger . isEnabledFor ( logging . DEBUG ) : <EOL> debugSql = True <EOL> self . __engine = None <EOL> self . __sessionFactory = None <EOL> dbUrl = self . _getDbUrl ( ) <EOL> if '<STR_LIT>' in dbUrl : <EOL> self . __engine = sqlalchemy . create_engine ( dbUrl , echo = debugSql ) <EOL> elif '<STR_LIT>' in dbUrl : <EOL> self . __engine = sqlalchemy . create_engine ( dbUrl , echo = debugSql , <EOL> pool_recycle = <NUM_LIT> , isolation_level = "<STR_LIT>" ) <EOL> else : <EOL> logger . error ( '<STR_LIT>' % dbUrl ) <EOL> raise InvalidConfiguration ( '<STR_LIT>' % dbUrl ) <EOL> Base . metadata . create_all ( self . __engine ) <EOL> self . __sessionFactory = sessionmaker ( bind = self . __engine ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> def __del__ ( self ) : <EOL> if self . __engine : <EOL> self . __sessionFactory . close_all ( ) <EOL> self . __engine . dispose ( ) <EOL> @ contextlib . contextmanager <EOL> def getReadSession ( self ) : <EOL> try : <EOL> session = scoped_session ( self . __sessionFactory ) <EOL> yield session <EOL> except Exception as ex : <EOL> logger . error ( ex ) <EOL> raise <EOL> finally : <EOL> session . remove ( ) <EOL> @ contextlib . contextmanager <EOL> def getReadWriteSession ( self ) : <EOL> try : <EOL> session = scoped_session ( self . __sessionFactory ) <EOL> yield session <EOL> session . commit ( ) <EOL> except Exception as ex : <EOL> session . rollback ( ) <EOL> logger . error ( ex ) <EOL> raise <EOL> finally : <EOL> session . remove ( ) <EOL> def _getRawSession ( self ) : <EOL> return scoped_session ( self . __sessionFactory ) <EOL> def _getDbUrl ( self ) : <EOL> raise NotImplementedError <EOL> def createObjects ( self , session , objects ) : <EOL> session . add_all ( objects ) <EOL> def createObjectsAndCommitNow ( self , session , objects ) : <EOL> try : <EOL> session . add_all ( objects ) <EOL> session . commit ( ) <EOL> except Exception as ex : <EOL> logger . error ( ex ) <EOL> session . rollback ( ) <EOL> def deleteObject ( self , session , obj ) : <EOL> session . delete ( obj ) <EOL> def deleteObjects ( self , session , objects ) : <EOL> for obj in objects : <EOL> session . delete ( obj ) <EOL> def updateObjects ( self , session , objects ) : <EOL> for obj in objects : <EOL> session . merge ( obj ) <EOL> def updateObjectsAndCommitNow ( self , session , objects ) : <EOL> try : <EOL> for obj in objects : <EOL> session . merge ( obj ) <EOL> session . commit ( ) <EOL> except Exception as ex : <EOL> logger . error ( ex ) <EOL> session . rollback ( ) <EOL> def getAll ( self , session , objectType ) : <EOL> return session . query ( objectType ) . order_by ( objectType . name ) . all ( ) <EOL> def getObjectById ( self , session , objectType , id ) : <EOL> return session . query ( objectType ) . filter_by ( id = id ) . one ( ) <EOL> def getUniqueObjectByName ( self , session , objectType , name ) : <EOL> try : <EOL> return session . query ( objectType ) . filter_by ( name = name ) . one ( ) <EOL> except ( exc . NoResultFound , exc . MultipleResultsFound ) as ex : <EOL> logger . info ( str ( ex ) ) <EOL> def getObjectsByName ( self , session , objectType , name ) : <EOL> return session . query ( objectType ) . filter_by ( name = name ) . all ( ) <EOL> def getIfdByDeviceNamePortName ( self , session , deviceName , portName ) : <EOL> try : <EOL> device = session . query ( Device ) . filter_by ( name = deviceName ) . one ( ) <EOL> return session . query ( InterfaceDefinition ) . filter_by ( device_id = device . id ) . filter_by ( name = portName ) . one ( ) <EOL> except ( exc . NoResultFound , exc . MultipleResultsFound ) as ex : <EOL> logger . info ( str ( ex ) ) <EOL> def getLeafSetting ( self , session , podId , deviceFamily ) : <EOL> try : <EOL> return session . query ( LeafSetting ) . filter_by ( pod_id = podId ) . filter_by ( deviceFamily = deviceFamily ) . one ( ) <EOL> except ( exc . NoResultFound ) as ex : <EOL> logger . info ( str ( ex ) ) <EOL> def getConnectedInterconnectIFDsFilterFakeOnes ( self , session , device ) : <EOL> '''<STR_LIT>''' <EOL> interconnectPorts = session . query ( InterfaceDefinition ) . filter ( InterfaceDefinition . device_id == device . id ) . filter ( InterfaceDefinition . peer != None ) . filter ( ( InterfaceDefinition . role == '<STR_LIT>' ) | ( InterfaceDefinition . role == '<STR_LIT>' ) ) . order_by ( InterfaceDefinition . sequenceNum ) . all ( ) <EOL> ports = [ ] <EOL> for port in interconnectPorts : <EOL> if device . family != '<STR_LIT>' and '<STR_LIT>' in port . name : <EOL> continue <EOL> ports . append ( port ) <EOL> return ports <EOL> class Dao ( AbstractDao ) : <EOL> def _getDbUrl ( self ) : <EOL> from propLoader import OpenClosProperty <EOL> return OpenClosProperty ( ) . getDbUrl ( ) </s>
