lib/shorturl.py
changeset 19 8d3ffd87cb0b
parent 14 4b5478da5850
child 20 6c774496bb00
equal deleted inserted replaced
18:46536daf9e04 19:8d3ffd87cb0b
    94 
    94 
    95         db[key] = obj.getObjInfo()
    95         db[key] = obj.getObjInfo()
    96 
    96 
    97     db['_id'] = id
    97     db['_id'] = id
    98     db.close()
    98     db.close()
       
    99 
       
   100 class DB (object) :
       
   101     def __init__ (self, read_only=True) :
       
   102         self.db = shelve.open('shorturls2', read_only and 'r' or 'c')
       
   103 
       
   104     def html_path (self, key, index) :
       
   105         type, dirpath, fname = self.db[key]
       
   106 
       
   107         if type == 'img' :
       
   108             fname += '.html'
       
   109         elif type == 'dir' :
       
   110             fname = ''
       
   111 
       
   112         if index :
       
   113             dirpath = '../%s' % dirpath
       
   114             
       
   115             if type == 'dir' and index > 1 : 
       
   116                 fname = 'index_%s.html' % (index - 1)
       
   117 
       
   118         return os.path.join(dirpath, fname)
       
   119    
       
   120     def image_info (self, key) :
       
   121         type, dirpath, fname = self.db[key]
       
   122 
       
   123         if type != 'img' :
       
   124             raise ValueError("%s is not an img" % key)
       
   125 
       
   126         return dirpath, fname
       
   127