#!/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
"""
-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"""
for line in open(filename):
if line == "--\n":
continue
- m = re.match(r"(?P<filename>.*?)(?P<sep>[-:])(?P<lineno>\d+)(?P=sep)(?P<line>.*\n)$", line)
+ m = re.match(
+ r"(?P<filename>.*?)(?P<sep>[-:])(?P<lineno>\d+)(?P=sep)(?P<line>.*\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()
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)
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)
-
-
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),
"""
+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),
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)
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)
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):