X-Git-Url: https://code.delx.au/bg-scripts/blobdiff_plain/fd176b77f01e67307c9899735ecac01939b2aebf..1faf9950610cb542094133beb0f17401237fc471:/randombg.py diff --git a/randombg.py b/randombg.py index 63e9460..fa775de 100755 --- a/randombg.py +++ b/randombg.py @@ -1,33 +1,22 @@ -#!/usr/bin/env python2 - -VERSION = "2.1" - +#!/usr/bin/env python3 import asyncore import asynchat import socket import os +import pickle import random import sys import time from optparse import OptionParser import logging -try: - logging.basicConfig(format="%(levelname)s: %(message)s") -except TypeError: -# Python 2.3's logging.basicConfig does not support parameters - logging.basicConfig() - -try: - import cPickle as pickle -except ImportError: - import pickle +logging.basicConfig(format="%(levelname)s: %(message)s") try: # Required libraries import asyncsched import wallchanger -except ImportError, e: +except ImportError as e: logging.critical("Missing libraries! Exiting...", exc_info=1) sys.exit(1) @@ -56,7 +45,7 @@ class BaseFileList(object): fd = open(filename, 'wb') pickle.dump(self, fd, 2) logging.debug("Cache successfully stored") - except Exception, e: + except Exception as e: warning("Storing cache: %s" % e) def load_cache(self, filename): @@ -72,7 +61,7 @@ class BaseFileList(object): tmp.paths.sort() if self.paths != tmp.paths: - raise ValueError, "Path list changed" + raise ValueError("Path list changed") # Overwrite this object with the other for attr, value in tmp.__dict__.items(): @@ -80,7 +69,7 @@ class BaseFileList(object): return True - except Exception, e: + except Exception as e: logging.warning("Loading cache: %s" % e) return False @@ -165,7 +154,7 @@ class AllRandomFileList(BaseFileList): fd = open(filename, 'wb') pickle.dump(self, fd, 2) logging.debug("Cache successfully stored") - except Exception, e: + except Exception as e: logging.warning("Storing cache", exc_info=1) def get_current_image(self): @@ -207,7 +196,7 @@ class FolderRandomFileList(BaseFileList): logging.debug('Added path "%s" to the list' % path) for dirpath, dirs, filenames in os.walk(path): logging.debug('Scanning "%s" for images' % dirpath) - if self.directories.has_key(dirpath): + if dirpath in self.directories: continue filenames = list(filter_images(filenames)) if len(filenames): @@ -321,10 +310,10 @@ class Server(asynchat.async_chat): asynchat.async_chat.__init__(self, sock) self.cycler = cycler self.ibuffer = [] - self.set_terminator("\n") + self.set_terminator(b'\n') def collect_incoming_data(self, data): - self.ibuffer.append(data) + self.ibuffer.append(data.decode("ascii")) def found_terminator(self): line = "".join(self.ibuffer).lower() @@ -369,11 +358,11 @@ def do_server(options, paths): try: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(options.socket_filename) - print >>sys.stderr, "Server is already running! Sending exit command." - sock = sock.makefile() + print("Server is already running! Sending exit command.", file=sys.stderr) + sock = sock.makefile("w") sock.write("cmd exit\n") sock.close() - except Exception, e: + except Exception as e: pass try: @@ -388,7 +377,7 @@ def do_server(options, paths): try: asyncsched.loop() except KeyboardInterrupt: - print + print() cycler.finish() def do_client(options, args): @@ -396,7 +385,7 @@ def do_client(options, args): args = ["next"] sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(options.socket_filename) - sock = sock.makefile() + sock = sock.makefile(mode="w") for i, cmd in enumerate(args): sock.write("cmd %s\n" % cmd) if i < len(args) - 1: @@ -408,7 +397,7 @@ def do_oneshot(options, paths): cycler.init(options, paths, oneshot=True) def build_parser(): - parser = OptionParser(version="%prog " + VERSION, + parser = OptionParser( description = "Cycles through random background images.", usage = "\n(server) %prog [options] dir [dir2 ...]" @@ -467,8 +456,8 @@ def main(): try: do_client(options, args) return - except Exception, e: - print >>sys.stderr, "Failed to connect to server:", e + except Exception as e: + print("Failed to connect to server:", e, file=sys.stderr) if __name__ == "__main__":