handlers: remove remnant bits of python2 compat

This commit is contained in:
Jean-Francois Dockes 2022-09-05 10:43:50 +02:00
parent 27f4d1f321
commit c86cb9438b
29 changed files with 33 additions and 139 deletions

View File

@ -20,8 +20,6 @@
# All data is binary. This is important for Python3 # All data is binary. This is important for Python3
# All parameter names are converted to and processed as str/unicode # All parameter names are converted to and processed as str/unicode
from __future__ import print_function
import sys import sys
import os import os
import tempfile import tempfile

View File

@ -18,8 +18,6 @@
# Base for extractor classes. With some common generic implementations # Base for extractor classes. With some common generic implementations
# for the boilerplate functions. # for the boilerplate functions.
from __future__ import print_function
import os import os
import sys import sys
import rclexecm import rclexecm

View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import print_function
# dia (http://live.gnome.org/Dia) file filter for recoll # dia (http://live.gnome.org/Dia) file filter for recoll
# stefan.friedel@iwr.uni-heidelberg.de 2012 # stefan.friedel@iwr.uni-heidelberg.de 2012

View File

@ -17,8 +17,6 @@
# Recoll DJVU extractor # Recoll DJVU extractor
from __future__ import print_function
import os import os
import sys import sys
import re import re

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from __future__ import print_function
import rclexecm import rclexecm
import rclexec1 import rclexec1

View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Extract Html content from an EPUB file (.epub)""" """Extract Html content from an EPUB file (.epub)"""
from __future__ import print_function
rclepub_html_mtype = "text/html" rclepub_html_mtype = "text/html"

View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Extract Html content from an EPUB file (.chm), concatenating all sections""" """Extract Html content from an EPUB file (.chm), concatenating all sections"""
from __future__ import print_function
import sys import sys
import os import os

View File

@ -26,8 +26,6 @@
# this would be to slow. So this helps implementing a permanent script # this would be to slow. So this helps implementing a permanent script
# to repeatedly execute single commands. # to repeatedly execute single commands.
from __future__ import print_function
import subprocess import subprocess
import rclexecm import rclexecm
from rclbasehandler import RclBaseHandler from rclbasehandler import RclBaseHandler

View File

@ -20,8 +20,6 @@
# All data is binary. This is important for Python3 # All data is binary. This is important for Python3
# All parameter names are converted to and processed as str/unicode # All parameter names are converted to and processed as str/unicode
from __future__ import print_function
import sys import sys
import os import os
import tempfile import tempfile
@ -30,7 +28,6 @@ import getopt
import rclconfig import rclconfig
import cmdtalk import cmdtalk
PY3 = (sys.version > '3')
_g_mswindows = (sys.platform == "win32") _g_mswindows = (sys.platform == "win32")
_g_execdir = os.path.dirname(sys.argv[0]) _g_execdir = os.path.dirname(sys.argv[0])
@ -62,12 +59,11 @@ def makebytes(data):
# Possibly decode binary file name for use as subprocess argument, # Possibly decode binary file name for use as subprocess argument,
# depending on platform. # depending on platform.
def subprocfile(fn): def subprocfile(fn):
# On Windows PY3 the list2cmdline() method in subprocess assumes that # On Windows Python 3 the list2cmdline() method in subprocess assumes that all args are str, and
# all args are str, and we receive file names as UTF-8. So we need # we receive file names as UTF-8. So we need to convert.
# to convert. # On Unix all list elements get converted to bytes in the C _posixsubprocess module, nothing to
# On Unix all list elements get converted to bytes in the C # do.
# _posixsubprocess module, nothing to do. if _g_mswindows and type(fn) != type(''):
if PY3 and _g_mswindows and type(fn) != type(''):
return fn.decode('UTF-8') return fn.decode('UTF-8')
else: else:
return fn return fn
@ -383,10 +379,7 @@ def main(proto, extract):
print("Open error", file=sys.stderr) print("Open error", file=sys.stderr)
sys.exit(1) sys.exit(1)
if PY3: ioout = sys.stdout.buffer
ioout = sys.stdout.buffer
else:
ioout = sys.stdout
if ipath != b"" or actAsSingle: if ipath != b"" or actAsSingle:
params['ipath'] = ipath params['ipath'] = ipath
ok, data, ipath, eof = extract.getipath(params) ok, data, ipath, eof = extract.getipath(params)

