Bladeren bron

g.html2man: use unique mod names, open, items, and try imports for Python 3 compatibility

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@70285 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 8 jaren geleden
bovenliggende
commit
82202359cb
4 gewijzigde bestanden met toevoegingen van 35 en 13 verwijderingen
  1. 1 1
      tools/g.html2man/Makefile
  2. 11 5
      tools/g.html2man/g.html2man.py
  3. 0 0
      tools/g.html2man/ggroff.py
  4. 23 7
      tools/g.html2man/html.py

+ 1 - 1
tools/g.html2man/Makefile

@@ -2,7 +2,7 @@ MODULE_TOPDIR = ../..
 
 include $(MODULE_TOPDIR)/include/Make/Other.make
 
-TARGETS := $(patsubst %.py,$(TOOLSDIR)/%.py,html.py groff.py g.html2man.py)
+TARGETS := $(patsubst %.py,$(TOOLSDIR)/%.py,ghtml.py ggroff.py g.html2man.py)
 
 default: $(TARGETS)
 

+ 11 - 5
tools/g.html2man/g.html2man.py

@@ -1,9 +1,15 @@
 #!/usr/bin/env python
 import sys
 import re
-from html import HTMLParser, HTMLParseError
-from groff import Formatter
-from StringIO import StringIO
+from ghtml import HTMLParser, HTMLParseError
+from ggroff import Formatter
+
+try:
+    # Python 2 str - bytes version
+    from StringIO import StringIO
+except ImportError:
+    # Python 3 str - unicode version
+    from io import StringIO
 
 entities = {
     'nbsp': " ",
@@ -32,7 +38,7 @@ def fix(content):
 def main():
     # parse HTML
     infile = sys.argv[1]
-    inf = file(infile)
+    inf = open(infile)
     p = HTMLParser(entities)
     for n, line in enumerate(inf):
         try:
@@ -63,7 +69,7 @@ def main():
     s = s.lstrip()
 
     # write groff
-    outf = file(sys.argv[2], 'w')
+    outf = open(sys.argv[2], 'w')
     outf.write(s)
     outf.close()
 

tools/g.html2man/groff.py → tools/g.html2man/ggroff.py


+ 23 - 7
tools/g.html2man/html.py

@@ -1,10 +1,26 @@
 from __future__ import (absolute_import, division, generators, nested_scopes,
                         print_function, unicode_literals, with_statement)
 import sys
-import HTMLParser as base
-import htmlentitydefs
 
-HTMLParseError = base.HTMLParseError
+try:
+    # Python 2 import
+    import HTMLParser as base
+    HTMLParseError = base.HTMLParseError
+except:
+    # Python 3 import
+    import html.parser as base
+    # TODO: this needs a better fix since HTMLParseError is actually
+    # used including its attributes, so that actually fails
+    # HTMLParseError is depreciated, parsing is not strict
+    HTMLParseError = Exception
+
+try:
+    # Python 3
+    from html.entities import entitydefs
+except ImportError:
+    # Python 2
+    from htmlentitydefs import entitydefs
+
 
 __all__ = ["HTMLParser", "HTMLParseError"]
 
@@ -35,12 +51,12 @@ head_content = ["title", "isindex", "base"]
 
 
 def setify(d):
-    return dict([(key, frozenset(val)) for key, val in d.iteritems()])
+    return dict([(key, frozenset(val)) for key, val in d.items()])
 
 
 def omit(allowed, tags):
     result = {}
-    for k, v in allowed.iteritems():
+    for k, v in allowed.items():
         for t in tags:
             if t in v:
                 v = v.union(allowed[t])
@@ -199,8 +215,8 @@ class HTMLParser(base.HTMLParser):
     def handle_entityref(self, name):
         if name in self.entities:
             self.handle_data(self.entities[name])
-        elif name in htmlentitydefs.entitydefs:
-            self.handle_data(htmlentitydefs.entitydefs[name])
+        elif name in entitydefs:
+            self.handle_data(entitydefs[name])
         else:
             sys.stderr.write("unrecognized entity: %s\n" % name)