Back to index

nordugrid-arc-nox  1.1.0~rc6
stringstore.py
Go to the documentation of this file.
00001 import arc
00002 import copy, os, base64
00003 
00004 from arcom.store.picklestore import PickleStore
00005 
00006 from arcom.logger import Logger
00007 log = Logger(arc.Logger(arc.Logger_getRootLogger(), 'arcom.StringStore'))
00008 
00009 class StringStore(PickleStore):
00010     """ Class for storing string objects. """
00011 
00012     def __init__(self, storecfg, non_existent_object = {}):
00013         """ Constructor of PickleStore.
00014 
00015         PickleStore(storecfg)
00016 
00017         'storecfg' is an XMLNode with a 'DataDir'
00018         'non_existent_object' will be returned if an object not found
00019         """
00020         PickleStore.__init__(self, storecfg, non_existent_object)
00021         log.msg(arc.VERBOSE, "StringStore constructor called")
00022         log.msg(arc.VERBOSE, "datadir:", self.datadir)
00023 
00024     def get(self, ID):
00025         """ Returns the object with the given ID.
00026 
00027         get(ID)
00028 
00029         'ID' is the ID of the requested object.
00030         If there is no object with this ID, returns the given non_existent_object value.
00031         """
00032         try:
00033             # generates a filename from the ID
00034             # then use pickle to load the previously serialized data
00035             return eval(file(self._filename(ID),'rb').read())
00036         except IOError:
00037             # don't print 'file not found' if there is no such ID
00038             pass
00039         except EOFError:
00040             # TODO: find out what causes this problem
00041             pass
00042         except:
00043             # print whatever exception happened
00044             log.msg()
00045             log.msg(arc.ERROR, "filename:", self._filename(ID))
00046         # if there was an exception, return the given non_existent_object
00047         return copy.deepcopy(self.non_existent_object)
00048 
00049     def set(self, ID, object):
00050         """ Stores an object with the given ID..
00051 
00052         set(ID, object)
00053 
00054         'ID' is the ID of the object
00055         'object' is the object itself
00056         If there is already an object with this ID it will be overwritten completely.
00057         """
00058         if not ID:
00059             raise Exception, 'ID is empty'
00060         try:
00061             # generates a filename from the ID
00062             fn = self._filename(ID)
00063             tmp_fn = self._tmpfilename(ID)
00064             # if 'object' is empty, don't make file
00065             if object:
00066                 # serialize the given list into tmp_fn
00067                 f = file(tmp_fn,'wb')
00068                 f.write(str(object))
00069                 f.close()
00070                 # try to rename the file
00071                 try:
00072                     os.rename(tmp_fn,fn)
00073                 except:
00074                     # try to create parent dir first, then rename the file
00075                     os.mkdir(os.path.dirname(fn))
00076                     os.rename(tmp_fn,fn)
00077             elif os.path.isfile(fn):
00078                 # object empty, file is not needed anymore
00079                 os.remove(fn)
00080         except:
00081             log.msg()