Przeglądaj źródła

doc: Python 3 compatibility (see https://trac.osgeo.org/grass/ticket/2708)

Decode file content on input in 3, leave as in in 2.
Call constructor of parent class (Python 3 version needs to init a var).
Call less efficient items() in Python 2 (same code in both, although slower in 2, ok here).
This avoids compile (mkhtml) errors in most of the directories with Python 3.5.2 as python.


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@71849 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 7 lat temu
rodzic
commit
560a3ff50c
1 zmienionych plików z 39 dodań i 2 usunięć
  1. 39 2
      tools/mkhtml.py

+ 39 - 2
tools/mkhtml.py

@@ -21,6 +21,7 @@ import os
 import string
 import re
 from datetime import datetime
+import locale
 
 try:
     # Python 2 import
@@ -34,6 +35,38 @@ except:
     import urllib.parse as urlparse
 
 
+if sys.version_info.major == 2:
+    PY2 = True
+else:
+    PY2 = False
+
+
+if not PY2:
+    unicode = str
+
+
+def _get_encoding():
+    encoding = locale.getdefaultlocale()[1]
+    if not encoding:
+        encoding = 'UTF-8'
+    return encoding
+
+
+def decode(bytes_):
+    """Decode bytes with default locale and return (unicode) string
+
+    No-op if parameter is not bytes (assumed unicode string).
+
+    :param bytes bytes_: the bytes to decode
+    """
+    if isinstance(bytes_, unicode):
+        return bytes_
+    if isinstance(bytes_, bytes):
+        enc = _get_encoding()
+        return bytes_.decode(enc)
+    return unicode(bytes_)
+
+
 pgm = sys.argv[1]
 
 src_file = "%s.html" % pgm
@@ -119,7 +152,10 @@ def read_file(name):
         f = open(name, 'rb')
         s = f.read()
         f.close()
-        return s
+        if PY2:
+            return s
+        else:
+            return decode(s)
     except IOError:
         return ""
 
@@ -127,6 +163,7 @@ def read_file(name):
 def create_toc(src_data):
     class MyHTMLParser(HTMLParser):
         def __init__(self):
+            HTMLParser.__init__(self)
             self.reset()
             self.idx = 1
             self.tag_curr = ''
@@ -291,7 +328,7 @@ def to_title(name):
 
 
 index_titles = {}
-for key, name in index_names.iteritems():
+for key, name in index_names.items():
     index_titles[key] = to_title(name)
 
 # process footer