[GRASS-SVN] r71849 - grass/trunk/tools
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Nov 26 13:39:25 PST 2017
Author: wenzeslaus
Date: 2017-11-26 13:39:24 -0800 (Sun, 26 Nov 2017)
New Revision: 71849
Modified:
grass/trunk/tools/mkhtml.py
Log:
doc: Python 3 compatibility (see #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.
Modified: grass/trunk/tools/mkhtml.py
===================================================================
--- grass/trunk/tools/mkhtml.py 2017-11-26 20:24:08 UTC (rev 71848)
+++ grass/trunk/tools/mkhtml.py 2017-11-26 21:39:24 UTC (rev 71849)
@@ -21,6 +21,7 @@
import string
import re
from datetime import datetime
+import locale
try:
# Python 2 import
@@ -34,6 +35,38 @@
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 @@
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 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 @@
index_titles = {}
-for key, name in index_names.iteritems():
+for key, name in index_names.items():
index_titles[key] = to_title(name)
# process footer
More information about the grass-commit
mailing list