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

In This Package:

sourcecontext.py
Go to the documentation of this file.
00001 import logging
00002 log = logging.getLogger(__name__)
00003 from DybDbi import Site, DetectorId
00004 
00005 class SourceContext(object):
00006     """
00007     Associates a source DB tablename with a DBI context ``(site,subsite)`` in target DB
00008     **Replaces DcsTableName**
00009 
00010     #. more flexible and yet more enum constrained than DcsTableName 
00011     #. avoids poor assumption of logical table naming in source DB
00012 
00013     """
00014     def __init__(self, table, ksite, ksubsite , attjoin='id' , pfxjoin='P_', name='name' ):
00015         """
00016         :param table: table name in source DB, or colon delimited list of 2 tablenames when using autojoin
00017         :param ksite: attribute name of Site enum eg 'kDayabay' OR integer enum value
00018         :param ksubsite: attribute name of DetectorId enum eg 'kAD1' specifying the logical (not physical) detector OR integer enum value
00019         :param attjoin: attribute with which to join tables, only used when table param is a colon delimited list of tables
00020         :param pfxjoin: string used to distinguish attribute names between 2 joined tables
00021         :param name: unique name for this table (to facilitate multiple outputs from single DCS table)
00022 
00023         """
00024         katts = lambda enumcls:sorted(filter(lambda _:_[0]=='k',dir(enumcls)))
00025 
00026         site = ksite if type(ksite) == int else getattr( Site, ksite , None )
00027         if not site:
00028             msg = "ksite must be one of %s unlike %s " % ( katts(Site), ksite ) 
00029             log.fatal(msg)
00030             raise Exception(msg)
00031 
00032         subsite = ksubsite if type(ksubsite) == int else getattr( DetectorId, ksubsite )
00033         if not subsite:
00034             msg = "ksubsite must be one of %s unlike %s " % ( katts(DetectorId), ksubsite ) 
00035             log.fatal(msg)
00036             raise Exception(msg)
00037   
00038         self.table = table
00039         self.site = site
00040         self.subsite = subsite
00041         self.sitemask = site      ## always single site assumption
00042         self.attjoin = attjoin
00043         self.pfxjoin = pfxjoin
00044         if (name == 'name'):
00045             self.name = table
00046         else:
00047             self.name = name
00048 
00049         if self.isjoin:
00050             tabs = self.table.split(":")
00051             assert len(tabs) == 2, "join requires 2 colon delimited tablenames "
00052 
00053     isjoin   = property(lambda self:self.table.find(":")>-1 )
00054     kln      = property(lambda self:'Y%s' % self.name  )  
00055     ksite    = property(lambda self:'k%s' % Site.AsString(self.site) )
00056     ksubsite = property(lambda self:'k%s' % DetectorId.AsString(self.subsite) )
00057 
00058     def __repr__(self):
00059         return "%s('%s','%s','%s', attjoin='%s', pfxjoin='%s', name='%s')" % ( self.__class__.__name__ , self.table, self.ksite , self.ksubsite, self.attjoin, self.pfxjoin, self.name)  
00060 
00061     def __str__(self):
00062         return self.table
00063 
00064     def jbits(self):
00065         assert self.isjoin
00066         a,b = self.table.split(":")
00067         j  = self.attjoin
00068         tb = self.pfxjoin   ## tiebreaker prefix to avoid name collisions 
00069         xa = SourceContext( a , self.ksite, self.ksubsite )
00070         xb = SourceContext( b , self.ksite, self.ksubsite )
00071         return xa,xb,j,tb
00072 
00073 
00074 if __name__ == '__main__':
00075     logging.basicConfig(level=logging.DEBUG)
00076 
00077     sc = SourceContext( table="DBNS_SAB_TEMP" , ksite="kSAB" , ksubsite="kAD1" ) 
00078     assert sc.ksite    == "kSAB"
00079     assert sc.ksubsite == "kAD1"
00080     assert not sc.isjoin
00081     print sc
00082 
00083 
00084     sc = SourceContext( table="DBNS_AD1_HV:DBNS_AD1_HV_Pw" , ksite="kSAB" , ksubsite="kAD1" , attjoin='id' , pfxjoin='P_' ) 
00085     assert sc.ksite    == "kSAB"
00086     assert sc.ksubsite == "kAD1"
00087     assert sc.isjoin
00088     print sc
00089     print sc.jbits()
00090    
00091 
| 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