예제 #1
0
	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)
예제 #2
0
	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
예제 #3
0
파일: xmltodict.py 프로젝트: CarlFK/dabo
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
예제 #4
0
			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, 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 = ''
예제 #5
0
            ) % locals()
    else:
        # 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 = ''