-#!/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)
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):
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():
return True
- except Exception, e:
+ except Exception as e:
logging.warning("Loading cache: %s" % e)
return False
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):
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):
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()
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:
try:
asyncsched.loop()
except KeyboardInterrupt:
- print
+ print()
cycler.finish()
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:
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 ...]"
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__":