def populate_objects(self): self.config = Settings() self.log = ChannelLogger(stdout=self.write, default_sns=False) self.users = UserManager(stdout=self.log.message, stddebug=self.log.debug) self.users.load() self.events = EventManager(stdout=self.log.message, stddebug=self.log.debug) self.client = Client( stdout=self.log.message, stddebug=self.log.debug, _events=self.events, _teardown=self.teardown, ) self.rules = RulesetBattery(stdout=self.log.message, stddebug=self.log.debug) self.exts = ReactorBattery(stdout=self.log.message, stddebug=self.log.debug) self.rules.load_objects(self.events, rules, core=self) self.exts.load_objects(self.events, extensions, 'Extension', self)
class Bot(object): class platform: """ Information about the slate platform. """ name = 'Slate' version = 1 state = 'Alpha (repo)' build = 2 stamp = '11122011-032934' series = 'Chip' author = 'photofroggy' config = None log = None client = None events = None agent = None debug = False restartable = False close = False restart = False def __init__(self, debug=False, restartable=True): self.platform.stamp = time.strftime('%d%m%Y-%H%M%S') self.debug = debug self.restartable = restartable logging.LEVEL.MESSAGE+= logging.LEVEL.WARNING logging.LEVEL.WARNING = logging.LEVEL.MESSAGE - logging.LEVEL.WARNING logging.LEVEL.MESSAGE-= logging.LEVEL.WARNING self.populate_objects() if debug: self.log.set_level(logging.LEVEL.DEBUG) self.log.start() self.hello() self.set_agent() self.start_configure() def populate_objects(self): self.config = Settings() self.log = ChannelLogger(stdout=self.write, default_sns=False) self.users = UserManager(stdout=self.log.message, stddebug=self.log.debug) self.users.load() self.events = EventManager(stdout=self.log.message, stddebug=self.log.debug) self.client = Client( stdout=self.log.message, stddebug=self.log.debug, _events=self.events, _teardown=self.teardown, ) self.rules = RulesetBattery(stdout=self.log.message, stddebug=self.log.debug) self.exts = ReactorBattery(stdout=self.log.message, stddebug=self.log.debug) self.rules.load_objects(self.events, rules, core=self) self.exts.load_objects(self.events, extensions, 'Extension', self) def start_configure(self): self.config.load() if self.config.api.username is None: c=Configure( reactor, '110', '605c4a06216380fbdff26228c53cf610', agent=self.agent, stdout=self.log.message, stddebug=self.log.debug ) if c.d is not None: c.d.addCallback(self.configured) reactor.run() return self.configured({'status': True, 'response': None}) def set_agent(self): uname = platform.uname() release, name, version = uname[:3] self.agent = 'slate/{0}/{1}.{2} (dAmnViper/{3}/{4}.{5}; reflex/{6}/{7}.{8}; stutter/1) {9} {10}'.format( self.platform.stamp, self.platform.version, self.platform.build, self.client.platform.stamp, self.client.platform.version, self.client.platform.build, self.events.info.stamp, self.events.info.version, self.events.info.build, '({0}; U; {1} {2}; en-GB; {3}) '.format(name, release, version, self.config.owner), 'Python/{0}.{1}'.format(sys.version_info[0], sys.version_info[1] ) ) self.client.agent = self.agent def hello(self): """ Greet the user, dawg. """ self.log.message('** Welcome to {0} {1}.{2} {3}!'.format( self.platform.name, self.platform.version, self.platform.build, self.platform.state ), showns=False) self.log.message('** Created by photofroggy.', showns=False) def configured(self, response): self.config.load() if response['status'] is False or self.config.api.username is None: self.teardown() return False self.client.user.username = self.config.api.username self.client.user.token = self.config.api.damntoken self.client.autojoin = self.config.autojoin self.client.owner = self.config.owner self.client.trigger = self.config.trigger self.users.load(owner=self.config.owner) self.client.start() try: reactor.run() except Exception: pass def teardown(self): self.close = self.client.flag.close self.restart = self.client.flag.restart try: reactor.stop() except Exception: pass self.log.message('** Exiting...', showns=False) self.log.stop() self.log.push(0) def write(self, msg, *args, **kwargs): try: sys.stdout.write(msg) sys.stdout.flush() except Exception as e: self.log.warning('>> Failed to display a message...', showns=False)
def run(args, restartable=True): args = args[2:].lower() from_menu = False if not args in ('bot', 'debug', 'config', 'exit') or args == 'menu': restartable = False from_menu = True sys.stdout.write( '===========> Main Menu <============\n>>> Welcome!\n>>> Please select an option!\n' ) sys.stdout.write('>> Bot - Run the bot.\n') sys.stdout.write('>> Debug - Run the bot in debug mode.\n') sys.stdout.write('>> Config - Configure the bot.\n') sys.stdout.write('>> Exit - Close this program.\n') sys.stdout.flush() while not args in ('bot', 'debug', 'config', 'exit'): args = get_input().lower() if args in ('bot', 'debug'): from slate import core ret = core.Bot(args == 'debug', restartable) sys.stdout.write(('=' * 80) + '\n') if ret.close and not (ret.restart and restartable): return argv = [(str(i) if not k else i) for k, i in enumerate(sys.argv)] if ret.restart and restartable: subprocess.Popen([sys.executable] + argv) return resp = '' while not resp in ('y', 'n'): resp = get_input('>> Would you like to reboot?[Y|N]: ').lower() if resp == 'y': sys.stdout.write(('=' * 80) + '\n') subprocess.Popen([sys.executable] + argv) return raw_input('>> Press enter to continue...') return if args == 'config': from slate.config import Configure log = ChannelLogger(default_sns=False) c = Configure(reactor, '110', '605c4a06216380fbdff26228c53cf610', agent='slate config', option=1, stdout=log.message, stddebug=log.debug) if c.d is not None: reactor.run() def stop(obj, restartable): try: reactor.stop() except Exception: pass if restartable: run('--menu', False) c.d.addCallback(stop, restartable) reactor.run() return run('--menu', False) return if args == 'exit': return run('--menu', False)