Skip to content

Commit

Permalink
Fix html.py to work with Python 3.x
Browse files Browse the repository at this point in the history
Fix up running in non-UTF-8 environments too.

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
  • Loading branch information
dwmw2 committed Feb 1, 2019
1 parent ca7d284 commit 938fcd7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
14 changes: 2 additions & 12 deletions configure.ac
Expand Up @@ -936,18 +936,8 @@ AC_CHECK_HEADER([endian.h],
[AC_DEFINE([ENDIAN_HDR], [<sys/isa_defs.h>])])])])

build_www=yes
AC_PATH_PROGS(PYTHON, [python2 python], [], $PATH:/bin:/usr/bin)
if (test -n "${ac_cv_path_PYTHON}"); then
AC_MSG_CHECKING([that python is version 2.x])
if $PYTHON --version 2>&1 | grep "Python 2\." > /dev/null; then
AC_MSG_RESULT([yes])
AC_SUBST(PYTHON, ${ac_cv_path_PYTHON})
else
AC_MSG_RESULT([no])
AC_MSG_NOTICE([Python is not v2.x; not building HTML pages])
build_www=no
fi
else
AC_PATH_PROGS(PYTHON, [python3 python2 python], [], $PATH:/bin:/usr/bin)
if test -z "${ac_cv_path_PYTHON}"; then
AC_MSG_NOTICE([Python not found; not building HTML pages])
build_www=no
fi
Expand Down
41 changes: 22 additions & 19 deletions www/html.py
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Simple XML to HTML converter.
#
Expand All @@ -19,21 +19,24 @@
import socket
import time
import xml.sax
import commands
import codecs

reload(sys)
sys.setdefaultencoding('utf-8')
if sys.version_info >= (3,0):
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
else:
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

lookupdir = ''

# Print the usage information
def usage():
print "USAGE:"
print "html.py <-f -h file.xml>"
print " -d DIR use DIR as base directory for opening files"
print " -f write output to file.html (default is stdout)"
print " -h help"
print ("USAGE:")
print ("html.py <-f -h file.xml>")
print (" -d DIR use DIR as base directory for opening files")
print (" -f write output to file.html (default is stdout)")
print (" -h help")
return


Expand Down Expand Up @@ -98,9 +101,9 @@ def startElement(self, name, attrs):
return
elif name == "INCLUDE":
try:
fd = open(attrs.get('file'), 'r')
fd = codecs.open(attrs.get('file'), 'r', 'utf-8')
except:
fd = open(lookupdir + attrs.get('file'), 'r')
fd = codecs.open(lookupdir + attrs.get('file'), 'r', 'utf-8')
lines = fd.readlines()
fd.close()
for line in lines:
Expand All @@ -127,15 +130,15 @@ def startElement(self, name, attrs):

elif name == "br":
writeHtml("<br")
if attrs.getLength > 0:
if attrs.getLength() > 0:
names = attrs.getNames()
for name in names:
writeHtml(" " + name + "=\"" + attrs.get(name) + "\"")
writeHtml(" />")

else:
writeHtml("<" + name)
if attrs.getLength > 0:
if attrs.getLength() > 0:
names = attrs.getNames()
for name in names:
writeHtml(" " + name + "=\"" + attrs.get(name) + "\"")
Expand Down Expand Up @@ -198,9 +201,9 @@ def parseConfig(file):
parser.setErrorHandler(eh)

try:
fd = open(file, 'r')
fd = codecs.open(file, 'r', 'utf-8')
except:
fd = open(lookupdir + file, 'r')
fd = codecs.open(lookupdir + file, 'r', 'utf-8')

# Parse the file
parser.parse(fd)
Expand All @@ -214,10 +217,10 @@ def parseConfig(file):

try:
(options, arguments) = getopt.getopt(sys.argv[1:],'fhd:')
except getopt.GetoptError, ex:
except getopt.GetoptError as ex:
print
print "ERROR:"
print ex.msg
print ("ERROR:")
print (ex.msg)
usage()
sys.exit(1)
pass
Expand All @@ -240,7 +243,7 @@ def parseConfig(file):
replace[idx:] = [lookupdir]

if not arguments:
print "No source file specified"
print ("No source file specified")
usage()
sys.exit(1)
pass
Expand Down

0 comments on commit 938fcd7

Please sign in to comment.