def create_path(fullPath): try: os.mkdir(fullPath, 0o775) return True except: MsgUtils.msg("Cannot create %r" % fullPath) return False
def end_log(self): if self._logger != None: self._logger.info("ENDLOG LOG AT: %s" % time.ctime()) self._logger.end_log() self._logger = None self._logF = "" MsgUtils.msg("doLog is off, print statement after this should be in the console") self._prefs['globalPreference']['DoLog'] = False
def instanciate(self, d): if (JsonHelper._CLASSKEY not in d or JsonHelper._MODULEKEY not in d): # this is a basic dict return d # create an instance aclass = d[JsonHelper._CLASSKEY] amodule = d[JsonHelper._MODULEKEY] if (amodule != '__builtin__'): cmd = "import %s" % amodule try: exec(cmd) except: traceback.print_exc() MsgUtils.warning("cannot import %r" % amodule) MsgUtils.msg("Creating a generic class container") amodule = "tdata" aclass = "container" cmd = "%s.%s()" % (amodule, aclass) newInstance = None try: newInstance = eval(cmd) except: traceback.print_exc() MsgUtils.error("COMMAND %r" % cmd) MsgUtils.msg("class definition %r not found in %r" % (aclass, amodule)) assert newInstance != None # name value and typevalue if JsonHelper._NAMEKEY in d: newInstance.setName(d[JsonHelper._NAMEKEY]) if JsonHelper._VALTYPEKEY in d: newInstance.setTypeValue(d[JsonHelper._VALTYPEKEY]) if JsonHelper._VALKEY in d: newInstance.setValue(d[JsonHelper._VALKEY]) if JsonHelper._PROPKEY in d: newInstance.setVariableMember(d[JsonHelper._PROPKEY]) if JsonHelper._CHKEY in d: assert type(d[JsonHelper._CHKEY]) == dict for k, vn in list(d[JsonHelper._CHKEY].items()): chInstance = self.instanciate(vn) if JsonHelper.isContainerClass(chInstance): chInstance.setName(str(k)) newInstance.addChild(chInstance, False) else: MsgUtils.error("trouble reading %r" % type(chInstance)) return newInstance
def start_log(self,editor): from dsk.base.utils.log_utils import LogUtils as LogBase if self._logger != None: MsgUtils.msg("doLog is on") return MsgUtils.msg("doLog is on, print statement after this") MsgUtils.msg("will be redirect to the log area") logfile = os.path.join(self._userInfo.log_dir,self.appName) MsgUtils.msg("log location %r" % logfile) self._logger = LogBase() self._logF = "%s_%s.log" % (logfile,os.getpid()) logTag = self.appName + str(self.__LOGCOUNT) #self._logger.startLogFileRotate(logTag,self._logF,5,self) self._logger.startLogFile(logTag,self._logF,editor) self._logger.info("STARTING LOG %r AT: %s" % (self._logF,time.ctime())) self.__LOGCOUNT += 1 self._prefs['globalPreference']['DoLog'] = True
def LoadRecurElement(self, elmt, parentTask, maxLevel): creationList = [] while elmt != None: if elmt.nodeType == elmt.ELEMENT_NODE: # get Attr in a dict adict = {} self.getXmlAttr(elmt, adict) assert not (elmt.nodeName in type_helper.LIST_TYPE_NAME) assert not (elmt.nodeName == 'dict') if elmt.nodeName in type_helper.SIMPLE_TYPE_NAME: result = self.getKids(elmt, parentTask, maxLevel) #assert len(result) == 1 if len(result) == 1: res = result[0] assert len(res) == 3 if elmt.nodeName in type_helper.STRING_TYPE_NAME: if res[1] == '""' or res[1] == '""': v = "" # TESTING else: v = "%r" % res[1] creationList.append( [elmt.nodeName, v, self.NoName]) else: val = res[1].strip() creationList.append( [elmt.nodeName, val, self.NoName]) # property elif elmt.nodeName == self.PROPID or elmt.nodeName == self.PROPID_OLD: # must have type, name optional assert 'type' in adict # property belong to a # 'valid parent' list, dict.. name = self.NoName if 'name' in adict: name = adict['name'] if adict['type'] in type_helper.SIMPLE_TYPE_NAME: #those are suppose to have 1 # child only holding the value result = self.getKids(elmt, parentTask, maxLevel) if len(result) == 0: break assert len(result) == 1 #if len(result) == 1 res = result[0] assert len(res) == 3 if adict['type'] in type_helper.STRING_TYPE_NAME: if res == '""' or res[1] == '""': v = "" # TESTING else: v = "%s" % res[1] creationList.append([adict['type'], v, name]) else: val = res[1].strip() creationList.append([adict['type'], val, name]) elif adict['type'] in type_helper.LIST_TYPE_NAME: result = self.getKids(elmt, parentTask, maxLevel) if adict['type'] == type_helper.LIST_TYPE_NAME[0]: newone = [] elif adict['type'] == type_helper.LIST_TYPE_NAME[1]: newone = () else: MsgUtils.warning("trouble with the list type") for res in result: assert len(res) == 3 if res[0] in type_helper.SIMPLE_TYPE_NAME: val = None val = eval('%s(%s)' % (res[0], res[1])) if adict['type'] == type_helper.LIST_TYPE_NAME[ 0]: newone.append(val) elif adict[ 'type'] == type_helper.LIST_TYPE_NAME[ 1]: newone += (val, ) else: if res[0] == self.REFID: nref = self.refRead(res[1], parentTask) if nref != None: res[1] = nref else: MsgUtils.warning( "can't resolve dependance yet") if adict['type'] == type_helper.LIST_TYPE_NAME[ 0]: newone.append(res[1]) elif adict[ 'type'] == type_helper.LIST_TYPE_NAME[ 1]: newone += (res[1], ) creationList.append( ["%s" % adict['type'], newone, name]) elif adict['type'] == 'dict': newone = {} result = self.getKids(elmt, parentTask, maxLevel) # the result must be key value,... an even number assert not len(result) % 2 for index in range(0, len(result), 2): key = result[index] val = result[index + 1] assert len(key) == 3 assert len(val) == 3 kk = None vv = None if key[0] in type_helper.SIMPLE_TYPE_NAME: kk = eval('%s(%s)' % (key[0], key[1])) else: if key[0] == self.REFID: nref = self.refRead(key[1], parentTask) if nref != None: key[1] = nref else: MsgUtils.warning( "can't resolve dependance yet") kk = key[1] if val[0] in type_helper.SIMPLE_TYPE_NAME: #if len(val[1]) > 2 and val[1][0] = "'" #if val[0] in type_helper.STRING_TYPE_NAME: # vv = val[1] #else: vv = eval('%s(%s)' % (val[0], val[1])) else: vv = val[1] if val[0] == self.REFID: nref = self.refRead(vv, parentTask) if nref != None: vv = nref else: MsgUtils.warning( "can't resolve dependance yet") newone[kk] = vv creationList.append(["dict", newone, name]) elif adict['type'] == types.NoneType.__name__: creationList.append([adict['type'], None, name]) else: #if nothing else it must be an object result = self.getKids(elmt, parentTask, maxLevel) assert len(result) == 1 res = result[0] assert len(res) == 3 if res[0] == self.REFID: nref = self.refRead(res[1], parentTask) if nref != None: res[1] = nref else: MsgUtils.warning( "can't resolve dependance yet") creationList.append(["unknownType", res[1], name]) elif elmt.nodeName == self.CHILDID: assert parentTask != None if maxLevel == 0: break if maxLevel != -1: maxLevel -= 1 result = self.getKids(elmt, parentTask, maxLevel) for res in result: assert len(res) == 3 assert res[0] != self.REFID parentTask.addChild(res[1]) elif elmt.nodeName == self.FILEID: assert 'path' in adict path = adict['path'] insts = tdata.Tdata.ReadAsXml(path) for ist in insts: creationList.append(["unknowType", ist, self.NoName]) elif elmt.nodeName == self.REFID: assert 'path' in adict instance = tdata.defaultContainer() instance.pathname = adict['path'] nref = self.refRead(instance, parentTask) if nref != None: creationList.append(["unknownType", nref, self.NoName]) else: creationList.append( [self.REFID, instance, self.NoName]) elif elmt.nodeName == 'NoneType': creationList.append([noneTypeCast, None, self.NoName]) else: # It's an object that need to be instanciate assert 'class' in adict amodule = elmt.nodeName aclass = adict['class'] if (amodule != 'Tdata' and amodule != '__builtin__'): cmd = "import %s" % amodule try: exec(cmd) except: traceback.print_exc() MsgUtils.warning("cannot import %s" % elmt.nodeName) MsgUtils.msg("Creating a generic class container") amodule = "Tdata" aclass = "container" if 'construct' in adict: #cmd = "newInstance = %s" % adict['construct'] cmd = "%s" % adict['construct'] else: #cmd = "newInstance = %s.%s()" % (amodule,aclass) cmd = "%s.%s()" % (amodule, aclass) newInstance = None try: #exec(cmd,globals(), locals()) newInstance = eval(cmd) except: traceback.print_exc() MsgUtils.error("COMMAND %s" % cmd) MsgUtils.msg("class definition %s not found in %s" % (aclass, amodule)) assert newInstance != None if self.isContainerClass(newInstance): newInstance.updateStringAttribute(adict) self.addRead(newInstance) result = self.getKids(elmt, newInstance, maxLevel) if not aclass in type_helper.SOMEOTHERTYPE: for res in result: assert len(res) == 3 assert not res[2] == self.NoName if res[0] == self.REFID: nref = self.refRead(res[1], parentTask) if nref != None: res[1] = nref else: MsgUtils.warning( "can't resolve dependance yet") if res[0] == "bool": if res[1] == "False": cmd = "newInstance.%s = False" % res[2] else: cmd = "newInstance.%s = True" % res[2] elif res[0] in type_helper.SIMPLE_TYPE_NAME: cmd = "newInstance.%s = %s(res[1])" % (res[2], res[0]) else: cmd = "newInstance.%s = res[1]" % res[2] exec(cmd) else: if aclass == "Set": for res in result: assert len(res) == 3 val = eval('%s(%s)' % (res[0], res[1])) newInstance.add(val) creationList.append(['object', newInstance, self.NoName]) elif elmt.nodeType == elmt.TEXT_NODE: data = self.getTextValue(elmt) if data != "": sdata = data.strip() if len(sdata) > 0: creationList.append(["dType", sdata, self.NoName]) else: raise elif elmt.nodeType == elmt.CDATA_SECTION_NODE: data = self.getcData(elmt) if data != "": sdata = data.strip() if len(sdata) > 0: creationList.append(["CdType", data, self.NoName]) else: pass elmt = elmt.nextSibling return creationList