def download(node): src = node.get("src") if src.startswith("file:"): src, search_path = get_path(node.get("src").replace("file:", "")) if not src: return (node, URLError("File not found: %s" % node.get("src"))) src = "file:" + src try: return (node, urlopen(src).read()) except Exception, e: return (node, e)
def download(node): src = node.get("src") if src.startswith("file:"): src, search_path = get_path( node.get("src").replace("file:", "")) if not src: return (node, URLError("File not found: %s" % node.get("src"))) src = "file:" + src try: return (node, urlopen(src).read()) except Exception, e: return (node, e)
def _open_document(self, uri): if hasattr(uri, "read"): return uri.read() elif isinstance(uri, basestring) and re.search("<(.+:)?scxml", uri): #"<scxml" in uri: self.filename = "<string source>" self.filedir = None return uri else: path, search_path = get_path(uri, self.filedir or "") if path: self.filedir, self.filename = os.path.split(os.path.abspath(path)) return open(path).read() else: msg = "No such file on the PYSCXMLPATH" self.logger.error(msg + ": '%s'" % uri) self.logger.error("PYTHONPATH: '%s'" % search_path) raise IOError(errno.ENOENT, msg, uri)
def _open_document(self, uri): if hasattr(uri, "read"): return uri.read() elif isinstance(uri, basestring) and re.search("<(.+:)?scxml", uri): #"<scxml" in uri: self.filename = "<string source>" self.filedir = None return uri else: path, search_path = get_path(uri, self.filedir or "") if path: self.filedir, self.filename = os.path.split( os.path.abspath(path)) return open(path).read() else: msg = "No such file on the PYSCXMLPATH" self.logger.error(msg + ": '%s'" % uri) self.logger.error("PYTHONPATH: '%s'" % search_path) raise IOError(errno.ENOENT, msg, uri)
def parseInvoke(self, node, parentId, n): invokeid = node.get("id") if not invokeid: # if not hasattr(node, "id_n"): node.id_n = 0 # else: node.id_n += 1 invokeid = "%s.%s.%s" % (parentId, n, self.invokeid_counter) self.invokeid_counter +=1 if node.get("idlocation"): self.dm[node.get("idlocation")] = invokeid invtype = self.parseAttr(node, "type", "scxml") src = self.parseAttr(node, "src") if src and src.startswith("file:"): newsrc, search_path = get_path(src.replace("file:", ""), self.dm.self.filedir or "") if not newsrc: #TODO: add search_path info to this exception. raise IOError(2, "File not found when searching the PYTHONPATH: %s" % src) src = "file:" + newsrc data = self.parseData(node, getContent=False) scxmlType = ["http://www.w3.org/TR/scxml", "scxml"] if invtype.strip("/") in scxmlType: inv = InvokeSCXML(data) contentNode = node.find(prepend_ns("content")) if contentNode != None: cnt = self.parseContent(contentNode) if isinstance(cnt, basestring): inv.content = cnt elif type(cnt) is list: #TODO: if len(cnt) > 0, we could throw exception. inv.content = etree.tostring(cnt[0]) elif self.datamodel == "ecmascript" and len(contentNode) > 0: # if cnt is a minidom object inv.content = etree.tostring(contentNode[0]) else: raise Exception("Error when parsing contentNode, content is %s" % cnt) elif invtype == "x-pyscxml-soap": inv = InvokeSOAP() elif invtype == "x-pyscxml-httpserver": inv = InvokeHTTP() else: raise NotImplementedError("The invoke type '%s' is not supported by the platform." % invtype) inv.invokeid = invokeid inv.parentSessionid = self.dm.sessionid inv.src = src inv.type = invtype inv.default_datamodel = self.default_datamodel finalizeNode = node.find(prepend_ns("finalize")) if finalizeNode != None and not len(finalizeNode): paramList = node.findall(prepend_ns("param")) namelist = filter(bool, map(lambda x: (x, x), node.get("namelist", "").split(" "))) paramMapping = [(param.get("name"), param.get("location")) for param in (p for p in paramList if p.get("location"))] def f(): for name, location in namelist + paramMapping: if name in self.dm["_event"].data: self.dm[location] = self.dm["_event"].data[name] inv.finalize = f elif finalizeNode != None: inv.finalize = partial(self.try_execute_content, finalizeNode) return inv
def parseInvoke(self, node, parentId, n): invokeid = node.get("id") if not invokeid: # if not hasattr(node, "id_n"): node.id_n = 0 # else: node.id_n += 1 invokeid = "%s.%s.%s" % (parentId, n, self.invokeid_counter) self.invokeid_counter += 1 if node.get("idlocation"): self.dm[node.get("idlocation")] = invokeid invtype = self.parseAttr(node, "type", "scxml") src = self.parseAttr(node, "src") if src and src.startswith("file:"): newsrc, search_path = get_path(src.replace("file:", ""), self.dm.self.filedir or "") if not newsrc: #TODO: add search_path info to this exception. raise IOError( 2, "File not found when searching the PYTHONPATH: %s" % src) src = "file:" + newsrc data = self.parseData(node, getContent=False) scxmlType = ["http://www.w3.org/TR/scxml", "scxml"] if invtype.strip("/") in scxmlType: inv = InvokeSCXML(dict(data)) contentNode = node.find(prepend_ns("content")) if contentNode != None: cnt = self.parseContent(contentNode) if isinstance(cnt, basestring): inv.content = cnt elif isinstance(cnt, list): #TODO: if len(cnt) > 0, we could throw exception. if len(cnt) == 0: raise InvokeError( "Line %s: The invoke content is empty." % node.sourceline) if cnt[0].xpath("local-name()") != "scxml": raise InvokeError( "Line %s: The invoke content is invalid for content: \n%s" % (node.sourceline, etree.tostring(cnt[0]))) inv.content = etree.tostring(cnt[0]) elif self.datamodel == "ecmascript" and isinstance( cnt, minidom.Element): # if cnt is a minidom object inv.content = cnt.toxml() else: raise Exception( "Error when parsing contentNode, content is %s" % cnt) elif invtype == "x-pyscxml-soap": inv = InvokeSOAP() elif invtype == "x-pyscxml-httpserver": inv = InvokeHTTP() else: raise NotImplementedError( "The invoke type '%s' is not supported by the platform." % invtype) inv.invokeid = invokeid inv.parentSessionid = self.dm.sessionid inv.src = src inv.type = invtype inv.default_datamodel = self.default_datamodel finalizeNode = node.find(prepend_ns("finalize")) if finalizeNode != None and not len(finalizeNode): paramList = node.findall(prepend_ns("param")) # namelist = filter(bool, map(lambda x: (x, x), node.get("namelist", "").split(" "))) namelist = [(x, x) for x in node.get('namelist', "").split(" ") if x] paramMapping = [(param.get("name"), param.get("location")) for param in (p for p in paramList if p.get("location"))] def f(): for name, location in namelist + paramMapping: if self.datamodel != "xpath" and name in self.dm[ "_event"].data: self.dm[location] = self.dm["_event"].data[name] elif len(self.dm["$_event/data/data[@id='%s']" % name]): self.dm[location.lstrip("$")] = self.dm[ "$_event/data/data[@id='%s']/text()|$_event/data/data[@id='%s']/*" % (name, name)] inv.finalize = f elif finalizeNode != None: inv.finalize = partial(self.try_execute_content, finalizeNode) return inv