Changeset 2559:8d4552168c39 in livinglogic.python.xist

Show
Ignore:
Timestamp:
06/28/06 18:29:30 (14 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Add setuptools to the list of requirements.

Update NEWS items.

parsed() can now return a different node (This is used by specials.url)

The PI specials.url has been added that does the same URL handling as
xsc.URLAttr.

Unused stuff (encode, Queue, Esc*Text) has been removed from presenters.
NormalPresenter? has been dropped.

Fixed a small bug in CodePresenter?.presentProcInst().

Exceptions and warning no longer have to output the node location
themselves as it is included in the normal repr() output.

Fixed a few str() methods for warnings that still used astyle.color().

The Node methods repr() and asrepr() have been removed.

Renamed venom to detox.

Files:
2 added
1 removed
8 modified

Legend:

Unmodified
Added
Removed
  • INSTALL.xml

    r2459 r2559  
    1212<item>if you want want to use the method <method>asText</method> to convert &html; to 
    1313plain text: <link href="http://w3m.sourceforge.net/">w3m</link>,</item> 
     14<item>if you want to install this package as an egg: 
     15<link href="http://peak.telecommunity.com/DevCenter/setuptools">setuptools</link>.</item> 
    1416<item>if you want to run the test suite: <link href="http://codespeak.net/py/current/doc/test.html">py.test</link>,</item> 
    1517<item>and a C compiler supported by <app>distutils</app>, if you want 
  • NEWS.xml

    r2536 r2559  
    11<?xml version='1.0' encoding='iso-8859-1'?> 
    2 <section><title>Changes in ?.? (released ?/?/2006)</title> 
    3 <ulist> 
     2<section><title>Changes in 2.14 (released 06/28/2006)</title> 
     3<ulist> 
     4<item>Namespaces for RSS 0.91, RSS 2.0 and Atom 1.0 have been added.</item> 
     5 
     6<item>A new namespace <module>ll.xist.ns.detox</module> has been added that 
     7is similar to <module>ll.toxic</module> but can be used to generate Python 
     8code instead of PL/SQL code. Using <module>detox</module> templates is about 
     950 times faster than using &xist; trees directly and about 10 times faster than 
     10<link href="http://kid.lesscode.org/">Kid</link>.</item> 
     11 
     12<item>Presenters are now compatible to <link href="http://ipython.scipy.org/">IPython</link>'s 
     13ipipe modules. This means that you can browse &xist; trees interactively 
     14if you have IPython installed.</item> 
     15 
     16<item>A new processing instruction <class>ll.xist.ns.specials.url</class> 
     17has been added that does the same &url; transformation as 
     18<class>ll.xist.xsc.URLAttr</class> does.</item> 
     19 
    420<item>On publishing <class>ll.xist.ns.html.html</class> now only adds a 
    521<lit>lang</lit> and <lit>xml:lang</lit> attribute, if neither of them exists.</item> 
    6 <item>Namespaces for RSS 0.91, RSS 2.0 and Atom 1.0 have been added.</item> 
    7 <item>A new namespace <module>ll.xist.ns.venom</module> has been added that 
    8 is similar to <module>ll.toxic</module> but can be used to generate Python 
    9 code instead of PL/SQL code. Using <module>venom</module> templates is about 
    10 50 times faster than using &xist; trees directly and about 10 times faster than 
    11 <link href="http://kid.lesscode.org/">Kid</link>.</item> 
     22 
     23<item><module>setuptools</module> is now supported for installation.</item> 
    1224</ulist> 
    1325</section> 
  • src/ll/xist/ns/specials.py

    r2520 r2559  
    1919import sys, types, datetime 
    2020 
     21from ll import url as url_ 
    2122from ll.xist import xsc, parsers, sims 
    2223 
     
    187188 
    188189 
     190class url(xsc.ProcInst): 
     191    """ 
     192    <class>url</class> is a processing instruction containing an &url;. 
     193    On publishing it will be replaced by an &url; that is relative to the base 
     194    &url; of the publisher. 
     195    """ 
     196    def parsed(self, parser, start=None): 
     197        return self.__class__(unicode(parser.base/self.content)) 
     198 
     199    def publish(self, publisher): 
     200        yield publisher.encodetext(unicode(url_.URL(self.content).relative(publisher.base))) 
     201 
     202 
    189203# Control characters (not part of HTML) 
    190204class lf(xsc.CharRef): "line feed"; codepoint = 10 
  • src/ll/xist/parsers.py

    r2543 r2559  
    541541                            pass 
    542542                        else: 
    543                             attrnode.parsed(self) 
     543                            attrnode = attrnode.parsed(self) 
    544544                        attr = attr.next 
    545                     newnode.attrs.parsed(self) 
    546                     newnode.parsed(self, start=True) 
     545                    newnode.attrs = newnode.attrs.parsed(self) 
     546                    newnode = newnode.parsed(self, start=True) 
    547547                child = node.children 
    548548                while child is not None: 
     
    550550                    child = child.next 
    551551                if isinstance(node, xsc.Element): # if we did recognize the element, otherwise we're in a Frag 
    552                     newnode.parsed(self, start=False) 
     552                    newnode = newnode.parsed(self, start=False) 
    553553            elif node.type in ("text", "cdata"): 
    554554                newnode = xsc.Text(decode(node.content)) 
     
    727727                    if attrname is not None: # None means an illegal attribute 
    728728                        node[attrname] = attrvalue 
    729                         node[attrname].parsed(self) 
    730             node.attrs.parsed(self) 
    731             node.parsed(self, start=True) 
     729                        node[attrname] = node[attrname].parsed(self) 
     730            node.attrs = node.attrs.parsed(self) 
     731            node = node.parsed(self, start=True) 
    732732            self.__appendNode(node) 
    733733        # push new innermost element onto the stack, together with the list of prefix mappings to which we have to return when we leave this element 
     
    739739        currentelement = self._last() 
    740740        if currentelement is not None: # we're not in an bad element 
    741             currentelement.parsed(self, start=False) 
     741            currentelement.parsed(self, start=False) # ignore return value 
    742742            if self.validate: 
    743743                currentelement.checkvalid() 
     
    761761        if content: 
    762762            node = self.createText(content) 
    763             node.parsed(self) 
     763            node = node.parsed(self) 
    764764            last = self._last() 
    765765            if len(last) and isinstance(last[-1], xsc.Text): 
     
    773773    def comment(self, content): 
    774774        node = self.createComment(content) 
    775         node.parsed(self) 
     775        node = node.parsed(self) 
    776776        self.__appendNode(node) 
    777777        self.skippingwhitespace = False 
     
    783783            node = self.createProcInst(target, data) 
    784784            if node is not None: 
    785                 node.parsed(self) 
     785                node = node.parsed(self) 
    786786                self.__appendNode(node) 
    787787            self.skippingwhitespace = False 
     
    793793                self.characters(unichr(node.codepoint)) 
    794794            else: 
    795                 node.parsed(self) 
     795                node = node.parsed(self) 
    796796                self.__appendNode(node) 
    797797        self.skippingwhitespace = False 
  • src/ll/xist/presenters.py

    r2557 r2559  
    153153# style to be used for IDs in repr() 
    154154s4id = astyle.Style.fromenv("LL_XIST_STYLE_ID", "yellow:black") 
    155  
    156  
    157 class Queue(object): 
    158     """ 
    159     queue: write bytes at one end, read bytes from the other end 
    160     """ 
    161     def __init__(self): 
    162         self._buffer = "" 
    163  
    164     def write(self, chars): 
    165         self._buffer += chars 
    166  
    167     def read(self, size=-1): 
    168         if size<0: 
    169             s = self._buffer 
    170             self._buffer = "" 
    171             return s 
    172         else: 
    173             s = self._buffer[:size] 
    174             self._buffer = self._buffer[size:] 
    175             return s 
    176  
    177  
    178 def encode(encoding, *iterators): 
    179     queue = Queue() 
    180     writer = codecs.getwriter(encoding)(queue) 
    181     for iterator in iterators: 
    182         for text in iterator: 
    183             writer.write(text) 
    184             yield queue.read() 
    185  
    186  
    187 class EscInlineText(object): 
    188     ascharref = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f<>&" 
    189     ascolor   = "\x09" 
    190  
    191     @classmethod 
    192     def parts(cls, style, string): 
    193         for char in string: 
    194             if char in cls.ascolor: 
    195                 yield s4tab(char) 
    196             else: 
    197                 ascharref = char in cls.ascharref 
    198                 if not ascharref: 
    199                     try: 
    200                         char.encode(options.reprencoding) 
    201                     except: 
    202                         ascharref = True 
    203                 if ascharref: 
    204                     charcode = ord(char) 
    205                     try: 
    206                         entity = xsc.defaultPrefixes.charref(charcode) 
    207                     except LookupError: 
    208                         yield s4charrefname(u"&#", unicode(charcode), u";") 
    209                     else: 
    210                         yield s4entityname(u"&", unicode(entity.xmlname), u";") 
    211                 else: 
    212                     yield style(char) 
    213  
    214  
    215 class EscInlineAttr(EscInlineText): 
    216     ascharref = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f<>\"&" 
    217     ascolor   = "\x09\x0a" 
    218  
    219  
    220 class EscOutlineText(EscInlineText): 
    221     ascharref = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f<>&" 
    222     ascolor   = "" 
    223  
    224  
    225 class EscOutlineAttr(EscInlineText): 
    226     ascharref = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f<>\"&" 
    227     ascolor   = "" 
    228155 
    229156 
     
    306233        <par>present an <pyref module="ll.xist.xsc" class="Attr"><class>Attr</class></pyref> node.</par> 
    307234        """ 
    308  
    309  
    310 class NormalPresenter(Presenter): 
    311     def present(self, node): 
    312         self.inattr = 0 
    313         for part in node.present(self): 
    314             yield part 
    315  
    316     def presentText(self, node): 
    317         if self.inattr: 
    318             yield astyle.aunicode().join(EscOutlineAttr.parts(xsc.c4attrvalue, node.content)) 
    319         else: 
    320             yield astyle.aunicode().join(EscInlineText.parts(astyle.color, node.content)) 
    321  
    322     def presentFrag(self, node): 
    323         for child in node: 
    324             for part in child.present(self): 
    325                 yield part 
    326  
    327     def presentComment(self, node): 
    328         yield s4comment(u"<!--") 
    329         yield astyle.aunicode().join(EscOutlineText.parts(xsc.c4commenttext, node.content)) 
    330         yield s4comment(u"-->") 
    331  
    332     def presentDocType(self, node): 
    333         yield s4doctype(u"<!DOCTYPE ") 
    334         yield astyle.aunicode().join(EscOutlineText.parts(xsc.c4doctypetext, node.content)) 
    335         yield s4doctype(u">") 
    336  
    337     def presentProcInst(self, node): 
    338         yield s4procinst(u"<?") 
    339         yield node._str(fullname=True, xml=False, decorate=False) 
    340         yield s4procinst(u" ") 
    341         yield astyle.aunicode().join(EscOutlineText.parts(xsc.c4doctypetext, node.content)) 
    342         yield s4procinst(u"?>") 
    343  
    344     def presentAttrs(self, node): 
    345         self.inattr += 1 
    346         for (attrname, attrvalue) in node.iteritems(): 
    347             yield u" " 
    348             if isinstance(attrname, tuple): 
    349                 yield attrvalue._str(fullname=False, xml=False, decorate=False) 
    350             else: 
    351                 yield s4attrname(attrname) 
    352             yield u"=" 
    353             yield s4attr(u'"') 
    354             for part in attrvalue.present(self): 
    355                 yield part 
    356             yield s4attr(u'"') 
    357         self.inattr -= 1 
    358  
    359     def presentElement(self, node): 
    360         yield s4element(u"<") 
    361         yield node._str(fullname=True, xml=False, decorate=False) 
    362         for part in node.attrs.present(self): 
    363             yield part 
    364         if not len(node) and node.model and node.model.empty: 
    365             yield s4element(u"/>") 
    366         else: 
    367             yield s4element(u">") 
    368             for child in node: 
    369                 for part in child.present(self): 
    370                     yield part 
    371             yield s4element(u"</") 
    372             yield node._str(fullname=True, xml=False, decorate=False) 
    373             yield s4element(u">") 
    374  
    375     def presentEntity(self, node): 
    376         yield node._str(fullname=True, xml=False, decorate=True) 
    377  
    378     def presentNull(self, node): 
    379         yield node._str(fullname=True, xml=False, decorate=True) 
    380  
    381     def presentAttr(self, node): 
    382         for part in xsc.Frag.present(node, self): 
    383             yield part 
    384235 
    385236 
     
    874725 
    875726    def presentProcInst(self, node): 
    876         name = s4procinst(s4ns(node.__class__.__module__), ".", s4procinstname(node.__fullname__())) 
     727        name = s4procinst(s4ns(node.__class__.__module__), ".", s4procinsttarget(node.__fullname__())) 
    877728        yield Line(node, node.startloc, self._path[:], astyle.style_default(self._indent(), name, "(", s4procinstcontent(repr(self._text(node.content))), ")")) 
    878729 
  • src/ll/xist/sims.py

    r2520 r2559  
    5656 
    5757    def __str__(self): 
    58         s = "element %r" % self.node 
    59         if self.node.startloc is not None: 
    60             s += " at %s" % self.node.startloc 
    61         s += " may not contain element %r" % self.badnode 
    62         if self.badnode.startloc is not None: 
    63             s += " at %s" % self.badnode.startloc 
    64         return s 
     58        return "element %r may not contain element %r" % (self.node, self.badnode) 
    6559 
    6660 
     
    7670 
    7771    def __str__(self): 
    78         s = "element %r" % self.node 
    79         if self.node.startloc is not None: 
    80             s += " at %s" % self.node.startloc 
    81         s += " may not contain other elements" 
    82         if self.badnode.startloc is not None: 
    83             s += " (at %s)" % self.badnode.startloc 
    84         return s 
     72        return "element %r may not contain other elements" % self.node 
    8573 
    8674 
     
    9583 
    9684    def __str__(self): 
    97         s = "element %r" % self.node 
    98         if self.node.startloc is not None: 
    99             s += " at %s" % self.node.startloc 
    100         s += " may not contain text nodes" 
    101         if self.badnode.startloc is not None: 
    102             s += " (at %s)" % self.badnode.startloc 
    103         return s 
     85        return "element %r may not contain text nodes" % self.node 
    10486 
    10587 
  • src/ll/xist/xsc.py

    r2534 r2559  
    1919import sys, os, random, copy, warnings, new, cPickle 
    2020 
    21 from ll import misc, url, astyle 
     21from ll import misc, url as url_ 
    2222 
    2323 
     
    2828    iexec = None 
    2929     
     30try: 
     31    from IPython.Extensions import astyle 
     32except ImportError: 
     33    from ll import astyle 
     34 
    3035# IPython/ipipe support 
    3136try: 
     
    6267    elif isinstance(value, (list, tuple)): 
    6368        return Frag(*value) 
    64     elif isinstance(value, url.URL): 
     69    elif isinstance(value, url_.URL): 
    6570        return Text(value) 
    6671    else: 
     
    305310    def __str__(self): 
    306311        attr = self.attr 
    307         return "Attribute value %s not allowed for %s", (repr(str(attr)), attr._str(fullname=True, xml=False, decorate=False)) 
     312        return "Attribute value %r not allowed for %s" % (unicode(attr), attr._str(fullname=True, xml=False, decorate=False)) 
    308313 
    309314 
     
    504509 
    505510    def __str__(self): 
    506         return "an illegal object %r of type %s has been found in the XSC tree. The object will be ignored." % (self.object, type(self.object).__name__) 
     511        return "an illegal object %r of type %s has been found in the XIST tree." % (self.object, type(self.object).__name__) 
    507512 
    508513 
     
    545550 
    546551    def __str__(self): 
    547         return astyle.color("processing instruction with content %r is illegal, as it contains '?>'." % self.procinst.content) 
     552        return "processing instruction with content %r is illegal, as it contains '?>'." % self.procinst.content 
    548553 
    549554 
     
    560565 
    561566    def __str__(self): 
    562         return astyle.color("XML declaration with content %r is malformed." % self.procinst.content) 
     567        return "XML declaration with content %r is malformed." % self.procinst.content 
    563568 
    564569 
     
    635640 
    636641    def __str__(self): 
    637         s = "node %s" % self.node._str(fullname=True, xml=False, decorate=True) 
    638         if self.node.startloc is not None: 
    639             s += " at %r" % self.node.startloc 
    640         s += " outside of %s" % self.outerclass._str(fullname=True, xml=False, decorate=True) 
    641         return s 
     642        return "node %s outside of %s" % (self.node._str(fullname=True, xml=False, decorate=True), self.outerclass._str(fullname=True, xml=False, decorate=True)) 
    642643 
    643644 
     
    828829        return self 
    829830 
    830     def repr(self, presenter=None, **presenterargs): 
    831         """ 
    832         <par>Return a string representation of <self/>. When you don't pass in a 
    833         <arg>presenter</arg>, you'll get the default presentation. Else 
    834         <arg>presenter</arg> should be an instance of 
    835         <pyref module="ll.xist.presenters" class="Presenter"><class>ll.xist.presenters.Presenter</class></pyref> 
    836         (or one of the subclasses).</par> 
    837         """ 
    838         if presenter is None: 
    839             presenter = presenters.defaultpresenter(**presenterargs) 
    840         return presenter(self) 
    841  
    842     def asrepr(self, presenter=None, **presenterargs): 
    843         """ 
    844         <par>Return a string representation of <self/>. When you don't pass in a 
    845         <arg>presenter</arg>, you'll get the default presentation. Else 
    846         <arg>presenter</arg> should be an instance of 
    847         <pyref module="ll.xist.presenters" class="Presenter"><class>ll.xist.presenters.Presenter</class></pyref>.</par> 
    848         """ 
    849         return str(self.repr(presenter, **presenterargs)) 
    850  
    851831    @misc.notimplemented 
    852832    def present(self, presenter): 
     
    10241004        """ 
    10251005        <par>This method will be called by the parser <arg>parser</arg> once after 
    1026         <self/> is created by the parser. This is e.g. used by 
     1006        <self/> is created by the parser and must return the node that is to be 
     1007        put into the tree (in most cases this is <self/>, it's used e.g. by 
    10271008        <pyref class="URLAttr"><class>URLAttr</class></pyref> to incorporate 
    10281009        the base <pyref module="ll.url" class="URL"><class>URL</class></pyref> 
     
    10321013        Once at the beginning (i.e. before the content is parsed) with 
    10331014        <lit><arg>start</arg>==True</lit> and once at the end after parsing of 
    1034         the content is finished <lit><arg>start</arg>==False</lit>.</par> 
    1035         """ 
    1036         pass 
     1015        the content is finished <lit><arg>start</arg>==False</lit>. For the 
     1016        second call the return value will be ignored.</par> 
     1017        """ 
     1018        return self 
    10371019 
    10381020    def checkvalid(self): 
     
    10441026        the <pyref module="warnings">Python warning framework</pyref>.</par> 
    10451027        """ 
    1046         pass 
    10471028 
    10481029    @misc.notimplemented 
     
    21762157            csshandler = cssparsers.ParseHandler(ignorecharset=True) 
    21772158            value = csshandler.parseString(unicode(self), base=parser.base) 
    2178             self[:] = (value, ) 
     2159            return self.__class__(value) 
     2160        return self 
    21792161 
    21802162    def _publishattrvalue(self, publisher): 
     
    22072189 
    22082190    def parsed(self, parser, start=None): 
    2209         self[:] = utils.replaceInitialURL(self, lambda u: parser.base/u) 
     2191        return self.__class__(utils.replaceInitialURL(self, lambda u: parser.base/u)) 
    22102192 
    22112193    def _publishattrvalue(self, publisher): 
     
    22192201        instance (note that non-character content will be filtered out).</par> 
    22202202        """ 
    2221         return url.URL(Attr.__unicode__(self)) 
     2203        return url_.URL(Attr.__unicode__(self)) 
    22222204 
    22232205    def __unicode__(self): 
     
    22352217        if u.scheme == "root": 
    22362218            u.scheme = None 
    2237         u = url.URL(root)/u 
     2219        u = url_.URL(root)/u 
    22382220        return u 
    22392221 
  • test/test_xist.py

    r2467 r2559  
    305305        repr(node) 
    306306        for class_ in allpresenters: 
    307             presenter = class_() 
     307            presenter = class_(node) 
    308308            # do it multiple time, to make sure the presenter gets properly reset 
    309309            for i in xrange(3): 
    310                 node.asrepr(presenter) 
    311         for showlocation in (False, True): 
    312             for showpath in (False, True): 
    313                 presenter = presenters.TreePresenter(showlocation=showlocation, showpath=showpath) 
    314                 # do it multiple time, to make sure the presenter gets properly reset 
    315                 for i in xrange(3): 
    316                     node.asrepr(presenter) 
     310                list(presenter) 
     311                str(presenter) 
    317312 
    318313