shithub: choc

Download patch

ref: 765f864d8481d039c222b22b0af703e66604a3c0
parent: d0c36735aaeb178d936fc7434d5e3a4b356430d6
author: Mike Swanson <[email protected]>
date: Sat Jan 21 02:13:08 EST 2017

man: make scripts Python2 compatible again.

My last commits broke functionality for Python2, but it's relatively
simple to fix.

open() becomes io.open() -- it effectively does nothing for Python3,
but for Python2 it uses a backported version of the open() call.

Python2 uses bytes and strings interchangably, but I needed to use
stdout.sys.buffer for writing arbitrary bytes data to stdout.
Conditionally call the appropriate stream depending on Python version.

--- a/man/docgen
+++ b/man/docgen
@@ -28,6 +28,7 @@
 #   CONFIG_VARIABLE_INT(my_variable,       c_variable),
 #
 
+import io
 import sys
 import os
 import re
@@ -36,6 +37,14 @@
 
 INCLUDE_STATEMENT_RE = re.compile("@include\s+(\S+)")
 
+# Use appropriate stdout function for Python 2 or 3
+
+def stdout(buf):
+    if sys.version_info.major < 3:
+        sys.stdout.write(buf)
+    else:
+        sys.stdout.buffer.write(buf)
+
 # Find the maximum width of a list of parameters (for plain text output)
 
 def parameter_list_width(params):
@@ -300,7 +309,7 @@
 # Read list of wiki pages
 
 def read_wikipages():
-    f = open("wikipages", encoding='UTF-8')
+    f = io.open("wikipages", encoding='UTF-8')
 
     try:
         for line in f:
@@ -356,7 +365,7 @@
 
     current_config_file = None
 
-    f = open(file, encoding='UTF-8')
+    f = io.open(file, encoding='UTF-8')
 
     try:
         param = None
@@ -422,7 +431,7 @@
         process_file(path)
 
 def print_template(template_file, content):
-    f = open(template_file, encoding='UTF-8')
+    f = io.open(template_file, encoding='UTF-8')
 
     try:
         for line in f:
@@ -432,7 +441,7 @@
                 print_template(filename, content)
             else:
                 line = line.replace("@content", content)
-                sys.stdout.buffer.write(line.rstrip().encode('UTF-8') + b'\n')
+                stdout(line.rstrip().encode('UTF-8') + b'\n')
 
     finally:
         f.close()
@@ -452,7 +461,7 @@
     read_wikipages()
 
     for t in targets:
-        sys.stdout.buffer.write(t.wiki_output().encode('UTF-8') + b'\n')
+        stdout(t.wiki_output().encode('UTF-8') + b'\n')
 
 def plaintext_output(targets, template_file):
 
--- a/man/simplecpp
+++ b/man/simplecpp
@@ -40,6 +40,7 @@
 #
 
 import collections
+import io
 import sys
 import re
 
@@ -76,7 +77,7 @@
 		raise Exception("Mismatched #if in '%s'" % stream.name)
 
 def parse_file(filename):
-	f = open(filename, encoding='UTF-8')
+	f = io.open(filename, encoding='UTF-8')
 
 	try:
 		parse_stream(f)