00001 import os
00002
00003 import GBSConfig
00004 from GBSLogger import Log, logger
00005 from GBSIOHelper import GBSIOHelper
00006
00007 class GBSObject :
00008 """Base for all "role playing" objects.
00009
00010 This class the basis for persistancy.
00011
00012 This class provides no (directly) user callable methods
00013 although they can be used via the classes that inherit from it."""
00014
00015 def __init__(self,name,parent,model):
00016 self.__name = name
00017 self.__parent = parent
00018 self.__model = model
00019 Log(self,logger.SYNOPSIS,"Creating a " + self.GetType() + " named " + self.__name)
00020
00021
00022
00023
00024 if os.path.isfile(self.GetStoreLocation()):
00025 self.Read()
00026 else:
00027 parentDir = self.GetStoreLocation("parent_dir")
00028 if not os.path.isdir(parentDir): os.mkdir(parentDir,0770)
00029 self.Write()
00030
00031 def GetType(self): return "GBSObject"
00032
00033 def __repr__(self) :
00034 return self.GetType() + " named '" + self.__name + "' stored in " + self.GetStoreLocation()
00035
00036
00037
00038
00039 def GetName(self): return self.__name
00040 def GetModel(self): return self.__model
00041 def GetParent(self): return self.__parent
00042
00043 def GetStoreLocation(self,type="self"):
00044
00045 """Return storage location: parent directory, self or child directory.
00046
00047 type is one of "parent_dir" parent's child directory
00048 "self" object's state file
00049 "child_dir" directory for own children"""
00050
00051 loc = ""
00052 if self.__parent == None : loc = GBSConfig.GetConfig().GetValue("DataDirectory")
00053 else : loc = self.__parent.GetStoreLocation("parent_dir") + "/" + self.__parent.GetName()
00054 if type != "parent_dir": loc += "/" + self.GetName()
00055 if type == "self": loc += ".state"
00056 return loc
00057
00058
00059
00060 def MakeChildDirectory(self):
00061
00062 """If directory used to store child objects does not exist, create it"""
00063
00064 child_dir = self.GetStoreLocation("child_dir")
00065 if not os.path.isdir(child_dir):
00066 Log(self,logger.SYNOPSIS,"Creating directory for child objects:" + str(child_dir))
00067 if os.mkdir(child_dir,0755):
00068 Log(self,logger.ERROR,"Failed to create directory for child objects:" + str(child_dir))
00069
00070 def Read(self): self.__DoIO("r")
00071 def Write(self): self.__DoIO("w")
00072 def WriteFamily(self): self.Write()
00073
00074
00075
00076 def __DoIO(self,mode):
00077 Log(self,logger.SYNOPSIS,"Doing I/O mode:" + str(mode) + " on " + GBSObject.__repr__(self))
00078 ioh = GBSIOHelper(self.GetStoreLocation(),mode)
00079 self._DoMemberIO(ioh)
00080 ioh.Close()
00081
00082 def _DoMemberIO(self,ioh):
00083 self.__model = ioh("Model Name","s",self.__model)
00084 self.__name = ioh("Object Name","s",self.__name)
00085
00086
00087 def Rename(self,new_name):
00088 """Rename to new_name (does not perform I/O - that must be done by caller)."""
00089 self.__name = new_name
00090 return
00091