Exemplo n.º 1
0
 def runTest(self):
     
     sys.stdout.write('\n')
     
     # Load an event system
     events = EventManager() #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))
     
     # Load rulesets
     rulesets = RulesetBattery() #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))
     rulesets.load_objects(events, rules)
     
     self.assertIsNotNone(events.rules.get('example', None),
         'Failed to load test ruleset')
     
     # Load plugins!
     plugins = ReactorBattery() #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))
     plugins.load_objects(events, reactors, 'Plugin')
     
     self.assertIsNotNone(events.map.get('example', None),
         'Event bindings failed')
     
     events.rules['example'].reset_called()
     
     # Fire an event!
     events.trigger(Event('example'))
     
     # Test Results!
     self.assertTrue(events.rules['example'].called,
         'Example ruleset not called properly')
     
     self.assertTrue(plugins.loaded['Example'].full_test,
         'Example reactor not called properly')
Exemplo n.º 2
0
    def runTest(self):

        sys.stdout.write('\n')

        # Load an event system
        events = EventManager(
        )  #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))

        # Load rulesets
        rulesets = RulesetBattery(
        )  #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))
        rulesets.load_objects(events, rules)

        self.assertIsNotNone(events.rules.get('example', None),
                             'Failed to load test ruleset')

        # Load plugins!
        plugins = ReactorBattery(
        )  #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))
        plugins.load_objects(events, reactors, 'Plugin')

        self.assertIsNotNone(events.map.get('example', None),
                             'Event bindings failed')

        events.rules['example'].reset_called()

        # Fire an event!
        events.trigger(Event('example'))

        # Test Results!
        self.assertTrue(events.rules['example'].called,
                        'Example ruleset not called properly')

        self.assertTrue(plugins.loaded['Example'].full_test,
                        'Example reactor not called properly')
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
    def test_loading_reactors(self):
        # Test loading reactors into a battery.
        # sys.stdout.write('\n')
        # Add `stddebug=debug` to the constructors here to see debug messages
        # during the tests.
        good_battery = ReactorBattery(lambda n: None)
        bad_battery = ReactorBattery(lambda n: None)

        # This should fail
        bad_battery.load_objects(self.events, reactors, 'Extension')
        self.assertFalse(
            len(bad_battery.loaded) > 0,
            'Reactors loaded into the wrong battery')

        # This should do the trick
        good_battery.load_objects(self.events, reactors, 'Plugin')
        self.assertTrue(
            len(good_battery.loaded) > 0,
            'Reactors not loaded by the right battery')
Exemplo n.º 5
0
 def test_loading_reactors(self):
     # Test loading reactors into a battery.
     # sys.stdout.write('\n')
     # Add `stddebug=debug` to the constructors here to see debug messages
     # during the tests.
     good_battery = ReactorBattery(lambda n: None)
     bad_battery = ReactorBattery(lambda n: None)
     
     # This should fail
     bad_battery.load_objects(self.events, reactors, 'Extension')
     self.assertFalse(len(bad_battery.loaded) > 0,
         'Reactors loaded into the wrong battery')
     
     # This should do the trick
     good_battery.load_objects(self.events, reactors, 'Plugin')
     self.assertTrue(len(good_battery.loaded) > 0, 'Reactors not loaded by the right battery')
Exemplo n.º 6
0
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)