Ignore:
Timestamp:
05/11/15 16:41:14 (5 years ago)
Author:
mjuckes
Message:

01.beta.10

File:
1 edited

Legend:

Unmodified
Added
Removed
  • CMIP6dreq/trunk/dreqPy/dreq.py

    r460 r465  
    1010from __init__ import DOC_DIR 
    1111 
     12 
     13blockSchemaFile = '%s/%s' % (DOC_DIR, 'BlockSchema.csv' ) 
     14 
     15def loadBS(bsfile): 
     16  ii = open( bsfile, 'r' ).readlines() 
     17  ll = [] 
     18  for l in ii: 
     19    ll.append( [x[1:-1] for x in string.strip(l).split('\t') ] ) 
     20  cc = collections.defaultdict( dict ) 
     21  for l in ll[3:]: 
     22    for i in range( len(ll[2]) -1 ): 
     23      cc[l[0]][ll[2][i+1]] = l[i+1] 
     24  return cc 
     25 
    1226class rechecks(object): 
     27  """Checks to be applied to strings""" 
    1328  def __init__(self): 
    1429    self.__isInt = re.compile( '-{0,1}[0-9]+' ) 
    1530 
    1631  def isIntStr( self, tv ): 
     32    """Check whether a string is a valid representation of an integer.""" 
    1733    if type( tv ) not in {type(''),type(u'')}: 
    1834      self.reason = 'NOT STRING' 
     
    5975           for a in self.__dict__.keys(): 
    6076             if a[0] != '_' or full: 
    61                if self._a[a].useClass == 'internalLink' and self._base._indexInitialised: 
     77               if hasattr( self._a[a], 'useClass') and self._a[a].useClass == 'internalLink' and self._base._indexInitialised: 
    6278                 if self.__dict__[a] in self._base._inx.uid: 
    6379                   targ = self._base._inx.uid[ self.__dict__[a] ] 
     
    8399 
    84100       def getHtmlLinkAttrStyle(self,a): 
     101         """Return a string containing a html fragment for a link to an attribute.""" 
    85102         if a in self.__class__._linkAttrStyle: 
    86103           return self.__class__._linkAttrStyle[a] 
     
    98115           for a in self.__dict__.keys(): 
    99116             if a[0] != '_': 
    100                if self._a[a].useClass == 'internalLink' and self._base._indexInitialised: 
     117               if hasattr( self._a[a], 'useClass') and self._a[a].useClass == 'internalLink' and self._base._indexInitialised: 
    101118                 if self.__dict__[a] == '__unset__': 
    102119                   m = '<li>%s: %s [missing link]</li>' % ( a, self.__dict__[a] ) 
     
    241258    self.nt__default = collections.namedtuple( 'deflt', ['defaults','glob'] ) 
    242259    self.ntf = collections.namedtuple( 'sect', ['header','attDefn','items'] ) 
     260    self.bscc = loadBS(blockSchemaFile) 
    243261 
    244262    self.coll = {} 
     
    269287    self.tableClasses = {} 
    270288    self.tableItems = collections.defaultdict( list ) 
     289## 
     290## this loads in some metadata, but not yet in a useful way. 
     291## 
     292    self._t0 = self.parsevcfg(None) 
     293    self.tt0 = {} 
     294    self._tableClass0 = self.itemClassFact( self._t0, ns=self.ns ) 
     295    for k in self.bscc: 
     296      self.tt0[k] = self._tableClass0(dict=self.bscc[k]) 
     297      setattr( self._tableClass0, '_%s' % k, self.tt0[k] ) 
     298 
    271299    for v in vl: 
    272300      t = self.parsevcfg(v) 
     
    350378     dreqItem._a = sectionInfo.attributes 
    351379     dreqItem._d = sectionInfo.defaults 
     380     if sectionInfo.attributes != None: 
     381        self.addAttributes(dreqItem, sectionInfo.attributes ) 
    352382     ##dreqItem.itemLabelMode = itemLabelMode 
    353383     ##dreqItem.attributes = attributes 
     
    355385     dreqItem.ns = ns 
    356386     return dreqItem 
     387 
     388  def addAttributes( self, thisClass, attrDict ): 
     389    for k in attrDict: 
     390      setattr( thisClass, '_%s' % k , attrDict[k] ) 
    357391          
    358392  def parsevcfg(self,v): 
    359393      """Parse a section definition element, including all the record attributes. The results are returned as a namedtuple of attributes for the section and a dictionary of record attribute specifications.""" 
    360       l = v.getAttribute( 'label' ) 
    361       t = v.getAttribute( 'title' ) 
    362       i = v.getAttribute( 'id' ) 
    363       ilm = v.getAttribute( 'itemLabelMode' ) 
    364       lev = v.getAttribute( 'level' ) 
    365       il = v.getElementsByTagName( 'rowAttribute' ) 
    366       vtt = self.nts( v.nodeName, l,t,i,ilm,lev ) 
    367       idict = {} 
    368       for i in il: 
    369         tt = self.parseicfg(i) 
    370         idict[tt.label] = tt 
     394      if v == None: 
     395        vtt = self.nts( '__core__', 'CoreAttributes', 'Core Attributes', '00000000', 'def', '0' ) 
     396        idict = {'label':None, 'title':None, 'id':None, 'itemLabelMode':None, 'level':None } 
     397      else: 
     398        l = v.getAttribute( 'label' ) 
     399        t = v.getAttribute( 'title' ) 
     400        i = v.getAttribute( 'id' ) 
     401        ilm = v.getAttribute( 'itemLabelMode' ) 
     402        lev = v.getAttribute( 'level' ) 
     403        il = v.getElementsByTagName( 'rowAttribute' ) 
     404        vtt = self.nts( v.nodeName, l,t,i,ilm,lev ) 
     405        idict = {} 
     406        for i in il: 
     407          tt = self.parseicfg(i) 
     408          idict[tt.label] = tt 
    371409      deflt = self.nt__default( {}, '__unset__' ) 
    372410      return self.ntt( vtt, idict, deflt ) 
     
    395433  def __init__(self): 
    396434    self.a = collections.defaultdict( list ) 
     435 
    397436class index(object): 
    398437  """Create an index of the document. Cross-references are generated from attributes with class 'internalLink'.  
     
    423462    ## 
    424463      for ka in dreq[k].attDefn.keys(): 
    425         if dreq[k].attDefn[ka].useClass == 'internalLink': 
     464        if hasattr( dreq[k].attDefn[ka], 'useClass') and dreq[k].attDefn[ka].useClass == 'internalLink': 
    426465           irefdict[k].append( ka ) 
    427466 
     
    484523 
    485524class loadDreq(object): 
    486   def __init__(self,dreqXML=defaultDreqPath, configdoc=defaultConfigPath, useShelve=False ): 
     525  """Load in a vocabulary document. 
     526  dreqXML: full path to the XML document 
     527  configdoc: full path to associated configuration document 
     528  useShelve: flag to specify whether to retrieve data from cache (not implemented) 
     529  htmlStyles: dictionary of styling directives which influence structure of html page generates by the "makeHtml" method 
     530""" 
     531 
     532  def __init__(self,dreqXML=defaultDreqPath, configdoc=defaultConfigPath, useShelve=False, htmlStyles=None ): 
    487533    self.c = config( thisdoc=dreqXML, configdoc=configdoc, useShelve=useShelve) 
    488534    self.coll = self.c.get() 
     
    495541    dreqItemBase._inx = self.inx 
    496542    dreqItemBase._indexInitialised = True 
    497     dreqItemBase._htmlStyle['CMORvar'] = {'getIrefs':['requestVar']} 
    498     dreqItemBase._htmlStyle['requestVarGroup'] = {'getIrefs':['requestVar','requestLink']} 
    499     dreqItemBase._htmlStyle['var'] = {'getIrefs':['CMORvar']} 
    500     dreqItemBase._htmlStyle['objective'] = {'getIrefs':['objectiveLink']} 
    501     dreqItemBase._htmlStyle['requestLink'] = {'getIrefs':['objectiveLink','requestItem']} 
    502     dreqItemBase._htmlStyle['exptgroup'] = {'getIrefs':['__all__']} 
    503     dreqItemBase._htmlStyle['experiment'] = {'getIrefs':['__all__']} 
    504     dreqItemBase._htmlStyle['mip'] = {'getIrefs':['__all__']} 
    505     dreqItemBase._htmlStyle['remarks'] = {'getIrefs':['__all__']} 
     543## 
     544## load in additional styling directives 
     545## 
     546    if htmlStyles != None: 
     547      for k in htmlStyles: 
     548        dreqItemBase._htmlStyle[k] = htmlStyles[k] 
     549 
    506550##    dreqItemBase._htmlStyle['__general__'] = {'addRemarks':True} 
    507551 
     
    517561    return self.defaultItemLineStyle 
    518562 
    519   def makeHtml(self,odir='./html'): 
     563  def makeHtml(self,odir='./html', ttl0 = 'Data Request Index'): 
     564    """Generate a html view of the vocabularies, using the "__html__" method of the vocabulary item class to generate a 
     565page for each item and also generating index pages. 
     566    odir: directory for html files; 
     567    ttl0: Title for main index (in odir/index.html)""" 
     568 
    520569    for k in self.inx.uid.keys(): 
    521570      i = self.inx.uid[k] 
     
    526575      oo.close() 
    527576 
    528     ttl0 = 'Data Request Index' 
    529577    msg0 = ['<h1>%s</h1>' % ttl0, '<ul>',] 
    530578    ks = sorted( self.coll.keys() ) 
Note: See TracChangeset for help on using the changeset viewer.