changeset 47:e647f30cc08e

remove backwards compat code for python < 2.4
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 03 Aug 2008 20:59:44 +0200
parents c228aba7258c
children 547347034e65
files emeraldtree/ElementTree.py emeraldtree/HTMLTreeBuilder.py emeraldtree/SimpleXMLWriter.py emeraldtree/TidyTools.py
diffstat 4 files changed, 29 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/emeraldtree/ElementTree.py	Sun Jul 27 20:57:32 2008 +0200
+++ b/emeraldtree/ElementTree.py	Sun Aug 03 20:59:44 2008 +0200
@@ -1286,13 +1286,9 @@
         try:
             from xml.parsers import expat
         except ImportError:
-            try:
-                import pyexpat
-                expat = pyexpat
-            except ImportError:
-                raise ImportError(
-                    "No module named expat; use SimpleXMLTreeBuilder instead"
-                    )
+            raise ImportError(
+                "No module named expat; use SimpleXMLTreeBuilder instead"
+                )
         parser = expat.ParserCreate(encoding, "}")
         if target is None:
             target = TreeBuilder()
--- a/emeraldtree/HTMLTreeBuilder.py	Sun Jul 27 20:57:32 2008 +0200
+++ b/emeraldtree/HTMLTreeBuilder.py	Sun Aug 03 20:59:44 2008 +0200
@@ -50,31 +50,17 @@
 ##
 
 import htmlentitydefs
-import re, string, sys
+import re
 import mimetools, StringIO
+from HTMLParser import HTMLParser
 
 import ElementTree
 
 AUTOCLOSE = "p", "li", "tr", "th", "td", "head", "body"
 IGNOREEND = "img", "hr", "meta", "link", "br"
 
-if sys.version[:3] == "1.5":
-    is_not_ascii = re.compile(r"[\x80-\xff]").search # 1.5.2
-else:
-    is_not_ascii = re.compile(eval(r'u"[\u0080-\uffff]"')).search
+is_not_ascii = re.compile(eval(r'u"[\u0080-\uffff]"')).search
 
-try:
-    from HTMLParser import HTMLParser
-except ImportError:
-    from sgmllib import SGMLParser
-    # hack to use sgmllib's SGMLParser to emulate 2.2's HTMLParser
-    class HTMLParser(SGMLParser):
-        # the following only works as long as this class doesn't
-        # provide any do, start, or end handlers
-        def unknown_starttag(self, tag, attrs):
-            self.handle_starttag(tag, attrs)
-        def unknown_endtag(self, tag):
-            self.handle_endtag(tag)
 
 ##
 # ElementTree builder for HTML source code.  This builder converts an
@@ -127,7 +113,7 @@
             http_equiv = content = None
             for k, v in attrs:
                 if k == "http-equiv":
-                    http_equiv = string.lower(v)
+                    http_equiv = v.lower()
                 elif k == "content":
                     content = v
             if http_equiv == "content-type" and content:
@@ -145,7 +131,7 @@
         attrib = {}
         if attrs:
             for k, v in attrs:
-                attrib[string.lower(k)] = v
+                attrib[k.lower()] = v
         self.__builder.start(tag, attrib)
         if tag in IGNOREEND:
             self.__stack.pop()
--- a/emeraldtree/SimpleXMLWriter.py	Sun Jul 27 20:57:32 2008 +0200
+++ b/emeraldtree/SimpleXMLWriter.py	Sun Aug 03 20:59:44 2008 +0200
@@ -87,39 +87,28 @@
 # </pre>
 ##
 
-import re, sys, string
+import re
 
-try:
-    unicode("")
-except NameError:
-    def encode(s, encoding):
-        # 1.5.2: application must use the right encoding
-        return s
-    _escape = re.compile(r"[&<>\"\x80-\xff]+") # 1.5.2
-else:
-    def encode(s, encoding):
-        return s.encode(encoding)
-    _escape = re.compile(eval(r'u"[&<>\"\u0080-\uffff]+"'))
+def encode(s, encoding):
+    return s.encode(encoding)
 
-def encode_entity(text, pattern=_escape):
+def encode_entity(text, pattern=re.compile(ur'[&<>"\u0080-\uffff]+')):
     # map reserved and non-ascii characters to numerical entities
     def escape_entities(m):
         out = []
         for char in m.group():
             out.append("&#%d;" % ord(char))
-        return string.join(out, "")
+        return "".join(out)
     return encode(pattern.sub(escape_entities, text), "ascii")
 
-del _escape
-
 #
 # the following functions assume an ascii-compatible encoding
 # (or "utf-16")
 
-def escape_cdata(s, encoding=None, replace=string.replace):
-    s = replace(s, "&", "&amp;")
-    s = replace(s, "<", "&lt;")
-    s = replace(s, ">", "&gt;")
+def escape_cdata(s, encoding=None):
+    s = s.replace("&", "&amp;")
+    s = s.replace("<", "&lt;")
+    s = s.replace(">", "&gt;")
     if encoding:
         try:
             return encode(s, encoding)
@@ -127,12 +116,12 @@
             return encode_entity(s)
     return s
 
-def escape_attrib(s, encoding=None, replace=string.replace):
-    s = replace(s, "&", "&amp;")
-    s = replace(s, "'", "&apos;")
-    s = replace(s, "\"", "&quot;")
-    s = replace(s, "<", "&lt;")
-    s = replace(s, ">", "&gt;")
+def escape_attrib(s, encoding=None):
+    s = s.replace("&", "&amp;")
+    s = s.replace("'", "&apos;")
+    s = s.replace('"', "&quot;")
+    s = s.replace("<", "&lt;")
+    s = s.replace(">", "&gt;")
     if encoding:
         try:
             return encode(s, encoding)
@@ -166,7 +155,7 @@
             self.__write(">")
             self.__open = 0
         if self.__data:
-            data = string.join(self.__data, "")
+            data = "".join(self.__data)
             self.__write(escape_cdata(data, self.__encoding))
             self.__data = []
 
--- a/emeraldtree/TidyTools.py	Sun Jul 27 20:57:32 2008 +0200
+++ b/emeraldtree/TidyTools.py	Sun Aug 03 20:59:44 2008 +0200
@@ -19,7 +19,7 @@
 # utility.
 ##
 
-import glob, string, os, sys
+import glob, os, sys
 
 from ElementTree import ElementTree, Element
 
@@ -40,13 +40,13 @@
 
     if new_inline_tags:
         command.append("--new-inline-tags")
-        command.append(string.join(new_inline_tags, ","))
+        command.append(",".join(new_inline_tags))
 
     # FIXME: support more tidy options!
 
     # convert
     os.system(
-        "%s %s >%s.out 2>%s.err" % (string.join(command), file, file, file)
+        "%s %s >%s.out 2>%s.err" % (" ".join(command), file, file, file)
         )
     # check that the result is valid XML
     try:
@@ -110,9 +110,9 @@
     if body is None:
         return
 
-    if body.text and string.strip(body.text):
+    if body.text and body.text.strip():
         title = Element("h1")
-        title.text = string.strip(body.text)
+        title.text = body.text.strip()
         title.tail = "\n\n"
         body.insert(0, title)