From: Oleg Broytman Date: Wed, 2 Aug 2023 16:10:08 +0000 (+0300) Subject: Feat: Update for Python 3 X-Git-Url: https://git.phdru.name/?p=extfs.d.git;a=commitdiff_plain;h=27b9e751daa0769d71563b0fbcd007d18e964b2f Feat: Update for Python 3 --- diff --git a/dummy b/dummy index 43a7e69..8e22a1d 100755 --- a/dummy +++ b/dummy @@ -1,98 +1,100 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 """Dummy VFS for Midnight Commander. Just for a test.""" -__version__ = "1.0.3" +__version__ = "1.1.0" __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2004-2013 PhiloSoft Design" +__copyright__ = "Copyright (C) 2004-2023 PhiloSoft Design" __license__ = "GPL" import sys + def log_error(msg): - sys.stderr.write(msg + '\n') + sys.stderr.write(msg + '\n') + def error(msg): - log_error(msg + '\n') - sys.exit(1) + log_error(msg + '\n') + sys.exit(1) if len(sys.argv) < 2: - error("""\ + error("""\ It is not a program - it is a dummy VFS for Midnight Commander. Put it in $HOME/.mc/extfs.d or /usr/lib/mc/extfs.""") def mcdummy_list(): - """List the entire VFS""" - # Ignore the VFS name (sys.argv[2]) - # Emit a dummy listing - print "-r--r--r-- 1 user group 0 Jun 13 02:20 file0" - print "-r--r--r-- 1 user group 1 Jun 13 02:21 file1" - print "dr--r--r-- 1 user group 2 Jun 13 02:22 subdir" - print "-r--r--r-- 1 user group 3 Jun 13 02:23 subdir/file3" - print "-r--r--r-- 1 user group 4 Jun 13 02:23 subdir/file4" + """List the entire VFS""" + # Ignore the VFS name (sys.argv[2]) + # Emit a dummy listing + print("-r--r--r-- 1 user group 0 Jun 13 02:20 file0") + print("-r--r--r-- 1 user group 1 Jun 13 02:21 file1") + print("dr--r--r-- 1 user group 2 Jun 13 02:22 subdir") + print("-r--r--r-- 1 user group 3 Jun 13 02:23 subdir/file3") + print("-r--r--r-- 1 user group 4 Jun 13 02:23 subdir/file4") def mcdummy_copyout(): - """Extract a file from the VFS""" - # Ignore the VFS name (sys.argv[2]) - dummy_filename = sys.argv[3] - real_filename = sys.argv[4] + """Extract a file from the VFS""" + # Ignore the VFS name (sys.argv[2]) + dummy_filename = sys.argv[3] + real_filename = sys.argv[4] - real_file = open(real_filename, 'a') - real_file.write("Copy from %s\n" % dummy_filename) - real_file.write("Copy to %s\n" % real_filename) - real_file.close() + real_file = open(real_filename, 'a') + real_file.write("Copy from %s\n" % dummy_filename) + real_file.write("Copy to %s\n" % real_filename) + real_file.close() def mcdummy_copyin(): - """Put a file to the VFS""" - # Ignore the VFS name (sys.argv[2]) - dummy_filename = sys.argv[3] - real_filename = sys.argv[4] + """Put a file to the VFS""" + # Ignore the VFS name (sys.argv[2]) + dummy_filename = sys.argv[3] + real_filename = sys.argv[4] - real_file = open(real_filename + "-dummy.tmp", 'a') - real_file.write("Copy from %s\n" % real_filename) - real_file.write("Copy to %s\n" % dummy_filename) - real_file.close() + real_file = open(real_filename + "-dummy.tmp", 'a') + real_file.write("Copy from %s\n" % real_filename) + real_file.write("Copy to %s\n" % dummy_filename) + real_file.close() def mcdummy_rm(): - """Remove a file from the VFS""" - # Ignore the VFS name (sys.argv[2]) - dummy_filename = sys.argv[3] + """Remove a file from the VFS""" + # Ignore the VFS name (sys.argv[2]) + dummy_filename = sys.argv[3] - real_file = open(".dummy.tmp", 'a') - real_file.write("Remove %s\n" % dummy_filename) - real_file.close() + real_file = open(".dummy.tmp", 'a') + real_file.write("Remove %s\n" % dummy_filename) + real_file.close() def mcdummy_mkdir(): - """Create a directory in the VFS""" - # Ignore the VFS name (sys.argv[2]) - dummy_dirname = sys.argv[3] + """Create a directory in the VFS""" + # Ignore the VFS name (sys.argv[2]) + dummy_dirname = sys.argv[3] - real_file = open(".dummy.tmp", 'a') - real_file.write("Create %s\n" % dummy_dirname) - real_file.close() + real_file = open(".dummy.tmp", 'a') + real_file.write("Create %s\n" % dummy_dirname) + real_file.close() def mcdummy_rmdir(): - """Remove a directory from the VFS""" - # Ignore the VFS name (sys.argv[2]) - dummy_dirname = sys.argv[3] + """Remove a directory from the VFS""" + # Ignore the VFS name (sys.argv[2]) + dummy_dirname = sys.argv[3] - real_file = open(".dummy.tmp", 'a') - real_file.write("Remove %s\n" % dummy_dirname) - real_file.close() + real_file = open(".dummy.tmp", 'a') + real_file.write("Remove %s\n" % dummy_dirname) + real_file.close() g = globals() command = sys.argv[1] procname = "mcdummy_" + command -if not g.has_key(procname): - error("Unknown command %s" % command) +if procname not in g: + error("Unknown command %s" % command) g[procname]() diff --git a/eff_bdecode.py b/eff_bdecode.py index 5f5b93b..fe1c4c2 100644 --- a/eff_bdecode.py +++ b/eff_bdecode.py @@ -23,9 +23,11 @@ # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # PERFORMANCE OF THIS SOFTWARE. +from functools import partial import re -def tokenize(text, match=re.compile("([idel])|(\d+):|(-?\d+)").match): + +def tokenize(text, match=re.compile("([idel])|(\\d+):|(-?\\d+)").match): i = 0 while i < len(text): m = match(text, i) @@ -38,33 +40,38 @@ def tokenize(text, match=re.compile("([idel])|(\d+):|(-?\d+)").match): else: yield s -def decode_item(next, token): + +def decode_item(next_, token): if token == "i": # integer: "i" value "e" - data = int(next()) - if next() != "e": + data = int(next_()) + if next_() != "e": raise ValueError elif token == "s": # string: "s" value (virtual tokens) - data = next() + data = next_() elif token == "l" or token == "d": # container: "l" (or "d") values "e" data = [] - tok = next() + tok = next_() while tok != "e": - data.append(decode_item(next, tok)) - tok = next() + data.append(decode_item(next_, tok)) + try: + tok = next_() + except StopIteration: + break if token == "d": data = dict(zip(data[0::2], data[1::2])) else: raise ValueError return data + def decode(text): try: src = tokenize(text) - data = decode_item(src.next, src.next()) - for token in src: # look for more tokens + data = decode_item(partial(next, src), next(src)) + for token in src: # look for more tokens raise SyntaxError("trailing junk") except (AttributeError, ValueError, StopIteration): raise SyntaxError("syntax error") diff --git a/torrent b/torrent index 7841fa7..76104d1 100755 --- a/torrent +++ b/torrent @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 """Torrent Virtual FileSystem for Midnight Commander The script requires Midnight Commander 3.1+ @@ -35,9 +35,9 @@ The filesystem is, naturally, read-only. """ -__version__ = "1.2.4" +__version__ = "1.3.0" __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2010-2018 PhiloSoft Design" +__copyright__ = "Copyright (C) 2010-2023 PhiloSoft Design" __license__ = "GPL" @@ -57,8 +57,8 @@ if use_locale: # Get the default charset. try: lcAll = locale.getdefaultlocale() - except locale.Error, err: - print >>sys.stderr, "WARNING:", err + except locale.Error as err: + print("WARNING:", err, file=sys.stderr) lcAll = [] if len(lcAll) == 2: @@ -66,8 +66,8 @@ if use_locale: else: try: default_encoding = locale.getpreferredencoding() - except locale.Error, err: - print >>sys.stderr, "WARNING:", err + except locale.Error as err: + print("WARNING:", err, file=sys.stderr) default_encoding = sys.getdefaultencoding() else: default_encoding = sys.getdefaultencoding() @@ -204,10 +204,10 @@ def mctorrent_list(): dt = decode_datetime(getmtime(sys.argv[2])) for name in sorted(dirs): - print "dr-xr-xr-x 1 user group 0 %s %s" % (dt, name) + print("dr-xr-xr-x 1 user group 0 %s %s" % (dt, name)) for name, size in sorted(paths): - print "-r--r--r-- 1 user group %d %s %s" % (size, dt, name) + print("-r--r--r-- 1 user group %d %s %s" % (size, dt, name)) def mctorrent_copyout(): @@ -295,7 +295,7 @@ def decode_torrent(): data = torrent_file.read() torrent_file.close() return decode(data) - except IOError, error_str: + except IOError as error_str: torrent_error(error_str) @@ -316,7 +316,7 @@ def decode_datetime(dt): def decode_announce_list(announce): - return '\n'.join(l[0] for l in announce if l) + return '\n'.join(a[0] for a in announce if a) command = sys.argv[1] @@ -333,5 +333,5 @@ try: g[procname]() except SystemExit: raise -except: +except Exception: logger.exception("Error during run") diff --git a/xml b/xml index 59fad36..d2beb55 100755 --- a/xml +++ b/xml @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 """XML Virtual FileSystem for Midnight Commander The script requires Midnight Commander 3.1+ @@ -44,9 +44,9 @@ The VFS was inspired by a FUSE xmlfs: https://github.com/halhen/xmlfs """ -__version__ = "1.1.5" +__version__ = "1.2.0" __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2013-2015 PhiloSoft Design" +__copyright__ = "Copyright (C) 2013-2023 PhiloSoft Design" __license__ = "GPL" # Can be None for default choice, 'lxml', 'elementtree' or 'minidom'. @@ -62,12 +62,13 @@ import sys from time import localtime import xml.dom.minidom -try: - import xml.etree.ElementTree as ET -except ImportError: - pass -else: - use_elementtree = True +if sys.version_info[0] == 2: + try: + import xml.etree.ElementTree as ET + except ImportError: + pass + else: + use_elementtree = True try: import lxml.etree as etree @@ -86,8 +87,8 @@ if use_locale: # Get the default charset. try: lcAll = locale.getdefaultlocale() - except locale.Error, err: - print >>sys.stderr, "WARNING:", err + except locale.Error as err: + print("WARNING:", err, file=sys.stderr) lcAll = [] if len(lcAll) == 2: @@ -95,8 +96,8 @@ if use_locale: else: try: default_encoding = locale.getpreferredencoding() - except locale.Error, err: - print >>sys.stderr, "WARNING:", err + except locale.Error as err: + print("WARNING:", err, file=sys.stderr) default_encoding = sys.getdefaultencoding() else: default_encoding = sys.getdefaultencoding() @@ -137,8 +138,8 @@ class XmlVfs(object): root_comments = self.get_root_comments() if root_comments: - print "-r--r--r-- 1 user group %d %s text" % ( - len(root_comments), self.xml_file_dt) + print("-r--r--r-- 1 user group %d %s text" % ( + len(root_comments), self.xml_file_dt)) self._list(self.getroot()) @@ -160,20 +161,20 @@ class XmlVfs(object): else: subpath = '%s %s' % (template % n, tag) subpath_encoded = subpath.encode(default_encoding, "replace") - print "dr-xr-xr-x 1 user group 0 %s %s" % ( - self.xml_file_dt, subpath_encoded) + print("dr-xr-xr-x 1 user group 0 %s %s" % ( + self.xml_file_dt, subpath_encoded)) if self.getattrs(element): attr_text = self.attrs2text(element) - print "-r--r--r-- 1 user group %d %s %s/attributes" % ( - len(attr_text), self.xml_file_dt, subpath_encoded) + print("-r--r--r-- 1 user group %d %s %s/attributes" % ( + len(attr_text), self.xml_file_dt, subpath_encoded)) if self.supports_namespaces and self.has_ns(element): ns_text = self.ns2text(element) - print "-r--r--r-- 1 user group %d %s %s/namespaces" % ( - len(ns_text), self.xml_file_dt, subpath_encoded) + print("-r--r--r-- 1 user group %d %s %s/namespaces" % ( + len(ns_text), self.xml_file_dt, subpath_encoded)) text = self.collect_text(element) if text: - print "-r--r--r-- 1 user group %d %s %s/text" % ( - len(text), self.xml_file_dt, subpath_encoded) + print("-r--r--r-- 1 user group %d %s %s/text" % ( + len(text), self.xml_file_dt, subpath_encoded)) self._list(element, subpath) def get_tag_node(self, node, i): @@ -471,5 +472,5 @@ try: g[procname]() except SystemExit: raise -except: +except Exception: logger.exception("Error during run")