def test_timeout_update_available(self): """ Make sure that we get a status update if the selected updater indicates that an update is available. """ mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(StableUpdater, "__init__") self.mox.StubOutWithMock(StableUpdater, "check") self.mox.StubOutWithMock(StableUpdater, "get_update_version") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(Client, "change_status") StableUpdater.__init__(self.__repo, self.__remote_url) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) StableUpdater.get_update_version().AndReturn("1.0") Client.change_status(mox.IgnoreArg()) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() self.mox.ReplayAll() notifyer = UpdateNotifyer(self.__repo, self.__remote_url) notifyer.start() notifyer.timeout()
def run(self): """ This method provides the main function of the daemon, i.e. connecting the XMPP client, and entering the application message loop. """ try: self.__setup_logging() client = Client(JID(self.__usr), self.__pwd) client.connect() provider = StatusProvider() provider.start() update_handler = updates.get_update_handler() if update_handler: update_handler.start() client.loop(1) except Exception, exc: logger = logging.getLogger() logger.critical(u"encountered exception %s. terminating XMPPMote." % repr(exc)) for line in filter(None, traceback.format_exc().split("\n")): logger.critical(line)
def stop(self): """ This method overrides Daemon.stop in order to disconnect the session when stopping the daemon. """ client = Client() client.disconnect() Daemon.stop(self)
def main(): """Main function""" intents = discord.Intents.default() intents.members = True bot = Client(intents=intents) if bot.error_code == 0: bot.run(constants.BOT_TOKEN) return bot.error_code
def test_duplicated_command_output(self): """ Make sure that StatusProvider only updates the status if the command yields a different result than it did previously. """ self.__setup_parser() mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(SafeConfigParser, "has_section") self.mox.StubOutWithMock(SafeConfigParser, "get") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(subprocess.Popen, "__init__") self.mox.StubOutWithMock(subprocess.Popen, "communicate") self.mox.StubOutWithMock(Client, "change_status") SafeConfigParser.has_section("status").AndReturn(True) SafeConfigParser.get("status", "command").AndReturn("foobar") SafeConfigParser.get("status", "interval").AndReturn(3) threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE) subprocess.Popen.communicate().AndReturn(("result", "")) Client.change_status("result") threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE) subprocess.Popen.communicate().AndReturn(("result", "")) # This time we expect no status change, since this is the same result as # returned previously.. #Client.change_status("result") threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE) subprocess.Popen.communicate().AndReturn(("another result", "")) Client.change_status("another result") self.mox.ReplayAll() provider = StatusProvider() provider.start() # We'll have to emulate a timeout here.. provider.timeout() provider.timeout() provider.timeout()
def test_toggle_update_availability(self): """ Make sure that the update indicaiton is reset if an update all of a sudden is not available. """ mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(StableUpdater, "__init__") self.mox.StubOutWithMock(StableUpdater, "check") self.mox.StubOutWithMock(StableUpdater, "get_update_version") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(Client, "change_status") StableUpdater.__init__(self.__repo, self.__remote_url) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) StableUpdater.get_update_version().AndReturn("1.0") Client.change_status(mox.IgnoreArg()) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) StableUpdater.get_update_version().AndReturn("1.0") threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(False) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) Client.change_status(mox.IgnoreArg()) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() self.mox.ReplayAll() notifyer = UpdateNotifyer(self.__repo, self.__remote_url) notifyer.start() notifyer.timeout() notifyer.timeout() notifyer.timeout() notifyer.timeout()
def timeout(self): """ Called upon by the Timer when self.__interval has elapsed. """ had_update = self.has_update self.has_update = self.__updater.check() if self.has_update and not had_update: cli = Client() new_version = self.__updater.get_update_version() if new_version: cli.change_status(u"update available: %s" % new_version[:7]) self.start()
def timeout(self): """ This method is responsible for executing the configured command. """ try: subproc = subprocess.Popen(self.__command, stdout = subprocess.PIPE) stdout = subproc.communicate()[0] except OSError: logger = logging.getLogger() logger.error("%s: error executing status command.") return if self.__previous_result != stdout: client = Client() client.change_status(stdout) self.start() self.__previous_result = stdout
def do_command(self, command, args=None): """ Overridden in order to provide the restricted command set feature. """ if "bye" == command: client = Client() client.change_status(u"terminating session", False) client.disconnect() return u"terminating" cmd = [command] if args: cmd.extend(args) try: body = self.make_syscall(cmd) except OSError as ex: body = "%s: %s (%d)" % (type(ex), ex.strerror, ex.errno) return body
def test_existing_status_config_section(self): """ If we have an existing, properly configured status section, StatusProvider will, after having been started, execute the configured command after the configured timeout has elapsed, by starting a threading.Timer. """ self.__setup_parser() mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(SafeConfigParser, "has_section") self.mox.StubOutWithMock(SafeConfigParser, "get") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(subprocess.Popen, "__init__") self.mox.StubOutWithMock(subprocess.Popen, "communicate") self.mox.StubOutWithMock(Client, "change_status") SafeConfigParser.has_section("status").AndReturn(True) SafeConfigParser.get("status", "command").AndReturn("foobar") SafeConfigParser.get("status", "interval").AndReturn(3) threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE) subprocess.Popen.communicate().AndReturn(("result", "")) Client.change_status("result") threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() self.mox.ReplayAll() provider = StatusProvider() provider.start() # We'll have to emulate a timeout here.. provider.timeout()
def test_timeout_update_still_available(self): """ Make sure that we do not spam with software update notices. Once there has been an update notification, updates found thereafter should not be indicated. """ mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(StableUpdater, "__init__") self.mox.StubOutWithMock(StableUpdater, "check") self.mox.StubOutWithMock(StableUpdater, "get_update_version") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(Client, "change_status") StableUpdater.__init__(self.__repo, self.__remote_url) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) Client.change_status(mox.IgnoreArg()) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) StableUpdater.get_update_version().AndReturn("1.0") threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() self.mox.ReplayAll() notifyer = UpdateNotifyer(self.__repo, self.__remote_url) notifyer.start() notifyer.timeout() notifyer.timeout()
def test_xmppmotedaemon_run(self): """ Test the run method of the XMPPMoteDaemon, using Mox in order to verify proper connection procedure. """ mock_update_handler = self.mox.CreateMockAnything() self.mox.StubOutWithMock(StatusProvider, "__init__") self.mox.StubOutWithMock(StatusProvider, "start") self.mox.StubOutWithMock(Client, "__init__") self.mox.StubOutWithMock(Client, "connect") self.mox.StubOutWithMock(Client, "loop") self.mox.StubOutWithMock(Client, "disconnect") self.mox.StubOutWithMock(Daemon, "start") self.mox.StubOutWithMock(xmppmoted.XMPPMoteDaemon, "_XMPPMoteDaemon__parse_config_file") self.mox.StubOutWithMock(xmppmoted.XMPPMoteDaemon, "_XMPPMoteDaemon__get_pidfile") self.mox.StubOutWithMock(credentials, "get_credentials") self.mox.StubOutWithMock(updates, "get_update_handler") xmppmoted.XMPPMoteDaemon._XMPPMoteDaemon__parse_config_file() xmppmoted.XMPPMoteDaemon._XMPPMoteDaemon__get_pidfile().AndReturn(None) Daemon.start(mox.IgnoreArg()) credentials.get_credentials().AndReturn((self.__usr, self.__pwd)) StatusProvider.__init__() StatusProvider.start() updates.get_update_handler().AndReturn(mock_update_handler) mock_update_handler.start() Client.__init__(JID(self.__usr), self.__pwd) Client.connect() Client.loop(1) Client.disconnect() self.mox.ReplayAll() daemon = xmppmoted.XMPPMoteDaemon() daemon.start() daemon.run()
def main(): client = Client() file_handler = logging.FileHandler(filename="output.log", encoding="utf-8", mode="w") if "--no-log-discord" in sys.argv: handlers = [file_handler, logging.StreamHandler()] else: handlers = [ DiscordLogHandler(client), file_handler, logging.StreamHandler() ] logging.basicConfig( format="%(asctime)s | %(name)10s | %(levelname)8s | %(message)s", level=logging.DEBUG if "--debug" in sys.argv else logging.INFO, handlers=handlers) logging.getLogger("discord").setLevel(logging.WARNING) logging.getLogger("websockets.protocol").setLevel(logging.INFO) client.run(client.get_token(), bot=True)
def main(): """Main function""" bot = Client() if bot.error_code == 0: bot.run(constants.BOT_TOKEN) return bot.error_code
import asyncio import os from bot.client import Client discord_token = os.environ['DISCORD_TOKEN'] # Create our eventloop and put it into discord client # and the Client class will put it into the aioredis client loop = asyncio.get_event_loop() discord = Client(loop=loop) discord.run(discord_token)