View File

@ -16,8 +16,6 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
###################################### ######################################
from __future__ import print_function
import sys import sys
import rclexecm import rclexecm
import rclxslt import rclxslt

View File

@ -18,8 +18,6 @@
# Base class for simple (one stylesheet) xslt-based handlers # Base class for simple (one stylesheet) xslt-based handlers
from __future__ import print_function
import sys import sys
import rclxslt import rclxslt
import gzip import gzip

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from __future__ import print_function
# Read an ICS file, break it into "documents" which are events, todos, # Read an ICS file, break it into "documents" which are events, todos,
# or journal entries, and interface with recoll execm # or journal entries, and interface with recoll execm

View File

@ -6,7 +6,6 @@
# #
# Uses pyexiv2. Also tried Pillow, found it useless for tags. # Uses pyexiv2. Also tried Pillow, found it useless for tags.
# #
from __future__ import print_function
import sys import sys
import os import os

View File

@ -3,8 +3,6 @@
# Read a file in GNU info format and output its nodes as subdocs, # Read a file in GNU info format and output its nodes as subdocs,
# interfacing with recoll execm # interfacing with recoll execm
from __future__ import print_function
import rclexecm import rclexecm
import sys import sys
import os import os

View File

@ -17,8 +17,6 @@
# Recoll handler for iPython / Jupyter notebook files. # Recoll handler for iPython / Jupyter notebook files.
from __future__ import print_function
import os import os
import sys import sys
import json import json

View File

