/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Public Attributes | Private Member Functions
Scraper::base::sa::SA Class Reference

List of all members.

Public Member Functions

def __init__
def __repr__
def kls
def reflect
def table
def add
def commit

Public Attributes

 dbconf
 cfg
 engine
 session
 meta
 classes

Private Member Functions

def _kls
def _map_properties
def _mapclass
def _join

Detailed Description

Manages SQLAlchemy DB connections, orchestrates reflection on 
tables, dynamic class creation and mapping from tables to classes.
These are all done lazily, when a class is requested via `.kls(xtn)` 

Definition at line 220 of file sa.py.


Constructor & Destructor Documentation

def Scraper::base::sa::SA::__init__ (   self,
  dbconf 
)

Definition at line 226 of file sa.py.

00227                                :
00228 
00229         cfg = ini_cfg(dbconf)
00230         url = "mysql://%(user)s:%(password)s@%(host)s/%(database)s" % cfg
00231         meta = MetaData()
00232         engine = create_engine( url , echo=False )
00233         Session.configure(bind=engine)
00234         session = Session()
00235 
00236         self.dbconf = dbconf        
00237         self.cfg = cfg
00238         self.engine = engine
00239         self.session = session
00240         self.meta = meta
00241         self.classes = {}


Member Function Documentation

def Scraper::base::sa::SA::__repr__ (   self)

Definition at line 242 of file sa.py.

00243                       :
00244         return "%s(\"%s\")# %r  " % (self.__class__.__name__, self.dbconf, dict(self.cfg, password="***"))

def Scraper::base::sa::SA::_kls (   self,
  xtn 
) [private]
Dynamic creation of class to represent table row instances 
with potentially table dependant base class 
Attributes `db` and `xtn` are planted to allow the class to 
know where it came from.

Definition at line 245 of file sa.py.

00246                        :
00247         """
00248         Dynamic creation of class to represent table row instances 
00249         with potentially table dependant base class 
00250         Attributes `db` and `xtn` are planted to allow the class to 
00251         know where it came from.
00252         """
00253         kln = xtn.kln                   
00254         Base = self.subbase( xtn )     ## from super ... use from instance of SA subclass, not directly from SA instance
00255         kls = type(kln,(Base,),dict(db=self,xtn=xtn))    
00256         return kls

def Scraper::base::sa::SA::_map_properties (   self,
  j,
  tb 
) [private]
:j param: the join instance
:tb param: tiebreaker prefix

when mapping to a join, there is tendency for property name collisions
in the mapped class, avoid collisions by using the key name which is
prefixed by table name 

Definition at line 257 of file sa.py.

00258                                      :
00259         """
00260         :j param: the join instance
00261         :tb param: tiebreaker prefix
00262 
00263         when mapping to a join, there is tendency for property name collisions
00264         in the mapped class, avoid collisions by using the key name which is
00265         prefixed by table name 
00266         """ 
00267         props = {}
00268         for k in j.c.keys():
00269             v = j.c.get(k)      # Column instance
00270             n = v.name
00271             if n in props:
00272                props["%s%s"% (tb,n)] = v
00273             else:
00274                props[n] = v 
00275         return props

def Scraper::base::sa::SA::_mapclass (   self,
  xtn 
) [private]
map single table or join to a dynamic class

Definition at line 276 of file sa.py.

00277                             :
00278         """
00279         map single table or join to a dynamic class
00280         """
00281         log.debug("mapclass for xtn %s " % xtn)
00282         kls = self._kls(xtn)
00283         if xtn.isjoin:
00284             j,tb = self._join( *xtn.jbits() ) 
00285             mapper( kls , j , properties=self._map_properties(j,tb) )
00286             kls.entity = j
00287         elif (xtn.name != xtn.table):
00288             tab = self.table( str(xtn))
00289             mapper ( kls, tab )
00290             name = xtn.name
00291             kls.entity = name 
00292         else:
00293             tab = self.table( str(xtn))
00294             mapper( kls , tab )
00295             kls.entity = tab
00296         self.classes[kls.__name__] = kls  

def Scraper::base::sa::SA::kls (   self,
  xtn 
)
Return mapped dynamic class from a xtn instance

Definition at line 297 of file sa.py.

00298                        :
00299         """Return mapped dynamic class from a xtn instance"""
00300 
00301         kln = xtn.kln                 # dynamic class name
00302         if kln not in self.classes:
00303             self._mapclass(xtn)
00304         return self.classes[kln]

def Scraper::base::sa::SA::_join (   self,
  tna,
  tnb,
  ja,
  tb 
) [private]
Returns the join of the 2 named tables

:tna param: coordinates of first table
:tnb param: coordinates of second table 
:ja param:  where clause join attribute, eg "id" or "SEQNO"
:tb param:  tiebreaker for name collisions

Definition at line 305 of file sa.py.

00306                                       :
00307         """
00308         Returns the join of the 2 named tables
00309 
00310         :tna param: coordinates of first table
00311         :tnb param: coordinates of second table 
00312         :ja param:  where clause join attribute, eg "id" or "SEQNO"
00313         :tb param:  tiebreaker for name collisions
00314 
00315         """ 
00316         a = self.table(str(tna))
00317         b = self.table(str(tnb))
00318         return join( a , b , getattr(a.c,ja) == getattr(b.c, ja) ), tb

def Scraper::base::sa::SA::reflect (   self,
  tn 
)
Reflect on the table, recording it in the meta

Definition at line 319 of file sa.py.

00320                           :
00321         """
00322         Reflect on the table, recording it in the meta
00323         """
00324         tt = Table( tn , self.meta,  autoload=True, autoload_with=self.engine)
00325         assert self.meta.tables[tn] == tt

def Scraper::base::sa::SA::table (   self,
  tn 
)
Return the sqlalchemy.schema.Table representation of a table, reflect upon
the table if not already done 

Definition at line 326 of file sa.py.

00327                         :
00328         """
00329         Return the sqlalchemy.schema.Table representation of a table, reflect upon
00330         the table if not already done 
00331         """
00332         tn = str(tn)
00333         if tn not in self.meta.tables:
00334             self.reflect(tn)
00335         return self.meta.tables[tn]

def Scraper::base::sa::SA::add (   self,
  obj 
)

Definition at line 336 of file sa.py.

00337                       :
00338         self.session.add( obj )

def Scraper::base::sa::SA::commit (   self)

Definition at line 339 of file sa.py.

00340                     :
00341         self.session.commit()  
00342 


Member Data Documentation

Definition at line 226 of file sa.py.

Definition at line 226 of file sa.py.

Definition at line 226 of file sa.py.

Definition at line 226 of file sa.py.

Definition at line 226 of file sa.py.

Definition at line 226 of file sa.py.


The documentation for this class was generated from the following file:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:50:03 for Scraper by doxygen 1.7.4