Changeset 1604:e53326641eb0 in orange-bioinformatics for obiKEGG2/pathway.py


Ignore:
Timestamp:
03/22/12 13:16:00 (2 years ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Cache invalidation of kgml files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • obiKEGG2/pathway.py

    r1601 r1604  
    3232    KGML_URL_FORMAT = "http://www.genome.jp/kegg-bin/download?entry={pathway_id}&format=kgml" 
    3333     
    34     """ 
    35     TODO: Caching should be done using a Store interface. 
    36      
    37     """ 
    3834    def __init__(self, pathway_id, local_cache=None, connection=None): 
    3935        if pathway_id.startswith("path:"): 
     
    6056        """ Return an open kgml file for the pathway. 
    6157        """ 
     58        from datetime import datetime, timedelta 
     59        valid = False 
    6260        local_filename = os.path.join(self.local_cache, self.pathway_id + ".xml") 
    63          
    64         if not os.path.exists(local_filename): 
     61        if os.path.exists(local_filename): 
     62            mtime = os.stat(local_filename).st_mtime 
     63            mtime = datetime.fromtimestamp(mtime) 
     64            now = datetime.now() 
     65            if conf.params["cache.invalidate"] == "always": 
     66                valid = False 
     67            elif conf.params["cache.invalidate"] == "session": 
     68                valid = (now - mtime) < (now - caching._SESSION_START) 
     69            elif conf.params["cache.invalidate"] == "daily": 
     70                valid = (now - mtime) < timedelta(1) 
     71            elif conf.params["cache.invalidate"] == "weekly": 
     72                valid = (now - mtime) < timedelta(7) 
     73            else: 
     74                valid = False 
     75         
     76        if not valid: 
    6577            url = self.KGML_URL_FORMAT.format(pathway_id=self.pathway_id) 
    6678            s = urllib2.urlopen(url) 
    6779            contents = s.read() 
     80             
    6881            with open(local_filename, "wb") as f: 
    6982                f.write(contents) 
Note: See TracChangeset for help on using the changeset viewer.