@ -1,8 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Read a .kar midi karaoke file and translate to recoll indexable format # Read a .kar midi karaoke file and translate to recoll indexable format
# This does not work with Python3 yet because python:midi doesn't
from __future__ import print_function
import rclexecm import rclexecm
import sys import sys
@ -46,11 +44,7 @@ htmltemplate = '''
nlbytes = b'\n' nlbytes = b'\n'
bsbytes = b'\\' bsbytes = b'\\'
PY3 = sys.version > '3' nullchar = 0
if PY3:
nullchar = 0
else:
nullchar = chr(0)
class KarTextExtractor(RclBaseHandler): class KarTextExtractor(RclBaseHandler):
# Afaik, the only charset encodings with null bytes are variations on # Afaik, the only charset encodings with null bytes are variations on

View File

@ -13,12 +13,7 @@ epsilon with dasia (in unicode but not iso). Can this be replaced by either epsi
with acute accent ? with acute accent ?
""" """
from __future__ import print_function
import sys import sys
PY3 = sys.version > '3'
if not PY3:
import string
import glob import glob
import os import os
import os.path import os.path
@ -38,10 +33,7 @@ class European8859TextClassifier:
# Table to translate from punctuation to spaces # Table to translate from punctuation to spaces
self.punct = b'''0123456789<>/*?[].@+-,#_$%&={};.,:!"''' + b"'\n\r" self.punct = b'''0123456789<>/*?[].@+-,#_$%&={};.,:!"''' + b"'\n\r"
spaces = len(self.punct) * b' ' spaces = len(self.punct) * b' '
if PY3: self.spacetable = bytes.maketrans(self.punct, spaces)
self.spacetable = bytes.maketrans(self.punct, spaces)
else:
self.spacetable = string.maketrans(self.punct, spaces)
def readlanguages(self, langzip): def readlanguages(self, langzip):
"""Extract the stop words lists from the zip file. """Extract the stop words lists from the zip file.

View File

@ -23,24 +23,15 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
# #
from __future__ import print_function
import sys import sys
from struct import unpack, pack from struct import unpack, pack
import six import six
PY3 = sys.version > '3' def next_byte_as_int(data):
return next(data)
if PY3: def next_byte_as_char(data):
def next_byte_as_int(data): return bytes([next(data)])
return next(data)
def next_byte_as_char(data):
return bytes([next(data)])
else:
def next_byte_as_int(data):
return ord(data.next())
def next_byte_as_char(data):
return next(data)
## ##
## Constants ## Constants
@ -270,12 +261,8 @@ class NoteEvent(Event):
self.velocity) self.velocity)
def decode_data(self): def decode_data(self):
if PY3: self.pitch = self.data[0]
self.pitch = self.data[0] self.velocity = self.data[1]
self.velocity = self.data[1]
else:
self.pitch = ord(self.data[0])
self.velocity = ord(self.data[1])
class NoteOnEvent(NoteEvent): class NoteOnEvent(NoteEvent):
@ -309,12 +296,8 @@ class ControlChangeEvent(Event):
hex(ord(self.data[1]))) hex(ord(self.data[1])))
def decode_data(self): def decode_data(self):
if PY3: self.control = self.data[0]
self.control = self.data[0] self.value = self.data[1]
self.value = self.data[1]
else:
self.control = ord(self.data[0])
self.value = ord(self.data[1])
class ProgramChangeEvent(Event): class ProgramChangeEvent(Event):
@ -328,10 +311,7 @@ class ProgramChangeEvent(Event):
hex(ord(self.data[0]))) hex(ord(self.data[0])))
def decode_data(self): def decode_data(self):
if PY3: self.value = self.data[0]
self.value = self.data[0]
else:
self.value = ord(self.data[0])
class ChannelAfterTouchEvent(Event): class ChannelAfterTouchEvent(Event):
@ -356,12 +336,8 @@ class PitchWheelEvent(Event):
hex(ord(self.data[1]))) hex(ord(self.data[1])))
def decode_data(self): def decode_data(self):
if PY3: first = self.data[0]
first = self.data[0] second = self.data[1]
second = self.data[1]
else:
first = ord(self.data[0])
second = ord(self.data[1])
self.value = ((second << 7) | first) - 0x2000 self.value = ((second << 7) | first) - 0x2000
@ -461,10 +437,7 @@ class PortEvent(MetaEvent):
def decode_data(self): def decode_data(self):
assert(len(self.data) == 1) assert(len(self.data) == 1)
if PY3: self.port = self.data[0]
self.port = self.data[0]
else:
self.port = ord(self.data[0])
class TrackLoopEvent(MetaEvent): class TrackLoopEvent(MetaEvent):
name = 'Track Loop' name = 'Track Loop'
@ -498,13 +471,7 @@ class SetTempoEvent(MetaEvent):
def decode_data(self): def decode_data(self):
assert(len(self.data) == 3) assert(len(self.data) == 3)
if PY3: self.mpqn = (self.data[0] << 16) + (self.data[1] << 8) + self.data[2]
self.mpqn = (self.data[0] << 16) + (self.data[1] << 8) \
+ self.data[2]
else:
self.mpqn = (ord(self.data[0]) << 16) + (ord(self.data[1]) << 8) \
+ ord(self.data[2])
self.tempo = float(6e7) / self.mpqn self.tempo = float(6e7) / self.mpqn
@ -523,22 +490,13 @@ class TimeSignatureEvent(MetaEvent):
(super(TimeSignatureEvent, self).__str__(), (super(TimeSignatureEvent, self).__str__(),
self.numerator, self.denominator, self.numerator, self.denominator,
self.metronome, self.thirtyseconds) self.metronome, self.thirtyseconds)
if PY3: def decode_data(self):
def decode_data(self): assert(len(self.data) == 4)
assert(len(self.data) == 4) self.numerator = self.data[0]
self.numerator = self.data[0] # Weird: the denominator is two to the power of the data variable
# Weird: the denominator is two to the power of the data variable self.denominator = 2 ** self.data[1]
self.denominator = 2 ** self.data[1] self.metronome = self.data[2]
self.metronome = self.data[2] self.thirtyseconds = self.data[3]
self.thirtyseconds = self.data[3]
else:
def decode_data(self):
assert(len(self.data) == 4)
self.numerator = ord(self.data[0])
# Weird: the denominator is two to the power of the data variable
self.denominator = 2 ** ord(self.data[1])
self.metronome = ord(self.data[2])
self.thirtyseconds = ord(self.data[3])
class KeySignatureEvent(MetaEvent): class KeySignatureEvent(MetaEvent):

View File

@ -2,8 +2,6 @@
# Recoll PPT text extractor # Recoll PPT text extractor
from __future__ import print_function
import rclexecm import rclexecm
import rclexec1 import rclexec1
import re import re

View File

@ -18,8 +18,6 @@
# Free Software Foundation, Inc., # Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import print_function
import sys import sys
import rclexecm import rclexecm
import os import os

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from __future__ import print_function
import rclexecm import rclexecm
import rclexec1 import rclexec1

View File

@ -6,8 +6,6 @@
# It works not only for tar-files, but automatically for gzipped and # It works not only for tar-files, but automatically for gzipped and
# bzipped tar-files at well. # bzipped tar-files at well.
from __future__ import print_function
import rclexecm import rclexecm
try: try:

View File

@ -18,8 +18,6 @@
# Wrapping a text file. Recoll does it internally in most cases, but # Wrapping a text file. Recoll does it internally in most cases, but
# this is for use by another filter. # this is for use by another filter.
from __future__ import print_function
import rclexecm import rclexecm
import sys import sys
from rclbasehandler import RclBaseHandler from rclbasehandler import RclBaseHandler

View File

@ -2,7 +2,6 @@
"""Index text lines as document (execm handler sample). This exists """Index text lines as document (execm handler sample). This exists
to demonstrate the execm interface and is not meant to be useful or to demonstrate the execm interface and is not meant to be useful or
efficient""" efficient"""
from __future__ import print_function
import sys import sys
import os import os

View File

@ -1,5 +1,4 @@
# No shebang: this is only used on Windows. We use a shell script on Linux # No shebang: this is only used on Windows. We use a shell script on Linux
from __future__ import print_function
import rclexecm import rclexecm
import sys import sys

View File

@ -2,8 +2,6 @@
# WAR web archive filter for recoll. War file are gzipped tar files # WAR web archive filter for recoll. War file are gzipped tar files
from __future__ import print_function
import rclexecm import rclexecm
import tarfile import tarfile

View File

@ -16,7 +16,6 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Code to extract XMP tags using libexempi and python-xmp # Code to extract XMP tags using libexempi and python-xmp
from __future__ import print_function
can_xmp = True can_xmp = True
try: try:

View File

@ -18,8 +18,6 @@
# Zip file extractor for Recoll # Zip file extractor for Recoll
from __future__ import print_function
import os import os
import posixpath import posixpath
import fnmatch import fnmatch
@ -155,14 +153,11 @@ class ZipExtractor:
if skipped is not None: if skipped is not None:
self.skiplist += conftree.stringToStrings(skipped) self.skiplist += conftree.stringToStrings(skipped)
try: try:
if rclexecm.PY3: # Note: py3 ZipFile wants an str file name, which
# Note: py3 ZipFile wants an str file name, which # is wrong: file names are binary. But it accepts an
# is wrong: file names are binary. But it accepts an # open file, and open() has no such restriction
# open file, and open() has no such restriction self.f = open(filename, 'rb')
self.f = open(filename, 'rb') self.zip = ZipFile(self.f)
self.zip = ZipFile(self.f)
else:
self.zip = ZipFile(filename)
return True return True
except Exception as err: except Exception as err:
self.em.rclog("openfile: failed: [%s]" % err) self.em.rclog("openfile: failed: [%s]" % err)

View File

@ -23,8 +23,6 @@
# the minimum version supported. # the minimum version supported.
from __future__ import print_function
import sys import sys
import xml.sax import xml.sax