def _fromLdot(self, source): for key in self.keys(): del(self[key]) a = source edges = re.findall(r'(\d+->\d+) \[(.*)\]', a) #edges = re.findall(r'\d+->\d+', a) nodelines = re.findall(r'(\d+) \[ (.*)\]', a) nodes = list() ## Creating nodes for node in nodelines: nodeid = node[0] nodename = '' node_attribs = node[1].split(',') for node_attrib in node_attribs: key_val = node_attrib.split(' = ') if key_val[0]=='label': nodename = key_val[1] nodes.append([nodeid, nodename]) self.setAttrib(nodename, 'id', int(nodeid)) self.setAttrib(nodename, key_val[0].strip(), key_val[1].strip()) if self.getAttrib(nodename, 'bind')==None: self.setAttrib(nodename, 'bind', nodename) names = dict() for node in nodes: names[node[0]]=node[1] ## Creating edges for edge in edges: thisnodes = re.findall('\d+', edge[0]) key = names[thisnodes[0]] if key in self.keys(): self[key].append(names[thisnodes[1]]) else: self[key]=[names[thisnodes[1]]] eattrib = re.findall('(.*)=(.*)', edge[1])[0] ## IDs are converted to integer numbers if eattrib[0] == 'id': self.setEdgeAttrib((names[thisnodes[0]], names[thisnodes[1]]), eattrib[0], int(eattrib[1])) ## All the rest are left as strings else: self.setEdgeAttrib((names[thisnodes[0]], names[thisnodes[1]]), eattrib[0], eattrib[1]) for value in names.values(): if not(value in self.keys()): self[value]=[] for key in self.keys(): self[key] = sorted(self[key]) #os.remove('leafprot.lf.dot') log.send('Graph is: ' + str(self), 2)
def _seekforProt(self, gname): #extracts a variable with the given name from #the user module: it must contain the leaf protocol log.send('Looking for protocol in user module.', 3) hislocals=self._getUserLocals() if gname in hislocals.keys(): return hislocals[gname] else: raise NameError('I couldn''t bind "'+gname+ '" to any of your defined objects.')
def _generateAltGraphs(self): altpaths = self._generateAltPaths() groups = self._getNodeGroups().values() for path in altpaths: self._altgraphs[self._altPathToName(path)] = copy.deepcopy(self._graph) for group in groups: for node in group: if node not in path: self._altgraphs[self._altPathToName(path)].delNode(node) log.send('Alternative protocol ' + self._altPathToName(path) + ' is: ' + str(self._altgraphs[self._altPathToName(path)]), 2) return self._altgraphs
def load(self): if self.isDumped(): log.send(self.name() + ' is dumped in ' + self._path + ': loading it.') res = pickle.load(open(self._path, 'rb')) ## Now it should be a "self = res" but I currently don't ## trust that. self._timestamp = res._timestamp self._buildtime = res._buildtime self._fingerprint = res._fingerprint self.setDumpPath(res.getDumpPath()) self.setIsFile(res.isFile()) self.setValue(res.getValue()) else: log.send(self.name() + ' is not dumped.', 2)
def isDumped(self): log.send('Checking ' + str(self) + ' in file: ' + self._path, 3) if os.path.exists(self._path): log.send('Available ' + str(self), 3) return True log.send('Unavailable: ' + str(self), 3) return False
def _seekforMods(self): log.send('Looking for mods in user module.', 3) hislocals = self._getUserLocals() mymods=dict() nodenames = self._graph.getNodes() for nodename in nodenames: modname = nodename #self._graph.getAttrib(nodename, 'bind') if modname in hislocals.keys(): if not self._graph.getAttrib(nodename, 'bind') in hislocals: NameError('There is no '+self._graph.getAttrib(nodename, 'bind')+ ' in your Python module.') mymods[modname] = hislocals[self._graph.getAttrib(nodename, 'bind')] #mymods[modname] = hislocals[modname] else: raise NameError('I couldn''t bind '+modname+' to any of your defined objects.') #resmods = dict() #for mod in mymods.keys(): # resmods[mod]=mymods[mod] return mymods
def _makeFingerprint(self, obj): try: inspect.getsource(obj) log.send('Source got:', 3) log.send(inspect.getsource(obj), 3) return inspect.getsource(obj) except Exception: log.send('No source: passing object', 3) return obj
def update(self): if self.changed(): if self._fingerprint != None: log.send(self.name() + ' has changed: updating.') else: log.send(self.name() + ' is new: building fingerprint.') self.updateFingerprint() self.dump() else: log.send(self.name() + ' has not changed.', 2)
def _updateUserModule(self): import sys #the following is needed to sync the inspect module #with actual disk content import linecache linecache.checkcache() if self._modulename in sys.modules.keys(): log.send('Reloading user module.') self._usermodule = sys.modules[self._modulename] reload(self._usermodule) else: log.send('Loading user module.') self._usermodule = __import__(self._modulename) log.send('Your module is: ' + str(self._usermodule), 2) return self._usermodule
def setAttrib(self, node, attr, value): log.send('Setting ' + str(attr) + ' = ' + str(value) + ' for node: ' + str(node), 3) self._nodeattribs[(node, attr)] = value
def listProtocols(self): """Lists the names of all the protocols of the project.""" for protname in self.protocols: log.send('- ' + protname, 0) log.send(' ' + str(self.protocols[protname]._getGraph()))
def run(self): """Calls run on all protocols of the project.""" for protname in self.protocols.keys(): log.insertBreak() log.send('Running instance: ' + protname) self.protocols[protname].run()
def setValue(self, v): log.send('New value is: ' + str(v), 3) self._contents = v
def dump(self): if not self._dodump: log.send('Dumping is switched off, so skipping.', 2) return log.send('Dumping resource: ' + self._name ,2) log.send('object: ' + str(self), 3) log.send('value: ' + str(self._contents), 3) log.send('fingerprint: ' + str(self._fingerprint), 3) log.send('Dumping to file: ' + self._path, 2) pickle.dump(self, open(self._path, 'wb'))
def setIsFile(self, isit = True): log.send('isFile value: ' + str(isit), 3) self._isfile = isit
def __init__(self, name, path): log.send('Initializing resource ' + name + ' with path ' + path, 3) self._name=name self._path = path if self.isDumped(): self.load()
def updateFingerprint(self): self._fingerprint = self._makeFingerprint(self._contents) log.send('Fingerprint is: ' + str(self._fingerprint), 3)
def setDumpPath(self, path): log.send('Updating path: ' + str(path),2) self._path = path