def test17(self): self.assertEqual(asUnicode(u'abc'),u'abc') self.assertEqual(asUnicode(b'abc'),u'abc') self.assertRaises(AttributeError,asUnicode,['abc']) self.myAssertRaisesRegex(AttributeError,r"asUnicode\(.*'list' object has no attribute 'decode'", asUnicode,['abc']) self.assertEqual(asUnicodeEx(u'abc'),u'abc') self.assertEqual(asUnicodeEx(b'abc'),u'abc') self.assertEqual(asUnicodeEx(123),u'123') self.assertEqual(asBytes(u'abc'),b'abc') self.assertEqual(asBytes(b'abc'),b'abc') self.assertRaises(AttributeError,asBytes,['abc']) self.myAssertRaisesRegex(AttributeError,"asBytes\(.*'list' object has no attribute 'encode'", asBytes,['abc'])
def __unicode__(self): if self.value is not None: return self.value L = [asUnicodeEx(l) for l in self.L] self.value = result = self.s.join(L) # eliminate possible reference loops self.L = None return result
def normalize(self, x): if self._test(x): return x try: return asUnicodeEx(x) except UnicodeError: raise ValueError("Can't convert to unicode: %r" % x)
def fix(tag): "Apply fixes to their descriptive markup" src = asUnicodeEx(tag) step1 = src.replace(u'\x82', u'é') step2 = unescape(step1) step3 = step2.encode('utf-8') return step3
def processParsed(self, elts): # added to allow messing with the parsed input if needed D = self.topDict = {} D["__prefix__"] = "" elts = self.clean_elts(elts) textout = self.processContent(elts, top_level=1) T = asUnicodeEx(textout) result = self.topLevelSubstitution % T # clean out the topDict for k in list(D.keys()): del D[k] # possible self reference self.topDict = {} return result
def translate(self, nodetuple, controller, overrides, containerdict=None): #we want to leave this node unchanged, but let children through (tagName, attrs, content, extra) = nodetuple chunks = ['<%s' % tagName] if attrs is not None: for key, value in list(attrs.items()): chunks.append('%s="%s"' % (key, value)) if content is None: chunks.append('/>') else: #must do children chunks.append('>') processedContent = self.MyProcessContent(content, controller, overrides, containerdict) chunks.append(asUnicodeEx(processedContent)) chunks.append('</%s>' % tagName) return ''.join(chunks)
def translate(self, nodetuple, controller, overrides, containerdict=None): # add own overrides if present if self.nodemappers: overrides = overrides.copy() overrides.update(self.nodemappers) (tagname, attdict, content, extra) = nodetuple #tagname = nodedict[0] #content = nodedict.get(1, None) if not attdict: attdict = {} sdict = attdict.copy() # for modification prefix = tagname # prefix must be set before processing children #print "processing", tagname #print containerdict if containerdict is not None: cprefix = containerdict["__prefix__"] if cprefix: prefix = "%s.%s" % (cprefix, prefix) else: cprefix = '' sdict['__prefix__'] = prefix sdict['__parent_prefix__'] = cprefix sdict['__depth__'] = prefix.count('.') sdict['__parent_depth__'] = sdict['__depth__']-1 sdict['__tag_depth__'] = (prefix+'.').count(tagname+'.') sdict['__mapnode__'] = self sdict['__controller__'] = controller sdict['__overrides__'] = overrides sdict['__containerdict__'] = containerdict sdict['__nodetuple__'] = nodetuple sdict["__tagname__"] = tagname sdict["__attrs__"] = LazyAttrs(attdict) defaults = self.defaults for name in list(defaults.keys()): if name not in sdict: sdict[name] = defaults[name] shadow = ShadowDict(tagname, sdict, containerdict) sdict['__shadow__'] = shadow #if content is None: stop if content is not None: pcontent = self.MyProcessContent(content, controller, overrides, shadow) sdict["__content__"] = pcontent # you can refer to TITLE.__content__ top = controller.topDict if containerdict is not None: containerdict[tagname+".__content__"] = pcontent # you can refer to PLAY.TITLE.__content__ top[prefix+".__content__"] = pcontent sstring = self.full_substitution_string if sstring is None: raise ValueError("no content allowed for %s" % ascii(tagname)) else: sstring = self.empty_substitution_string if sstring is None: raise ValueError("content required for %s" % ascii(tagname)) transforms = self.transforms for name in list(transforms.keys()): if name in sdict: t = transforms[name] if isinstance(t,SpecialTransform) or name=='__attrs__': sdict[name] = t(sdict,name) else: oldvalue = asUnicodeEx(sdict[name]) # IS STRING CONVERSION ALWAYS RIGHT? if isinstance(t,ExtendedTransform): sdict[name] = t(oldvalue, sdict) else: sdict[name] = t(oldvalue) try: result = shadow.substitute(sstring) #sstring % sdict except: raise ValueError("for tagname %s bad string %s for dict %s" % ( ascii(tagname), ascii(sstring), ascii(list(sdict.keys())))) return result
def __call__(self, sdict, name): return asUnicodeEx(sdict[name])
def preProcess(tree, nameSpace, caller=None): """Expands the parsed tree in the namespace and return new one. Returns a single tag-tuple in most cases, but a list of them if processing a loop node. """ from reportPackages.rlextra.radxml import xmlutils #expand this into a class with methods for each tag it handles. #then I can put logic tags in one and data access in another. tagName, attrs, children, extraStuff = tree #any attribute as $name becomes th value of name #tags might be nested in a loop, and if so then #each dictionary must be a fresh copy rather than # a pointer to the same dict newAttrs = attrs.copy() if attrs is not None else {} for key, value in list(newAttrs.items()): if isinstance(value,str) and value[0:1] == '$': newValue = eval(value[1:], nameSpace) newAttrs[key] = newValue attrs = newAttrs if tagName in TAG_LOOP: innerTxt = attrs[TAG_LOOP_INNER] outer = eval(attrs[TAG_LOOP_OUTER], nameSpace) dataSet = [] for row in outer: nameSpace['__loop_inner__'] = row rl_exec((innerTxt + " = __loop_inner__\n"), nameSpace) #at this point we're making lots of child nodes. # the attribute dictionary of each shold be a copy, not # a reference newChildren = processChildren(children, nameSpace) if newChildren is not None: dataSet.extend(newChildren) return dataSet elif tagName in TAG_ASSIGN: name = attrs[TAG_ASSIGN_NAME] valueStr = attrs[TAG_ASSIGN_VALUE] try: value = eval(valueStr, nameSpace) except SyntaxError: #must be a string value = valueStr nameSpace[name] = value return None elif tagName in TAG_SCRIPT: code = children[0] if not code.endswith('\n'): code += '\n' try: rl_exec(code, nameSpace) except SyntaxError: raise SyntaxError("Error with following script in xpreppy:\n\n%s" % code) return None elif tagName in TAG_EXPR: exprText = children[0] assert isinstance(exprText,strTypes), "expr can only contain strings" #attributes may affect escaping escape = attrs.get(u'escape', None) encoding = attrs.get(u'encoding',u'utf8') exprValue = eval(exprText, nameSpace) if isBytes(exprValue): exprValue = exprValue.decode(encoding) elif isUnicode(exprValue): pass else: exprValue = asUnicodeEx(exprValue) if escape in (u'CDATA',u'CDATAESCAPE'): exprValue = u'<![CDATA[%s]]>' % exprValue if escape==u'CDATA': return [exprValue] elif escape == u'off': return [asUnicodeEx(exprValue)] elif escape == u'unescape': return [xmlutils.unescape(exprValue, ENTITY_SUBSTITUTIONS_DRAWSTRING_DICT)] return [xmlEscape(exprValue)] elif tagName in TAG_IF: condText = attrs[u'cond'] yesOrNo = eval(condText, nameSpace) if yesOrNo: return processChildren(children, nameSpace) elif tagName in TAG_SWITCH: #two modes, with and without top level variable exprText = attrs.get(u'expr',u'') if exprText: expr = eval(exprText, nameSpace) selected = None for child in children: if isinstance(child,tuple): (childTagName, childAttrs, grandChildren, stuff) = child if childTagName in TAG_CASE: condition = childAttrs[u'condition'] if exprText: #check if it equals the value try: value = eval(condition, nameSpace) except NameError: value = condition # assume a string if (expr == value): selected = processChildren(grandChildren, nameSpace) break else: #they gave us a full condition, evaluate it yes = eval(condition, nameSpace) if yes: selected = processChildren(grandChildren, nameSpace) break elif childTagName in TAG_DEFAULT: selected = processChildren(grandChildren, nameSpace) break else: raise ValueError('%s tag may only contain these tags: ' % (TAG_SWITCH, ', '.join(TAG_CASE+TAG_DEFAULT))) return selected elif tagName in TAG_ACQUIRE: #all children will be data fetchers xacquire.acquireData(children, nameSpace) return None elif tagName in TAG_DOCLET: #pull out args needed to initialize dirName = attrs.get(u"baseDir", None) moduleName = attrs[u"module"] className = attrs[u"class"] dataStr = attrs.get(u"data", None) #load module, import and create it if caller == 'rml': from reportPackages.rlextra.rml2pdf.rml2pdf import _rml2pdf_locations locations = _rml2pdf_locations(dirName) else: locations = dirName m = recursiveImport(moduleName, locations) klass = getattr(m, className) docletObj = klass() #give it the data model if dataStr: dataObj = eval(dataStr, nameSpace) else: dataObj = nameSpace docletObj.setData(dataObj) #hide it in the tree so RML can see the object attrs[u'__doclet__'] = docletObj #return the tag otherwise unmodified return (tagName, attrs, children, extraStuff) else: newChildren = processChildren(children, nameSpace) return (tagName, attrs, newChildren, extraStuff)
def preProcess(tree, nameSpace, caller=None): """Expands the parsed tree in the namespace and return new one. Returns a single tag-tuple in most cases, but a list of them if processing a loop node. """ from rlextra.radxml import xmlutils #expand this into a class with methods for each tag it handles. #then I can put logic tags in one and data access in another. tagName, attrs, children, extraStuff = tree #any attribute as $name becomes th value of name #tags might be nested in a loop, and if so then #each dictionary must be a fresh copy rather than # a pointer to the same dict newAttrs = attrs.copy() if attrs is not None else {} for key, value in list(newAttrs.items()): if isinstance(value,str) and value[0:1] == '$': newValue = eval(value[1:], nameSpace) newAttrs[key] = newValue attrs = newAttrs if tagName in TAG_LOOP: innerTxt = attrs[TAG_LOOP_INNER] outer = eval(attrs[TAG_LOOP_OUTER], nameSpace) dataSet = [] for row in outer: nameSpace['__loop_inner__'] = row rl_exec((innerTxt + " = __loop_inner__\n"), nameSpace) #at this point we're making lots of child nodes. # the attribute dictionary of each shold be a copy, not # a reference newChildren = processChildren(children, nameSpace) if newChildren is not None: dataSet.extend(newChildren) return dataSet elif tagName in TAG_ASSIGN: name = attrs[TAG_ASSIGN_NAME] valueStr = attrs[TAG_ASSIGN_VALUE] try: value = eval(valueStr) except SyntaxError: #must be a string value = valueStr nameSpace[name] = value return None elif tagName in TAG_SCRIPT: code = children[0] if not code.endswith('\n'): code += '\n' try: rl_exec(code, nameSpace) except SyntaxError: raise SyntaxError("Error with following script in xpreppy:\n\n%s" % code) return None elif tagName in TAG_EXPR: exprText = children[0] assert isinstance(exprText,strTypes), "expr can only contain strings" #attributes may affect escaping escape = attrs.get(u'escape', None) encoding = attrs.get(u'encoding',u'utf8') exprValue = eval(exprText, nameSpace) if isBytes(exprValue): exprValue = exprValue.decode(encoding) elif isUnicode(exprValue): pass else: exprValue = asUnicodeEx(exprValue) if escape in (u'CDATA',u'CDATAESCAPE'): exprValue = u'<![CDATA[%s]]>' % exprValue if escape==u'CDATA': return [exprValue] elif escape == u'off': return [asUnicodeEx(exprValue)] elif escape == u'unescape': return [xmlutils.unescape(exprValue, ENTITY_SUBSTITUTIONS_DRAWSTRING_DICT)] return [xmlEscape(exprValue)] elif tagName in TAG_IF: condText = attrs[u'cond'] yesOrNo = eval(condText, nameSpace) if yesOrNo: return processChildren(children, nameSpace) elif tagName in TAG_SWITCH: #two modes, with and without top level variable exprText = attrs.get(u'expr',u'') if exprText: expr = eval(exprText, nameSpace) selected = None for child in children: if isinstance(child,tuple): (childTagName, childAttrs, grandChildren, stuff) = child if childTagName in TAG_CASE: condition = childAttrs[u'condition'] if exprText: #check if it equals the value try: value = eval(condition, nameSpace) except NameError: value = condition # assume a string if (expr == value): selected = processChildren(grandChildren, nameSpace) break else: #they gave us a full condition, evaluate it yes = eval(condition, nameSpace) if yes: selected = processChildren(grandChildren, nameSpace) break elif childTagName in TAG_DEFAULT: selected = processChildren(grandChildren, nameSpace) break else: raise ValueError('%s tag may only contain these tags: ' % (TAG_SWITCH, ', '.join(TAG_CASE+TAG_DEFAULT))) return selected elif tagName in TAG_ACQUIRE: #all children will be data fetchers xacquire.acquireData(children, nameSpace) return None elif tagName in TAG_DOCLET: #pull out args needed to initialize dirName = attrs.get(u"baseDir", None) moduleName = attrs[u"module"] className = attrs[u"class"] dataStr = attrs.get(u"data", None) #load module, import and create it if caller == 'rml': from rlextra.rml2pdf.rml2pdf import _rml2pdf_locations locations = _rml2pdf_locations(dirName) else: locations = dirName m = recursiveImport(moduleName, locations) klass = getattr(m, className) docletObj = klass() #give it the data model if dataStr: dataObj = eval(dataStr, nameSpace) else: dataObj = nameSpace docletObj.setData(dataObj) #hide it in the tree so RML can see the object attrs[u'__doclet__'] = docletObj #return the tag otherwise unmodified return (tagName, attrs, children, extraStuff) else: newChildren = processChildren(children, nameSpace) return (tagName, attrs, newChildren, extraStuff)