def addCodeFile(self, dct, pth=None, encoding=None): if pth is None: pth = self._srcFile if encoding is None: encoding = self._encoding # Get the associated code file, if any codePth = "%s-code.py" % os.path.splitext(pth)[0] if os.path.exists(codePth): try: codeContent = codecs.open(codePth, "r", encoding).read() codeDict = desUtil.parseCodeFile(codeContent) dct["importStatements"] = codeDict.pop("importStatements", "") desUtil.addCodeToClassDict(dct, codeDict) except Exception as e: print("Failed to parse code file:", e)
def addCodeFile(self, dct, pth=None, encoding=None): if pth is None: pth = self._srcFile if encoding is None: encoding = self._encoding # Get the associated code file, if any codePth = "%s-code.py" % os.path.splitext(pth)[0] if os.path.exists(codePth): try: codeContent = codecs.open(codePth, "r", encoding).read() codeDict = desUtil.parseCodeFile(codeContent) dct["importStatements"] = codeDict.pop("importStatements", "") desUtil.addCodeToClassDict(dct, codeDict) except StandardError, e: print "Failed to parse code file:", e
def xmltodict(xml, attsToSkip=[], addCodeFile=False, encoding=None): """Given an xml string or file, return a Python dictionary.""" parser = Xml2Obj(encoding=encoding) parser.attsToSkip = attsToSkip if eol in xml and "<?xml" in xml: isPath = False else: isPath = os.path.exists(xml) errmsg = "" if eol not in xml and isPath: # argument was a file xmlContent = codecs.open(xml, "r", encoding).read() if isinstance(xmlContent, str): xmlContent = xmlContent.encode(encoding) try: ret = parser.Parse(xmlContent) except expat.ExpatError as e: errmsg = _("The XML in '%(xml)s' is not well-formed and cannot be parsed: %(e)s") % locals() else: # argument must have been raw xml: try: ret = parser.Parse(xml) except expat.ExpatError as e: errmsg = _("An invalid XML string was encountered: %s") % e if errmsg: raise dabo.dException.XmlException(errmsg) if addCodeFile and isPath: # Get the associated code file, if any codePth = "%s-code.py" % os.path.splitext(xml)[0] if os.path.exists(codePth): try: codeContent = codecs.open(codePth, "r", encoding).read() codeDict = desUtil.parseCodeFile(codeContent) ret["importStatements"] = codeDict.pop("importStatements", "") desUtil.addCodeToClassDict(ret, codeDict) except Exception as e: print("Failed to parse code file:", e) return ret
# argument must have been raw xml: try: ret = parser.Parse(xml) except expat.ExpatError, e: errmsg = _("An invalid XML string was encountered: %s") % e if errmsg: raise dabo.dException.XmlException(errmsg) if addCodeFile and isPath: # Get the associated code file, if any codePth = "%s-code.py" % os.path.splitext(xml)[0] if os.path.exists(codePth): try: codeContent = codecs.open(codePth, "r", encoding).read() codeDict = desUtil.parseCodeFile(codeContent) ret["importStatements"] = codeDict.pop("importStatements", "") desUtil.addCodeToClassDict(ret, codeDict) except StandardError, e: print "Failed to parse code file:", e return ret def escQuote(val, noEscape=False, noQuote=False): """ Add surrounding quotes to the string, and escape any illegal XML characters. """ val = ustr(val) if noQuote: qt = '' else: qt = '"'