def test_return_event_list(self): e = Event(time(), 'foo') p = EventProcessor(e) p.start() p.join() self.assertEqual(p.returnEvents,[])
def __init_broker_and_processors(self): self.__broker = Broker(self) self.__event_processor = EventProcessor(0, self) self.__event_processors = list() for i in range(self.config.processor_num): id = i + 1 event_channel = EventChannel() event_channel.register(self.__broker) event_processor = EventProcessor(id, self, event_channel) self.__event_processors.append(event_processor)
def callback(*args): from event_processor import EventProcessor handler = MessageHandler._singleton msg = f(*args) if(handler._valid_message(msg)): print("RECEIVED A VALID MESSAGE") EventProcessor.handle_message(msg.event) handler.emit_event(msg.event, msg.sender, msg.id) else: # Register the sender of the message # in case multiple people are sending us the same message # that way we won't send to them print("RECEIVED AN INVALID MESSAGE") Message.register(msg.id, {msg.sender})
def callback(*args): from event_processor import EventProcessor handler = MessageHandler._singleton msg = f(*args) if (handler._valid_message(msg)): print("RECEIVED A VALID MESSAGE") EventProcessor.handle_message(msg.event) handler.emit_event(msg.event, msg.sender, msg.id) else: # Register the sender of the message # in case multiple people are sending us the same message # that way we won't send to them print("RECEIVED AN INVALID MESSAGE") Message.register(msg.id, {msg.sender})
def __init__(self, db): self.app = Flask(__name__) self.app.config['DEBUG'] = True self.app.config['db'] = db self.app.config['ep'] = EventProcessor(db) self.app.register_blueprint(tools)
def main(argv=None): if argv is None: argv = sys.argv if len(argv) == 2: config_file = argv[1] elif len(argv) > 2: print("Usage: " + argv[0] + " <config_file>") print("Config file defaults to /etc/monasca/monasca_event.yaml") return 1 else: config_file = '/etc/monasca/monasca_event.yaml' config = yaml.load(open(config_file, 'r')) # Setup logging logging.config.dictConfig(config['logging']) # create EventProcessor(s) num_event_processors = config['processors']['event']['number'] log.info('num_event_processors %d', num_event_processors) for x in xrange(0, num_event_processors): event_processor = multiprocessing.Process(target=EventProcessor( config['kafka'], config['winchester']['winchester_config']).run) processors.append(event_processor) # create PipelineProcessor(s) num_pipeline_processors = config['processors']['pipeline']['number'] log.info('num_pipeline_processors %d', num_pipeline_processors) for x in xrange(0, num_pipeline_processors): print("constructing PipelineProcessor") pipeline_processor = multiprocessing.Process(target=PipelineProcessor( config['kafka'], config['winchester']['winchester_config']).run) processors.append(pipeline_processor) # Start try: log.info('Starting processes') for process in processors: process.start() # The signal handlers must be added after the processes start otherwise # they run on all processes signal.signal(signal.SIGCHLD, clean_exit) signal.signal(signal.SIGINT, clean_exit) signal.signal(signal.SIGTERM, clean_exit) while True: time.sleep(5) except Exception: log.exception('Error! Exiting.') for process in processors: process.terminate()
def callback(*args): event = f(*args) EventProcessor.handle_detection(event)
def __init__(self, event, delay): EventProcessor.__init__(self, event) self.delay = delay self.returnEvents = []
class TestEventProcessor(unittest.TestCase): """ Test cases for EventProcessor. """ def setUp(self): self.mock_session = Mock() self.mock_notifier = Mock() self.mock_compute_engine = Mock() self.event_processor = EventProcessor(self.mock_notifier, self.mock_compute_engine) def testNotifierReceivesSessionStarted(self): """ Verify that notifier receives a `SessionStarted' message. Plan: 1. Create a SessionStatus admin event using blpapi.test.createEvent(). 2. Obtain the message schema using blpapi.test.getAdminMessageDefinition(). 3. Append a message of type SessionStarted using blpapi.test.appendMessage(). 4. Verify that the expected message is passed to notifier.log_session_state(). """ event = blpapi.test.createEvent(blpapi.Event.SESSION_STATUS) session_started = blpapi.Name("SessionStarted") schema_def = blpapi.test.getAdminMessageDefinition(session_started) blpapi.test.appendMessage(event, schema_def) self.event_processor.processEvent(event, self.mock_session) self.mock_notifier.log_session_state.assert_called_once() actual_message = self.mock_notifier.log_session_state.call_args[0][0] self.assertEqual(session_started, actual_message.messageType()) def testNotifierReceivesSubscriptionStarted(self): """ Verify that notifier receives a `SubscriptionStarted` message. Plan: 1. Create a SubscriptionStatus admin event using blpapi.test.createEvent(). 2. Obtain the schema for SubscriptionStarted message using blpapi.test.getAdminMessageDefinition(). 3. Append a message of type `SubscriptionStarted` using blpapi.test.appendMessage(). 4. Verify that the expected message is passed to notifier.log_subscription_state(). """ event = blpapi.test.createEvent(blpapi.Event.SUBSCRIPTION_STATUS) subscription_started = blpapi.Name("SubscriptionStarted") schema_def = blpapi.test.getAdminMessageDefinition(subscription_started) blpapi.test.appendMessage(event, schema_def) self.event_processor.processEvent(event, self.mock_session) self.mock_notifier.log_subscription_state.assert_called_once() actual_message = \ self.mock_notifier.log_subscription_state.call_args[0][0] self.assertEqual(subscription_started, actual_message.messageType()) def testNotifierReceivesSubscriptionData(self): """ Verify that: 1. Compute engine receives the correct LAST_PRICE. 2. Compute engine performs the correct computation and returns the correct value. 3. The correct value is sent to the terminal. Plan: 1. Obtain the service by deserializing its schema. 2. Create a SubscriptionEvent using blpapi.test.createEvent(). 3. Obtain the element schema definition from the service. 4. Append a message of type `MarketDataEvents' using blpapi.test.appendMessage(). 5. Format the message using the formatter returned by appendMessage(). In this example the message's body is represented by { LAST_PRICE: 142.80 }. 6. Verify that the expected and actual values are the same. """ event = blpapi.test.createEvent(blpapi.Event.SUBSCRIPTION_DATA) service = blpapi.test.deserializeService(MKTDATA_SCHEMA) mktdata_events = blpapi.Name("MarketDataEvents") schema_def = service.getEventDefinition(mktdata_events) formatter = blpapi.test.appendMessage(event, schema_def) expected_last_price = 142.80 message_content = { "LAST_PRICE": expected_last_price } formatter.formatMessageDict(message_content) expected_compute_result = 200.00 self.mock_compute_engine.someVeryComplexComputation.return_value = \ expected_compute_result self.event_processor.processEvent(event, self.mock_session) self.mock_compute_engine.someVeryComplexComputation \ .assert_called_once_with(expected_last_price) self.mock_notifier.send_to_terminal \ .assert_called_once_with(expected_compute_result)
def init_game(): pygame.init() # Set up the clock for a decent framerate clock = pygame.time.Clock() game_sound = GameSound(pygame) # set up the drawing window screen = pygame.display.set_mode( (Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT)) player = Player(game_sound) # Create groups to hold enemy sprites and all sprites # - enemies is used for collision detection and position updates # - clouds is used for position updates # - all_sprites is used for rendering enemies = pygame.sprite.Group() clouds = pygame.sprite.Group() shoots = pygame.sprite.Group() all_sprites = pygame.sprite.Group() all_sprites.add(player) # game loop running = True event_processor = EventProcessor(enemies, clouds, all_sprites, player, shoots) while running: running = event_processor.process_events() pressed_keys = pygame.key.get_pressed() player.update(pressed_keys) enemies.update() clouds.update() shoots.update() # fill the background with white screen.fill(Constants.SKY_BLUE) # drawn all sprites for entity in all_sprites: if not isinstance(entity, Player): screen.blit(entity.surf, entity.rect) for entity in all_sprites: if isinstance(entity, Player): screen.blit(entity.surf, entity.rect) if len(shoots.spritedict) > 0: for shoot in shoots: enemy_hit = pygame.sprite.spritecollideany(shoot, enemies) if enemy_hit is not None: print("Enemy hit...") enemy_hit.kill() shoot.kill() if pygame.sprite.spritecollideany(player, enemies): # If so, then remove the player and stop the loop player.kill() # Stop any moving sounds and play the collision sound game_sound.move_up_sound.stop() game_sound.move_down_sound.stop() channel = game_sound.collision_sound.play() # while channel.get_busy(): # continue # running = False # flip the display pygame.display.flip() # Ensure program maintains a rate of 30 frames per second clock.tick(45) pygame.mixer.music.stop() pygame.mixer.quit()
def main(): EventProcessor(VkAPI(token=TOKEN), WeatherAPI(api_key=OPENWEATHER_API_KEY)).process_events()
def admin_logic_test(): conf = config.Config() conf.load('./unit/test.xml') umgr = url_mgr.UrlMgr() umgr.init('./url.xml', None) umgr.load() print 'mongo ip=%s,port=%d' % (conf.mongo_ip, conf.mongo_port) mongo = WxMongo() mongo.start() LOG().setlogger('./log') log = LOG().getlogger() admin_logic = AdminLogic(mongo, log) eventp = EventProcessor(mongo, log) admin_coll = mongo.admin user_coll = mongo.user stat_coll = mongo.stat menu_coll = mongo.menu for aitem in admin_coll.coll.find(): print aitem #admin_coll.coll.drop() fid = 'test_zhengyang' tid = 'zyqlzr' print admin_logic.process_text(fid, admin.PRI_ROOT, 'sub') msg = '' print eventp.process_subscribe(msg, 'ifanyoyo', 'test_1') print eventp.process_subscribe(msg, 'ifanyoyo', 'test_2') print eventp.process_subscribe(msg, 'ifanyoyo', 'test_2') print eventp.process_subscribe(msg, 'ifanyoyo', 'test_3') print eventp.process_subscribe(msg, 'ifanyoyo', 'test_4') print eventp.process_unsubscribe(msg, 'ifanyoyo', 'test_3') print eventp.process_unsubscribe(msg, 'ifanyoyo', 'test_4') print eventp.process_unsubscribe(msg, 'ifanyoyo', 'test_5') today = mytime.today2str() menu_coll.add_menu(name='menu_1', day=today, price=10, title=FOOD_1, url='http://127.0.0.1/menu1', info='balabala1', clock=0) menu_coll.add_menu(name='menu_2', day=today, price=10, title=FOOD_2, url='http://127.0.0.1/menu2', info='balabala2', clock=0) menu_coll.add_menu(name='menu_3', day=today, price=10, title=FOOD_3, url='http://127.0.0.1/menu3', info='balabala3', clock=0) admin_coll.add_admin(user='******', pri=10, name='jianglili') #admin_coll.add_admin(user='******', pri=8, name='jiangzifa') #admin_coll.add_admin(user='******', pri=5, name='zhangsan') #admin_coll.add_admin(user='******', pri=5, name='lisi') #admin_coll.add_admin(user='******', pri=0, name='wangwu') #admin_coll.add_admin(user='******', pri=0, name='zhaoliu') print admin_logic.process_text(fid, admin.PRI_ROOT, 'add admin x351575427 jiangzifa') print admin_logic.process_text(fid, admin.PRI_ROOT, 'add employee employee_1 zhangsan') print admin_logic.process_text(fid, admin.PRI_ROOT, 'add employee employee_2 lisi') print admin_logic.process_text(fid, admin.PRI_ROOT, 'add employee employee_3 zhengqi') print admin_logic.process_text(fid, admin.PRI_ROOT, 'add employee employee_4 zhengqi') print admin_logic.process_text(fid, admin.PRI_ROOT, 'add client client_1 wangwu') print admin_logic.process_text(fid, admin.PRI_ROOT, 'add client client_2 zhaoliu') print admin_logic.process_text(fid, admin.PRI_ROOT, 'add client client_3 zhaoliu') for uitem in user_coll.coll.find(): print uitem for sitem in stat_coll.coll.find(): print sitem for aitem in admin_coll.coll.find(): print aitem print admin_logic.process_text(fid, admin.PRI_ROOT, 'sub') print admin_logic.process_text(fid, admin.PRI_ROOT, 'sub +') print admin_logic.process_text(fid, admin.PRI_ROOT, 'sub -') print admin_logic.process_text(fid, admin.PRI_ROOT, 'unsub') print admin_logic.process_text(fid, admin.PRI_ROOT, 'unsub +') print admin_logic.process_text(fid, admin.PRI_ROOT, 'unsub -') print admin_logic.process_text(fid, admin.PRI_ROOT, 'unsub') print admin_logic.process_text(fid, admin.PRI_ROOT, 'menu') print admin_logic.process_text(fid, admin.PRI_ROOT, 'menu +') print admin_logic.process_text(fid, admin.PRI_ROOT, 'menu -') print admin_logic.process_text(fid, admin.PRI_ROOT, 'list all') print admin_logic.process_text(fid, admin.PRI_ROOT, 'list admin') print admin_logic.process_text(fid, admin.PRI_ROOT, 'list employee') print admin_logic.process_text(fid, admin.PRI_ROOT, 'list client') print admin_logic.process_text(fid, admin.PRI_ROOT, 'del wx employee_2') print admin_logic.process_text(fid, admin.PRI_ROOT, 'del name zhaoliu') print admin_logic.process_text(fid, admin.PRI_ROOT, 'del employee_4 zhengqi') print admin_logic.process_text(fid, admin.PRI_ROOT, 'list employee') print admin_logic.process_text(fid, admin.PRI_ROOT, 'list client') admin_coll.coll.drop() user_coll.coll.drop() stat_coll.coll.drop() menu_coll.coll.drop()
def empty_commandhandler(): sys.stdout.flush() def help_handler(): print('All the available command is list as follow:') for key in commanddict: print(key) sys.stdout.flush() if __name__ == '__main__': vm = VehicleManager() vm.initialize() ep = EventProcessor(vm) ep.start() CommunicationTerminal.setEventProcessor(ep) CommunicationTerminal.startServer() def showInfo_commandhandler(): vm.show() ep.show() # interactive command line commanddict = { '': empty_commandhandler, 'exit': exit_handler, 'showinfo': showInfo_commandhandler }
def main(wf): from query_exchange import query_exchange_server from query_google import query_google_calendar import pytz from pytz import timezone from datetime import timedelta, datetime from settings import get_value_from_settings_with_default_boolean, get_value_from_settings_with_default_int import time # Check to see if updates are available if wf.update_available: wf.add_item('A newer version is available', '↩ to install update', icon='update-available.png', arg='update', valid=True) # Parse and log the query variable query = None if len(wf.args): query = wf.args[0] log.debug('query : {!r}'.format(query)) # Get date offset args = wf.args date_offset = 0 if len(args) > 0: date_offset = int(args[0]) #Start calculating elapsed time - displayed in results page at end action_start_time = time.time() # Find out cache time cache_time = get_value_from_settings_with_default_int( wf, 'cache_time', 9000) morning = timezone("US/Eastern").localize( datetime.today().replace(hour=0, minute=0, second=1) + timedelta(days=date_offset)) night = timezone("US/Eastern").localize( datetime.today().replace(hour=23, minute=59, second=59) + timedelta(days=date_offset)) # Outlook needs a different time format than google it would appear start_outlook = morning.astimezone(pytz.utc) end_outlook = night.astimezone(pytz.utc) start_google = morning.astimezone(pytz.utc).isoformat() stop_google = night.astimezone(pytz.utc).isoformat() log.info("%s\t\t\t%s", start_google, stop_google) def google_wrapper(): """A wrapper around doing a google query so this can be used with a cache function""" return query_google_calendar(wf, start_google, stop_google, date_offset) def exchange_wrapper(): """Wrapper around outlook query so can be used with caching""" return query_exchange_server(wf, start_outlook, end_outlook, date_offset) # Format date text for displays date_text = night.strftime("%A %B %d, %Y") date_text_numeric = night.strftime("%m/%d/%y") # Build Cache Keys exchange_cache_key = get_cache_key('exchange', date_offset) google_cache_key = get_cache_key('google', date_offset) log.debug("-- FG: CacheKey (Google) " + google_cache_key) log.debug("-- FG: CacheKey (Exchange) " + exchange_cache_key) # Check which calendars to use from settings use_exchange = get_value_from_settings_with_default_boolean( wf, 'use_exchange', False) use_google = get_value_from_settings_with_default_boolean( wf, 'use_google', False) if not use_google and not use_exchange: wf.add_item('Calendars are disabled', 'use the tc command to setup a calendar', icon=ICON_INFO, arg="tc") wf.send_feedback() return log.debug("Max Age: %i Cache Age Google: %i Exchange: %i", cache_time, wf.cached_data_age(google_cache_key), wf.cached_data_age(exchange_cache_key)) # Check cache status google_fresh = wf.cached_data_fresh(google_cache_key, max_age=cache_time) exchange_fresh = wf.cached_data_fresh(exchange_cache_key, max_age=cache_time) # Determine whether cache data is being shown or "live" data showing_cached_data = True if use_google: showing_cached_data &= google_fresh if use_exchange: showing_cached_data &= exchange_fresh event_count = 0 error_state = False log.debug('--FG: Use Exchange:' + str(use_exchange)) log.debug('--FG: Exchange Fresh:' + str(exchange_fresh)) if use_exchange: # If the cache is fresh we need to do a bg refresh - because who knows what has happened # If the cache is stale then directly query the exchange server if exchange_fresh: log.debug('--FG: Loading Exchange events from Cache') #Extract the cached events exchange_events = wf.cached_data(exchange_cache_key, max_age=0) log.debug(str(exchange_events)) # Run update in the background if not is_running('update_exchange'): cmd = [ '/usr/bin/python', wf.workflowfile('query_exchange.py'), start_outlook.strftime("%Y-%m-%d-%H:%M:%S"), end_outlook.strftime("%Y-%m-%d-%H:%M:%S"), str(date_offset) ] log.debug('--FG: Launching background exchange update') # Fire off in the background the script to update things! :) run_in_background('update_exchange', cmd) else: log.debug('--FG: Background exchange update already running') else: log.debug('--FG: Directly querying Exchange') # Directly query the exchange server exchange_events = wf.cached_data(exchange_cache_key, exchange_wrapper, max_age=cache_time) if exchange_events is None: log.debug('--FG: Exchange Events returned NONE!!!') error_state = True wf.add_item('Unable to connect to exchange server', 'Check your connectivity or NTLM auth settings', icon='img/disclaimer.png') exchange_events = [] else: event_count += len(exchange_events) else: exchange_events = [] if use_google: # check for any enabled calendars no_google_calendars = True for key in wf.settings: if 'calendar' in key: no_google_calendars = False if no_google_calendars: wf.add_item('Not showing any Google Calendars', 'use the tcgc command to select calendars') # If the cache is "fresh" we need to do a bg refresh - because we are loading from the cache # If the cache isnt fresh - the server will be queried directly anyways if google_fresh: # Extract cached events google_events = wf.cached_data(google_cache_key, max_age=0) # Run update in background if not is_running('update_google'): cmd = [ '/usr/bin/python', wf.workflowfile('query_google.py'), start_google, stop_google, str(date_offset) ] # Fire off in the background the script to update things! :) run_in_background('update_google', cmd) else: # Directly run event update - ignore background stuff google_events = wf.cached_data(google_cache_key, google_wrapper, max_age=cache_time) if google_events is None: error_state = True import httplib conn = httplib.HTTPConnection("www.google.com") try: conn.request("HEAD", "/") wf.add_item( 'Unable to connect to Google', 'Authorization or Connection error - use tc to reauthorize', icon='img/disclaimer.png') except Exception as ex: wf.logger.info("Unable to connect to google") template = "An exception of type {0} occured. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) wf.logger.info(message) import traceback wf.logger.info(traceback.format_exc()) wf.add_item('Unable to connect to Google', 'Check your internet connection or proxy settings', icon='img/disclaimer.png') google_events = [] else: for e in google_events: wf.logger.debug(' '.join([ '**FG --- Google:', str(e.get(u'start').get(u'dateTime', 'All Day')), e.get('summary', 'NoTitle') ])) event_count += len(google_events) else: google_events = [] # Build Header icon_file = 'img/date_span.png' if use_exchange and use_google: icon_file = 'img/iconBoth.png' elif use_exchange: icon_file = 'img/iconOutlook.png' elif use_google: icon_file = 'img/iconGoogle.png' # Fire off some log data log.info("Event Count Google: " + str(len(google_events))) log.info("Event Count Exchange: " + str(len(exchange_events))) log.info("Event Count Total: " + str(event_count)) if event_count == 0: if error_state is False: wf.add_item('Calendar is empty', date_text, icon=icon_file) wf.send_feedback() return first_menu_entry = wf.add_item(date_text, date_text_numeric, icon=icon_file) # Process events EventProcessor(wf).process_events(exchange_events, google_events) # Update elapsed time counter action_elapsed_time = time.time() - action_start_time if showing_cached_data: first_menu_entry.subtitle += " - Cached Data" else: first_menu_entry.subtitle += " query time: " + "{:.1f}".format( action_elapsed_time) + " seconds" wf.send_feedback()
class App(object): def __init__(self, name, config={}): self.name = name self.config = Configure(config) self.__init_local_and_global() self.__init_logger() self.__init_broker_and_processors() self.__init_runtime_tree_storage() def __init_local_and_global(self): local_defaults = { 'event_handler_map': dict, } self.__local = Local(**local_defaults) self.__global = self self.__global.event_handler_map = {} def __init_logger(self): self.logger = logging.getLogger(self.name) self.logger.propagate = 0 formatter = logging.Formatter( '[%(asctime)s: %(levelname)s] %(message)s') if self.config.debug: ch = logging.StreamHandler(sys.stdout) ch.setFormatter(formatter) ch.setLevel(logging.DEBUG) self.logger.addHandler(ch) self.logger.setLevel(logging.DEBUG) else: fh = logging.FileHandler(self.config.log_path) fh.setFormatter(formatter) fh.setLevel(logging.INFO) self.logger.addHandler(fh) self.logger.setLevel(logging.INFO) def __init_broker_and_processors(self): self.__broker = Broker(self) self.__event_processor = EventProcessor(0, self) self.__event_processors = list() for i in range(self.config.processor_num): id = i + 1 event_channel = EventChannel() event_channel.register(self.__broker) event_processor = EventProcessor(id, self, event_channel) self.__event_processors.append(event_processor) def __init_runtime_tree_storage(self): self.runtime_tree_storage = RuntimeTreeStorage(self.config.runtime_db) def __load_include_modules(self): pass def start(self): self.__broker.start() for event_processor in self.__event_processors: event_processor.start() def stop(self): self.__broker.stop() for event_processor in self.__event_processors: event_processor.stop() def find_handlers(self, event_name): handlers = [] if event_name in self.__global.event_handler_map: handlers.extend(self.__global.event_handler_map[event_name]) if event_name in self.__local.event_handler_map: handlers.extend(self.__local.event_handler_map[event_name]) return handlers def on(self, event_name, handler, local=False): if local: if event_name not in self.__local.event_handler_map: self.__local.event_handler_map[event_name] = [] self.__local.event_handler_map[event_name].append(handler) else: if event_name not in self.__global.event_handler_map: self.__global.event_handler_map[event_name] = [] self.__global.event_handler_map[event_name].append(handler) def fire(self, event, background=True): if background: self.__broker.put(event) else: return self.__event_processor.process_event(event) def handler(self, event_name, throws_events=list(), local=False): def decorator(func): self.on(event_name, Handler(func, throws_events), local) def wrapper(*args, **kw): return func(*args, **kw) return wrapper return decorator
import blpapi import appconfig from compute_engine import ComputeEngine from notifier import Notifier from event_processor import EventProcessor from token_generator import TokenGenerator from authorizer import Authorizer from subscriber import Subscriber from application import Application if __name__ == "__main__": options = appconfig.parseCmdLine() compute_engine = ComputeEngine() notifier = Notifier() event_processor = EventProcessor(notifier, compute_engine) so = blpapi.SessionOptions() for i, host in enumerate(options.hosts): so.setServerAddress(host, options.port, i) so.setAuthenticationOptions(options.auth['option']) session = blpapi.Session(so, event_processor.processEvent) token_generator = TokenGenerator(session) authorizer = Authorizer(session, token_generator) subscriber = Subscriber(session) application = Application(session, authorizer, subscriber, options)
def run(self): EventProcessor().run()
def setUp(self): self.mock_session = Mock() self.mock_notifier = Mock() self.mock_compute_engine = Mock() self.event_processor = EventProcessor(self.mock_notifier, self.mock_compute_engine)