first pass at converting the filters for python 2/3 compat
This commit is contained in:
+44
-51
@@ -3,6 +3,7 @@
|
||||
# Read a file in GNU info format and output its nodes as subdocs,
|
||||
# interfacing with recoll execm
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import rclexecm
|
||||
import sys
|
||||
@@ -16,24 +17,12 @@ import subprocess
|
||||
# Some info source docs contain charset info like:
|
||||
# @documentencoding ISO-2022-JP
|
||||
# But this seems to be absent from outputs.
|
||||
htmltemplate = '''
|
||||
<html>
|
||||
<head>
|
||||
<title>%s</title>
|
||||
<meta name="rclaptg" content="gnuinfo">
|
||||
</head>
|
||||
<body>
|
||||
<pre style="white-space: pre-wrap">
|
||||
%s
|
||||
</pre></body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
# RclExecm interface
|
||||
class InfoExtractor:
|
||||
def __init__(self, em):
|
||||
self.file = ""
|
||||
self.contents = []
|
||||
self.contents = []
|
||||
self.em = em
|
||||
|
||||
def extractone(self, index):
|
||||
@@ -43,8 +32,13 @@ class InfoExtractor:
|
||||
nodename, docdata = self.contents[index]
|
||||
nodename = self.em.htmlescape(nodename)
|
||||
docdata = self.em.htmlescape(docdata)
|
||||
|
||||
docdata = htmltemplate % (nodename, docdata)
|
||||
# strange whitespace to avoid changing the module tests (same as old)
|
||||
docdata = b'\n<html>\n <head>\n <title>' + nodename + \
|
||||
b'</title>\n' + \
|
||||
' <meta name="rclaptg" content="gnuinfo">\n' + \
|
||||
b' </head>\n <body>\n' + \
|
||||
b' <pre style="white-space: pre-wrap">\n ' + \
|
||||
docdata + b'\n </pre></body>\n</html>\n'
|
||||
|
||||
iseof = rclexecm.RclExecM.noteof
|
||||
if self.currentindex >= len(self.contents) -1:
|
||||
@@ -60,19 +54,18 @@ class InfoExtractor:
|
||||
self.em.rclog("Openfile: %s is not a file" % self.file)
|
||||
return False
|
||||
|
||||
cmd = "info --subnodes -o - -f " + self.file
|
||||
cmd = b'info --subnodes -o - -f ' + self.file
|
||||
nullstream = open("/dev/null", 'w')
|
||||
try:
|
||||
infostream = subprocess.Popen(cmd, shell=True, bufsize=1,
|
||||
stderr=nullstream,
|
||||
stdout=subprocess.PIPE).stdout
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
# Consider this as permanently fatal.
|
||||
self.em.rclog("Openfile: exec info: %s" % str(e))
|
||||
print "RECFILTERROR HELPERNOTFOUND info"
|
||||
print("RECFILTERROR HELPERNOTFOUND info")
|
||||
sys.exit(1);
|
||||
|
||||
|
||||
self.currentindex = -1
|
||||
|
||||
self.contents = InfoSimpleSplitter().splitinfo(self.file, infostream)
|
||||
@@ -117,9 +110,9 @@ class InfoSimpleSplitter:
|
||||
index = 0
|
||||
listout = []
|
||||
node_dict = {}
|
||||
node = ""
|
||||
node = b''
|
||||
infofile = os.path.basename(filename)
|
||||
nodename = "Unknown"
|
||||
nodename = b'Unknown'
|
||||
|
||||
for line in fin:
|
||||
|
||||
@@ -128,41 +121,41 @@ class InfoSimpleSplitter:
|
||||
# beginning with spaces (it's a bug probably, only seen it once)
|
||||
# Maybe we'd actually be better off directly interpreting the
|
||||
# info files
|
||||
if gotblankline and line.lstrip(" ").startswith("File: "):
|
||||
if gotblankline and line.lstrip(b' ').startswith(b'File: '):
|
||||
prevnodename = nodename
|
||||
line = line.rstrip("\n\r")
|
||||
pairs = line.split(",")
|
||||
up = "Top"
|
||||
line = line.rstrip(b'\n\r')
|
||||
pairs = line.split(b',')
|
||||
up = b'Top'
|
||||
nodename = str(index)
|
||||
try:
|
||||
for pair in pairs:
|
||||
name, value = pair.split(':')
|
||||
name = name.strip(" ")
|
||||
value = value.strip(" ")
|
||||
if name == "Node":
|
||||
name, value = pair.split(b':')
|
||||
name = name.strip(b' ')
|
||||
value = value.strip(b' ')
|
||||
if name == b'Node':
|
||||
nodename = value
|
||||
if name == "Up":
|
||||
if name == b'Up':
|
||||
up = value
|
||||
if name == "File":
|
||||
if name == b'File':
|
||||
infofile = value
|
||||
except:
|
||||
print >> sys.stderr, "rclinfo: bad line in %s: [%s]\n" % \
|
||||
(infofile, line)
|
||||
except Exception as err:
|
||||
print("rclinfo: bad line in %s: [%s] %s\n" % \
|
||||
(infofile, line, err), file = sys.stderr)
|
||||
nodename = prevnodename
|
||||
node += line
|
||||
continue
|
||||
|
||||
if node_dict.has_key(nodename):
|
||||
print >> sys.stderr, "Info file", filename, \
|
||||
"Dup node: ", nodename
|
||||
if nodename in node_dict:
|
||||
print("Info file %s Dup node: %s" % (filename, nodename), \
|
||||
file=sys.stderr)
|
||||
node_dict[nodename] = up
|
||||
|
||||
if index != 0:
|
||||
listout.append((prevnodename, node))
|
||||
node = ""
|
||||
node = b''
|
||||
index += 1
|
||||
|
||||
if line.rstrip("\n\r") == '':
|
||||
if line.rstrip(b'\n\r') == b'':
|
||||
gotblankline = 1
|
||||
else:
|
||||
gotblankline = 0
|
||||
@@ -170,7 +163,7 @@ class InfoSimpleSplitter:
|
||||
node += line
|
||||
|
||||
# File done, add last dangling node
|
||||
if node != "":
|
||||
if node != b'':
|
||||
listout.append((nodename, node))
|
||||
|
||||
# Compute node paths (concatenate "Up" values), to be used
|
||||
@@ -178,34 +171,34 @@ class InfoSimpleSplitter:
|
||||
# the info file tree is bad
|
||||
listout1 = []
|
||||
for nodename, node in listout:
|
||||
title = ""
|
||||
title = b''
|
||||
loop = 0
|
||||
error = 0
|
||||
while nodename != "Top":
|
||||
title = nodename + " / " + title
|
||||
if node_dict.has_key(nodename):
|
||||
while nodename != b'Top':
|
||||
title = nodename + b' / ' + title
|
||||
if nodename in node_dict:
|
||||
nodename = node_dict[nodename]
|
||||
else:
|
||||
print >> sys.stderr, \
|
||||
print(
|
||||
"Infofile: node's Up does not exist: file %s, path %s, up [%s]" % \
|
||||
(infofile, title, nodename)
|
||||
(infofile, title, nodename), sys.stderr)
|
||||
error = 1
|
||||
break
|
||||
loop += 1
|
||||
if loop > 50:
|
||||
print >> sys.stderr, "Infofile: bad tree (looping)", \
|
||||
infofile
|
||||
print("Infofile: bad tree (looping) %s" % infofile, \
|
||||
file = sys.stderr)
|
||||
error = 1
|
||||
break
|
||||
|
||||
if error:
|
||||
continue
|
||||
|
||||
if title == "":
|
||||
if title == b'':
|
||||
title = infofile
|
||||
else:
|
||||
title = infofile + " / " + title
|
||||
title = title.rstrip(" / ")
|
||||
title = infofile + b' / ' + title
|
||||
title = title.rstrip(b' / ')
|
||||
listout1.append((title, node))
|
||||
|
||||
return listout1
|
||||
|
||||
Reference in New Issue
Block a user