From a4b7aa61ed1e89cae05d079a44e520658323e927 Mon Sep 17 00:00:00 2001 From: Darshaka Pathirana Date: Fri, 7 May 2021 17:40:57 +0200 Subject: [PATCH] grepedit/notifyd.py: Convert to Python 3 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Run the following commands: % 2to3-2.7 -w usr_bin/grepedit % isort usr_bin/grepedit % black usr_bin/grepedit In grepedit the following print statement has been split manually: + print( + "%s:%s has changed since the grep command ran- " + "not modifying this line" % key + ) And: % 2to3-2.7 -w usr_bin/notifyd.py % black usr_bin/notifyd.py % isort usr_bin/notifyd.py In notifyd.py added some ignore statements/comments (for the examples which were actually comments). The following errors/warnings were fixed manually: ❯ flake8 --max-line-length 88 usr_bin/notifyd.py usr_bin/notifyd.py:201:1: E741 ambiguous variable name 'l' usr_bin/notifyd.py:214:5: E722 do not use bare 'except' Variable 'l' has been renamed to 'listen' and the bare 'except' has been changed to catch 'OSError' as described in the socket documentation: https://docs.python.org/3/library/socket.html grepedit + notifyd.py + osd_server.py has not been functionally tested. Please test. (But this is not the scope of this change, as it had to be dropped anyway as it is missing Python 3 support. If an errors occurs, please file a bug report.) black defaults to line length 88 and flake to 79. Decided to go set --max-line-length 88 for flake8. Note that osd_server.py is just a symlink to notifyd.py. --- Makefile | 4 +- usr_bin/grepedit | 100 ++++++++++++++++++++---------------- usr_bin/notifyd.py | 145 +++++++++++++++++++++++++++++++++-------------------- 3 files changed, 150 insertions(+), 99 deletions(-) diff --git a/Makefile b/Makefile index c122c0f..bcbfe07 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ pythoncheck: ## Run pythoncheck (flakecheck, isortcheck + blackcheck) @RETURN=0 @for pythonfile in usr_bin/* usr_sbin/* usr_share/*; do @ if head -1 "$${pythonfile}" | grep -q "python"; then - @ flake8 "$${pythonfile}" || RETURN=1 + @ flake8 --max-line-length 88 "$${pythonfile}" || RETURN=1 @ isort --check "$${pythonfile}" || RETURN=1 @ black --check "$${pythonfile}" || RETURN=1 @ fi @@ -34,7 +34,7 @@ flakecheck: ## Run flake8 only @RETURN=0 @for pythonfile in usr_bin/* usr_sbin/* usr_share/*; do @ if head -1 "$${pythonfile}" | grep -q "python"; then - @ flake8 "$${pythonfile}" || RETURN=1 + @ flake8 --max-line-length 88 "$${pythonfile}" || RETURN=1 @ fi @done @exit $${RETURN} diff --git a/usr_bin/grepedit b/usr_bin/grepedit index 2d5730b..9908ff2 100755 --- a/usr_bin/grepedit +++ b/usr_bin/grepedit @@ -1,20 +1,20 @@ #!/usr/bin/python -## Copyright 2005 Drew Perttula - -## This program is free software; you can redistribute it and/or modify -## it under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 2 of the License, or -## (at your option) any later version. - -## This program is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. - -## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# Copyright 2005 Drew Perttula + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """sometimes we grep for something in a bunch of files, and then we want to make edits to the lines that were returned from the grep. It's @@ -49,9 +49,14 @@ ez_setup.py-179- for name in filenames: """ -import tempfile, sys, os, re +import os +import re +import sys +import tempfile + from sets import Set + def grep_parse(filename): """parse grep output lines in given file into a dict of (filename, lineno) : text""" @@ -59,41 +64,50 @@ def grep_parse(filename): for line in open(filename): if line == "--\n": continue - m = re.match(r"(?P.*?)(?P[-:])(?P\d+)(?P=sep)(?P.*\n)$", line) + m = re.match( + r"(?P.*?)(?P[-:])(?P\d+)(?P=sep)(?P.*\n)$", + line, + ) if m is None: - print "couldn't parse grep result line %r" % line + print("couldn't parse grep result line %r" % line) continue - filename, lineno, text = (m.group('filename'), int(m.group('lineno')), - m.group('line')) - if (filename,lineno) in parse: - raise ValueError("multiple results found for %s:%s" % - (filename, lineno)) + filename, lineno, text = ( + m.group("filename"), + int(m.group("lineno")), + m.group("line"), + ) + if (filename, lineno) in parse: + raise ValueError("multiple results found for %s:%s" % (filename, lineno)) parse[(filename, lineno)] = text return parse + options = {} passthru_args = [] for arg in sys.argv[1:]: if arg == "--sort-text": - options['sort-text'] = True + options["sort-text"] = True continue passthru_args.append(arg) tf = tempfile.NamedTemporaryFile(prefix="grepedit_") tf.close() -cmd = ("grep --with-filename --line-number --binary-files=without-match %s > %s" % - (" ".join(['"%s"' % s.replace("\\","\\\\").replace('"','\\"') - for s in passthru_args]),tf.name)) +cmd = "grep --with-filename --line-number --binary-files=without-match %s > %s" % ( + " ".join( + ['"%s"' % s.replace("\\", "\\\\").replace('"', '\\"') for s in passthru_args] + ), + tf.name, +) os.system(cmd) originals = grep_parse(tf.name) -if options.get('sort-text', False): - orig = [(v,k) for k,v in originals.items()] +if options.get("sort-text", False): + orig = [(v, k) for k, v in list(originals.items())] orig.sort() f = open(tf.name, "w") - for text, (filename,lineno) in orig: + for text, (filename, lineno) in orig: f.write("%s:%s:%s" % (filename, lineno, text)) f.close() @@ -101,25 +115,29 @@ os.system("%s %s" % (os.getenv("EDITOR"), tf.name)) corrections = grep_parse(tf.name) -files = Set([filename for filename,lineno in corrections.keys()]) +files = Set([filename for filename, lineno in list(corrections.keys())]) for orig_filename in files: (copy_fd, copy_filename) = tempfile.mkstemp( dir=os.path.dirname(orig_filename), - prefix="_%s_tmp_grepedit" % os.path.basename(orig_filename)) + prefix="_%s_tmp_grepedit" % os.path.basename(orig_filename), + ) any_changes = False - for lineno,line in enumerate(open(orig_filename)): - lineno = lineno + 1 # grep is 1-based - key = orig_filename,lineno + for lineno, line in enumerate(open(orig_filename)): + lineno = lineno + 1 # grep is 1-based + key = orig_filename, lineno if key in corrections: if line != originals[key]: - print "%s:%s has changed since the grep command ran- not modifying this line" % key - print repr(line) - print repr(originals[key]) + print( + "%s:%s has changed since the grep command ran- " + "not modifying this line" % key + ) + print(repr(line)) + print(repr(originals[key])) elif corrections[key] == line: pass else: - print "%s:%s substituting new line" % key + print("%s:%s substituting new line" % key) line = corrections[key] any_changes = True os.write(copy_fd, line) @@ -128,7 +146,5 @@ for orig_filename in files: if any_changes: os.rename(copy_filename, orig_filename) else: - print "no changes made in file %s" % orig_filename + print("no changes made in file %s" % orig_filename) os.unlink(copy_filename) - - diff --git a/usr_bin/notifyd.py b/usr_bin/notifyd.py index 312c9b0..ec50fce 100755 --- a/usr_bin/notifyd.py +++ b/usr_bin/notifyd.py @@ -29,7 +29,7 @@ host = "127.0.0.1" port = 8901 logfile = os.path.expanduser("~/.event.log") osdparams = "-p bottom --color=red --delay=4 --age=4 " \ - "--font=\-\*\-helvetica\-medium\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-\*\-15 " \ + "--font=\-\*\-helvetica\-medium\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-\*\-15 " \ # noqa: W605,E501 "--offset=100 --shadow=0 --lines=5 --align=right --indent=100" actions = ( (".*", [log], True), @@ -40,78 +40,101 @@ actions = ( """ +import getopt +import logging import os -import sys import re -import string import socket -import logging -import getopt +import string import subprocess +import sys -default_hostname = 'localhost' +default_hostname = "localhost" default_port = 1234 -default_osd_params = osdparams = "-p bottom --color=red --delay=4 --age=4 " \ - "--font=\-\*\-helvetica\-medium\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-\*\-15 " \ - "--offset=100 --shadow=0 --lines=5 --align=right --indent=100" +default_osd_params = osdparams = ( + "-p bottom --color=red --delay=4 --age=4 " + "--font=\-\*\-helvetica\-medium\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-\*\-15 " # noqa: W605,E501 + "--offset=100 --shadow=0 --lines=5 --align=right --indent=100" +) default_logfile = None def play(sound_file): def play_wrapper(msg): - with open(os.devnull, 'w') as devnull: - subprocess.Popen(['/usr/bin/aplay', sound_file], stderr=devnull) + with open(os.devnull, "w") as devnull: + subprocess.Popen(["/usr/bin/aplay", sound_file], stderr=devnull) + return play_wrapper + def execute(command): def command_wrapper(msg): - subprocess.call(command % dict(msg = msg)) + subprocess.call(command % dict(msg=msg)) + return command_wrapper + def osd(msg): osdcmd = "/usr/bin/osd_cat" - osdpipe = os.popen("%s %s" % (osdcmd, osdparams), 'w') + osdpipe = os.popen("%s %s" % (osdcmd, osdparams), "w") osdpipe.write(msg) osdpipe.close() + def libnotify(msg): try: import dbus except ImportError: - sys.stderr.write('Please install python-dbus\n') + sys.stderr.write("Please install python-dbus\n") raise SystemExit(1) bus = dbus.SessionBus() - notifyService = bus.get_object("org.freedesktop.Notifications", '/org/freedesktop/Notifications') - interface = dbus.Interface(notifyService, 'org.freedesktop.Notifications') + notifyService = bus.get_object( + "org.freedesktop.Notifications", "/org/freedesktop/Notifications" + ) + interface = dbus.Interface(notifyService, "org.freedesktop.Notifications") - message, title = (':' + msg).split(':')[::-1][0:2] + message, title = (":" + msg).split(":")[::-1][0:2] if not title: title, message = message, title - interface.Notify('notify-server', 0, 'notification-message-im', title, message, [], {'x-canonical-append':'allowed'}, -1) + interface.Notify( + "notify-server", + 0, + "notification-message-im", + title, + message, + [], + {"x-canonical-append": "allowed"}, + -1, + ) + def log(msg): if logger: logger.info(msg) + def syntax(): - print "osd_server.py [options]" - print " options:" - print " -h --help print this message" - print " -H --host host of the osd server (def: " + default_hostname + ")" - print " -P --port port of the osd server (def: " + str(default_port) + ")" - print " -l --log log file ('-' logs to stdout)" - - -env = { 'play' : play, - 'execute' : execute, - 'osd' : osd, - 'libnotify' : libnotify, - 'log' : log, - 'host' : default_hostname, - 'port' : default_port, - 'logfile' : default_logfile, - } + print("osd_server.py [options]") + print(" options:") + print(" -h --help print this message") + print(" -H --host host of the osd server (def: " + default_hostname + ")") + print( + " -P --port port of the osd server (def: " + str(default_port) + ")" + ) + print(" -l --log log file ('-' logs to stdout)") + + +env = { + "play": play, + "execute": execute, + "osd": osd, + "libnotify": libnotify, + "log": log, + "host": default_hostname, + "port": default_port, + "logfile": default_logfile, +} default_actions = ( (".*", [log], True), @@ -122,12 +145,22 @@ default_actions = ( default_bind = (default_hostname, default_port) try: - execfile(os.path.expanduser('~/.notifyd.conf'), {}, env) + exec( + compile( + open(os.path.expanduser("~/.notifyd.conf"), "rb").read(), + os.path.expanduser("~/.notifyd.conf"), + "exec", + ), + {}, + env, + ) except IOError: pass try: - opts, args = getopt.getopt(sys.argv[1:], "hH:P:l:", ["help", "host=", "port=", "log="]) + opts, args = getopt.getopt( + sys.argv[1:], "hH:P:l:", ["help", "host=", "port=", "log="] + ) except getopt.GetoptError: syntax() sys.exit(2) @@ -137,23 +170,23 @@ for opt, arg in opts: syntax() sys.exit(3) elif opt in ("-H", "--host"): - env['host'] = arg + env["host"] = arg elif opt in ("-P", "--port"): - env['port'] = int(arg) + env["port"] = int(arg) elif opt in ("-l", "--log"): - env['logfile'] = arg + env["logfile"] = arg -actions = env.get('actions', default_actions) -logfile_name = env.get('logfile') -logfile_format = env.get('logformat', '%(asctime)s %(message)s') -bind_address = (env['host'], env['port']) -osd_params = env.get('osdparams', default_osd_params) +actions = env.get("actions", default_actions) +logfile_name = env.get("logfile") +logfile_format = env.get("logformat", "%(asctime)s %(message)s") +bind_address = (env["host"], env["port"]) +osd_params = env.get("osdparams", default_osd_params) if logfile_name: - logger = logging.getLogger('notify_server') + logger = logging.getLogger("notify_server") lformatter = logging.Formatter(logfile_format) - if logfile_name not in ('', '-'): + if logfile_name not in ("", "-"): lfh = logging.FileHandler(logfile_name) lfh.setFormatter(lformatter) logger.addHandler(lfh) @@ -165,23 +198,25 @@ if logfile_name: else: logger = None -l = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -l.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -l.bind(bind_address) -l.listen(5) +listen = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +listen.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +listen.bind(bind_address) +listen.listen(5) + def filter_char(c): - return c in string.printable + "äöüßÄÖÜ" and c or '_' + return c in string.printable + "äöüßÄÖÜ" and c or "_" + while 1: try: - (con, addr) = l.accept() - except: + (con, addr) = listen.accept() + except OSError: continue data = con.recv(50).strip() con.close() - log = ''.join(filter_char(c) for c in data) + log = "".join(filter_char(c) for c in data) for pattern, handlers, cont in actions: if re.match(pattern, log): -- 2.1.4