Changeset 565


Ignore:
Timestamp:
28/01/16 10:48:09 (3 years ago)
Author:
mjuckes
Message:

cleaned code structure with addLatest() method

Location:
hdldemo/trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • hdldemo/trunk/hdldemo.py

    r564 r565  
    55 -v: print version; 
    66 -t: run a test; 
    7  -f <file name>: examine file, print path to replacement if this file is obsolete, print path to sibling files. 
     7 -f <file name>: examine file, print path to replacement if this file is obsolete, print path to sibling files (or replacements). 
    88 -id <tracking id>: examine handle record of tracking id. 
    99""" 
     
    2626   
    2727  def __init__(self, hdlDict, k='values'): 
     28    """Obsolete class to parse handle metadat ... replaced by pyhandle class""" 
    2829    self.h = hdlDict 
    2930    self.d = {} 
     
    3940  dh = dummyHandles() 
    4041  def __init__(self,hdl,url=None): 
     42     """Class to retrieve a handle .. optionally to retrieve from test data. 
     43     Still needs some error handling based on 'responseCode'.""" 
    4144 
    4245     if hdl[:5] == 'xxxxx': 
     
    4952 
    5053  def fetch(self,url): 
    51      ##print url 
    5254 
    5355     try: 
     
    6163       assert k in msg, 'Required key %s not found: %s' % (k, str( msg.keys() ) ) 
    6264 
    63      ###print ( 'url read ok' ) 
    6465     self.msg = msg 
    6566 
    6667class GHandles(object): 
    6768  def __init__(self): 
     69    """Class to manage a local cache of retrieved handles, to avoid repeat http requests""" 
    6870    self.hh = {} 
    6971  def __call__(self,hdl): 
     
    7274    return self.hh[hdl] 
    7375 
    74 ghandles = GHandles() 
    7576 
    7677class pyhandle(object): 
     
    8586    g = ghandles( self.REC_id ) 
    8687    for r in g.msg['values']: 
    87         if str(r['type']) in ['replaces', 'replacedBy', 'isReplacedBy','parent']: 
     88        if str(r['type']) in ['replaces', 'replacedBy', 'isReplacedBy','parent','replaced_by']: 
    8889          self.__dict__[r['type']] = pyhandle( r['data']['value'] ) 
    8990 
     
    102103      self.__dict__['filename'] = '__unnamed__' 
    103104 
    104   def getSiblings(self): 
     105  def addLatest(self): 
     106    """Retrieve handle records for replacements until a current file is found.""" 
     107    if not self.obsolete: 
     108      return 
     109    self.replaced_by.get() 
     110    self.replacements=[self.replaced_by,] 
     111    while self.replacements[-1].obsolete: 
     112        self.replacements.append( self.replacements[-1].replaced_by.get() ) 
     113    self.latest = self.replacements[-1] 
     114 
     115  def addSiblings(self): 
    105116     if self.aggregation_level != 'file': 
    106117       print 'No known siblings .....' 
     
    156167    self.p = pyhandle(  thisid ) 
    157168    self.p.get() 
    158     self.p.parent.get() 
    159169    if self.p.obsolete: 
    160170      print '******* OBSOLETE FILE ******' 
    161       this = self.p 
    162       while this.obsolete: 
    163         this.replaced_by.get() 
    164         print 'REPLACED BY: ',this.replaced_by.URL, this.replaced_by.REC_id 
    165         this = this.replaced_by 
     171      self.p.addLatest() 
     172      for this in self.p.replacements: 
     173        print 'REPLACED BY: ',this.URL, this.REC_id 
    166174    else: 
    167175      print '****** File is current' 
    168176 
    169     self.p.getSiblings() 
    170     print '------- SIBLINGS -------' 
    171     for c in self.p.siblings: 
     177    if 'parent' not in self.p.__dict__: 
     178      print 'No parent' 
     179    else: 
     180      self.p.parent.get() 
     181      print 'Parent: %s' % self.p.parent.REC_id 
     182      self.p.addSiblings() 
     183      print '------- SIBLINGS -------' 
     184      for c in self.p.siblings: 
    172185        c.get() 
    173186        if c.obsolete: 
    174           while c.obsolete: 
    175             c.replaced_by.get() 
    176             print '>>>', c.replaced_by.URL 
     187          c.addLatest() 
     188          print '>>>', c.latest.URL 
    177189        else: 
    178             print c.URL 
    179  
    180   def dumpFxx(self,fn, id=None): 
     190          print c.URL 
     191 
     192  def dumpF__obsolete(self,fn, id=None): 
    181193    if id == None: 
    182194      assert os.path.isfile( fn ), 'File %s not found' % fn 
     
    265277class ncHead(object): 
    266278  def __init__(self, fn): 
     279    """Read global attributes of a NetCDF file""" 
    267280    nc0 = ncq3.open( fn ) 
    268281    nc0.getDigest() 
     
    271284      self.ga[a.name] = a.value 
    272285       
    273        
     286ghandles = GHandles() 
    274287if __name__ == "__main__": 
    275288  import sys 
Note: See TracChangeset for help on using the changeset viewer.