From 1faf9950610cb542094133beb0f17401237fc471 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Tue, 11 Nov 2014 20:11:04 +1100 Subject: [PATCH] Converted to Python3 --- asyncsched.py | 7 +++++-- randombg.py | 49 +++++++++++++++++++------------------------------ wallchanger.py | 10 +++++----- 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/asyncsched.py b/asyncsched.py index 22eb788..5fd8348 100644 --- a/asyncsched.py +++ b/asyncsched.py @@ -15,8 +15,11 @@ class Task(object): self.time = time.time() + delay self.func = lambda: func(*args, **kwargs) - def __cmp__(self, other): - return cmp(self.time, other.time) + def __lt__(self, other): + return self.time < other.time + + def __eq__(self, other): + return self.time == other.time def __call__(self): f = self.func 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__": diff --git a/wallchanger.py b/wallchanger.py index 8bfb69b..30f1400 100755 --- a/wallchanger.py +++ b/wallchanger.py @@ -1,11 +1,11 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # Copyright 2008 Greg Darke # Copyright 2008 James Bunton # Licensed for distribution under the GPL version 2, check COPYING for details # This is a cross platform/cross window manager way to change your wallpaper -import commands, sys, os, os.path, time +import subprocess, sys, os, os.path, time import logging try: import PIL, PIL.Image @@ -24,7 +24,7 @@ def set_image(filename): logging.warning("Failed to set background: wallchanger.set_image(%s), changer=%s", filename, changer) def check_cmd(cmd): - return commands.getstatusoutput(cmd)[0] == 0 + return subprocess.getstatusoutput(cmd)[0] == 0 def init(*args, **kwargs): """Desktop Changer factory""" @@ -204,7 +204,7 @@ class OSXChanger(BaseChanger): return False cmd = """osascript -e 'tell application "finder" to set desktop picture to posix file "%s"'""" % filename logging.debug(cmd) - return not commands.getstatusoutput(cmd)[0] + return not subprocess.getstatusoutput(cmd)[0] class WIN32Changer(BaseChanger): name = "Windows" @@ -284,7 +284,7 @@ if __name__ == "__main__": try: filename = sys.argv[1] except: - print >>sys.stderr, "Usage: %s filename" % sys.argv[0] + print("Usage: %s filename" % sys.argv[0], file=sys.stderr) sys.exit(1) main(filename) -- 2.39.2