diff options
author | Miklos Vajna <vmiklos@vmiklos.hu> | 2013-01-13 12:41:21 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@vmiklos.hu> | 2013-01-13 12:41:21 +0100 |
commit | fffabad4863460fde5906e71c6c3cb79a4c4bab3 (patch) | |
tree | 4c5add6774d9e45fff264fc236489dcc37f06df2 /protocols | |
parent | 4cb21b79b3ade188b628dfc877cfa5fd6f8d5d03 (diff) |
skyped: add mock mode
Diffstat (limited to 'protocols')
-rw-r--r-- | protocols/skype/skyped.py | 58 | ||||
-rw-r--r-- | protocols/skype/skyped.txt | 3 |
2 files changed, 53 insertions, 8 deletions
diff --git a/protocols/skype/skyped.py b/protocols/skype/skyped.py index 00de898b..c6224d78 100644 --- a/protocols/skype/skyped.py +++ b/protocols/skype/skyped.py @@ -2,7 +2,7 @@ # # skyped.py # -# Copyright (c) 2007, 2008, 2009, 2010, 2011 by Miklos Vajna <vmiklos@frugalware.org> +# Copyright (c) 2007-2013 by Miklos Vajna <vmiklos@frugalware.org> # # 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 @@ -275,11 +275,46 @@ def dprint(msg): sock.write("%s: %s\n" % (now, msg)) sock.close() +class MockedSkype: + """Mock class for Skype4Py.Skype(), in case the -m option is used.""" + def __init__(self, mock): + sock = open(mock) + self.lines = sock.readlines() + + def SendCommand(self, c): + pass + + def Command(self, msg, Block): + if msg == "PING": + return ["PONG"] + line = self.lines[0].strip() + if not line.startswith(">> "): + raise Exception("Corrupted mock input") + line = line[3:] + if line != msg: + raise Exception("'%s' != '%s'" % (line, msg)) + self.lines = self.lines[1:] # drop the expected incoming line + ret = [] + while True: + # and now send back all the following lines, up to the next expected incoming line + if len(self.lines) == 0: + break + if self.lines[0].startswith(">> "): + break + if not self.lines[0].startswith("<< "): + raise Exception("Corrupted mock input") + ret.append(self.lines[0][3:].strip()) + self.lines = self.lines[1:] + return ret + class SkypeApi: - def __init__(self): - self.skype = Skype4Py.Skype() - self.skype.OnNotify = self.recv - self.skype.Client.Start() + def __init__(self, mock): + if not mock: + self.skype = Skype4Py.Skype() + self.skype.OnNotify = self.recv + self.skype.Client.Start() + else: + self.skype = MockedSkype(mock) def recv(self, msg_text): global options @@ -334,7 +369,11 @@ class SkypeApi: try: c = self.skype.Command(e, Block=True) self.skype.SendCommand(c) - self.recv(c.Reply) + if hasattr(c, "Reply"): + self.recv(c.Reply) # Skype4Py answer + else: + for i in c: # mock may return multiple iterable answers + self.recv(i) except Skype4Py.SkypeError: pass except Skype4Py.SkypeAPIError, s: @@ -359,6 +398,7 @@ class Options: self.conn = None # this will be read first by the input handler self.buf = None + self.mock = None def usage(self, ret): @@ -421,7 +461,7 @@ def serverloop(options, skype): if __name__=='__main__': options = Options() try: - opts, args = getopt.getopt(sys.argv[1:], "c:dhH:l:np:v", ["config=", "debug", "help", "host=", "log=", "nofork", "port=", "version"]) + opts, args = getopt.getopt(sys.argv[1:], "c:dhH:l:m:np:v", ["config=", "debug", "help", "host=", "log=", "mock=", "nofork", "port=", "version"]) except getopt.GetoptError: options.usage(1) for opt, arg in opts: @@ -435,6 +475,8 @@ if __name__=='__main__': options.host = arg elif opt in ("-l", "--log"): options.log = arg + elif opt in ("-m", "--mock"): + options.mock = arg elif opt in ("-n", "--nofork"): options.daemon = False elif opt in ("-p", "--port"): @@ -485,7 +527,7 @@ if __name__=='__main__': if hasgobject: server(options.host, options.port) try: - skype = SkypeApi() + skype = SkypeApi(options.mock) except Skype4Py.SkypeAPIError, s: sys.exit("%s. Are you sure you have started Skype?" % s) if hasgobject: diff --git a/protocols/skype/skyped.txt b/protocols/skype/skyped.txt index fe0b4d50..218b6736 100644 --- a/protocols/skype/skyped.txt +++ b/protocols/skype/skyped.txt @@ -38,6 +38,9 @@ See the README for information about how to configure this daemon. -l, --log:: Set the log file in background mode (default: none) +-m, --mock=<file>:: + Mock mode: replay session from file, instead of connecting to Skype. + -n, --nofork:: Don't run as daemon in the background |