]> code.delx.au - bg-scripts/commitdiff
Converted to Python3
authorJames Bunton <jamesbunton@delx.net.au>
Tue, 11 Nov 2014 09:11:04 +0000 (20:11 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Tue, 11 Nov 2014 09:11:04 +0000 (20:11 +1100)
asyncsched.py
randombg.py
wallchanger.py

index 22eb78808e1e307ba3ef2dd01ee8e671d43fa020..5fd8348ff2b3be4ba0a4a6f3a2ec4a9feeb3365e 100644 (file)
@@ -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
index 63e94605d2146d85c902e38dff760b373a0284cc..fa775de535a021951553db376e2f5ec52fcd725d 100755 (executable)
@@ -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__":
index 8bfb69bd7b1a64949b11c59cb5bf26e46ff66869..30f1400087288ee2e888f6fed9a16ca7cd6084bd 100755 (executable)
@@ -1,11 +1,11 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 
 # Copyright 2008 Greg Darke <greg@tsukasa.net.au>
 # Copyright 2008 James Bunton <jamesbunton@fastmail.fm>
 # 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)