def main(): loop = ZMQEventLoop() asyncio.set_event_loop(loop) try: opts = parse_args(sys.argv[1:]) init_console_logging(verbose_level=opts.verbose) validator_url = opts.connect if "tcp://" not in validator_url: validator_url = "tcp://" + validator_url messenger = Messenger(validator_url) database = Database(opts.db_host, opts.db_port, opts.db_name, opts.db_user, opts.db_password, loop) try: host, port = opts.bind.split(":") port = int(port) except ValueError: print("Unable to parse binding {}: Must be in the format" " host:port".format(opts.bind)) sys.exit(1) start_rest_api(host, port, messenger, database) except Exception as err: # pylint: disable=broad-except LOGGER.exception(err) sys.exit(1) finally: database.disconnect() messenger.close_validator_connection()
def get_storage(config): storage = [] if 'storage' in config: for storage_config in config['storage']: messenger = Messenger(config['messaging']) messenger.set_incoming(storage_config['alias']) if storage_config['type'] == 'MongoDB': storage.append(MongoDB(storage_config, messenger)) if storage_config['type'] == 'FlatFileStorage': storage.append(FileStorage(storage_config, messenger)) if storage_config['type'] == 'IO': storage.append(IO(messenger)) return storage
def get_data_sources(config): data_sources = [] if 'data_sources' in config: for source_config in config['data_sources']: messenger = Messenger(config['messaging']) # Only need to set outputs for messenger, since data sources don't receive external messages messenger.set_outgoing(source_config['outputs']) if source_config['type'] == 'TwitterStreamingAPI': data_sources.append(Twitter(source_config, messenger)) if source_config['type'] == 'FlatFileDataSource': # Include the base path (the location of the config file), in case the flat file's path is relative to the config file #source_config['base_path'] = config['base_path'] data_sources.append(FlatFile(source_config, messenger)) if source_config['type'] == 'StreamingImagesAPI': data_sources.append( StreamingImagesAPI(source_config, messenger)) return data_sources
async def main(): load_dotenv() service = services.Chromedriver(log_file=os.devnull) browser = browsers.Chrome() telegram_client = TelegramClient(token=os.getenv("TG_TOKEN"), default_channel=os.getenv("TG_CHANNEL")) loguru_client = LoguruClient() messenger = Messenger([loguru_client, telegram_client]) async with get_session(service, browser) as session: extractor = CUExtractor(session) memory = Memory() while True: items = await extractor.extract() added: List[Entry] = memory.update(items)[0] for entry in added: messenger.send(entry.to_markdown()) await asyncio.sleep(DELAY)
def main(): loop = ZMQEventLoop() asyncio.set_event_loop(loop) try: opts = parse_args(sys.argv[1:]) init_console_logging(verbose_level=opts.verbose) validator_url = opts.connect if 'tcp://' not in validator_url: validator_url = 'tcp://' + validator_url messenger = Messenger(validator_url) database = Database( opts.db_host, opts.db_port, opts.db_name, opts.db_user, opts.db_password, loop) try: host, port = opts.bind.split(':') port = int(port) except ValueError: print(f'Unable to parse binding {opts.bind}: Must be in the format host:port') sys.exit(1) init_api(messenger, database) except Exception as err: # pylint: disable=broad-except # LOGGER.exception(err) fprint('err exe: ') fprint(err) sys.exit(1) finally: # database.disconnect() messenger.close_validator_connection()
def get_filters(config): filters = [] if 'filters' in config: for filter_config in config['filters']: messenger = Messenger(config['messaging']) messenger.set_incoming(filter_config['alias']) messenger.set_outgoing(filter_config['outputs']) filters.append(Filter(filter_config, messenger)) return filters
def __init__(self, config: SessionConfig): self.config = config self.client = AsyncClient(config.homeserver, config.matrix_id) try: with open(config.next_batch_file, "r") as next_batch_token: self.client.next_batch = next_batch_token.read() except FileNotFoundError: # No existing next_batch file; no worries. self.client.next_batch = 0 # Update next_batch every sync self.client.add_response_callback(self.__sync_cb, SyncResponse) # Handle text messages self.client.add_event_callback(self.__message_cb, RoomMessageText) # Handle invites self.client.add_event_callback(self.__autojoin_room_cb, InviteEvent) self.client.add_ephemeral_callback(self.sample, ReceiptEvent) self.load_plugins() self.messenger = Messenger(self.client)
def get_models(config): models = [] if 'models' in config: for model_config in config['models']: messenger = Messenger(config['messaging']) messenger.set_incoming(model_config['alias']) messenger.set_outgoing(model_config['outputs']) if 'preprocessor_filename' in model_config and model_config[ 'preprocessor_filename'] != '': preprocessor_path = os.path.join( os.getcwd(), model_config['preprocessor_filename']) print('Loading preprocessor from path "%s"' % preprocessor_path) preprocessor = import_from_file( model_config['preprocessor_classname'], preprocessor_path) constructor = getattr(preprocessor, model_config['preprocessor_classname']) instance = constructor() preprocessor = AIPreprocessor(model_config, instance) # No preprocessor provided? That's ok, we'll just use the default one and pass data through without preprocessing. else: preprocessor = AIPreprocessor(None, None) # TODO: Make it more obvious that models can be either on-premise or exist as APIs if 'module_file_path' in model_config: module = import_from_file( model_config['module_classname'], os.path.join(os.getcwd(), model_config['module_file_path'])) constructor = getattr(module, model_config['module_classname']) # Used for ONNX models if 'model_path' in model_config and model_config[ 'model_path'].strip() != '': instance = constructor(model_config) else: instance = constructor() models.append( Model(model_config, instance, messenger, preprocessor)) else: instance = None models.append( APIModel(model_config, instance, messenger, preprocessor)) return models
def get_custom_entities(pipeline): custom_entities = [] if 'custom_entities' in pipeline: for config in pipeline['custom_entities']: messenger = Messenger(pipeline['messaging']) messenger.set_incoming(config['alias']) messenger.set_outgoing(config['outputs']) module = import_from_file( config['classname'], os.path.join(os.getcwd(), config['filename'])) constructor = getattr(module, config['classname']) instance = constructor() custom_entities.append(CustomEntity(config, instance, messenger)) return custom_entities
def __init__(self, name='PanoGame'): # setup logging logging.config.fileConfig('game.cfg') self.log = logging.getLogger('pano') self.name = name self.config = ConfigVars() self.windowProperties = {} self.resources = ResourceLoader() self.inventory = Inventory(self) self.gameView = GameView(gameRef=self, title=name) self.inputMappings = InputActionMappings(self) self.i18n = i18n(self) self.music = MusicPlayer(self) self.soundsFx = SoundsPlayer(self) self.msn = Messenger(self) self.initialNode = None self.mouseMode = PanoConstants.MOUSE_UI_MODE self.paused = False self.isGameSequenceActive = False self.gameActions = GameActions(self) self.fsm = None self.gameTask = None self.console = None self.consoleVisible = False self.saveLoad = None self.saveRequest = None self.loadRequest = None self.persistence = None self.quitRequested = False # read the boot configuration self._readBootConfig() os.chdir(self.config.get(PanoConstants.CVAR_GAME_DIR)) try: with open('Config.prc') as cfgFile: loadPrcFileData('game-config', cfgFile.read()) except IOError: self.log.exception( 'Could not find Config.prc in the game directory')
class PanoGame(object): """ Runs the game and manages the game tasks and rendering. """ def __init__(self, name='PanoGame'): # setup logging logging.config.fileConfig('game.cfg') self.log = logging.getLogger('pano') self.name = name self.config = ConfigVars() self.windowProperties = {} self.resources = ResourceLoader() self.inventory = Inventory(self) self.gameView = GameView(gameRef=self, title=name) self.inputMappings = InputActionMappings(self) self.i18n = i18n(self) self.music = MusicPlayer(self) self.soundsFx = SoundsPlayer(self) self.msn = Messenger(self) self.initialNode = None self.mouseMode = PanoConstants.MOUSE_UI_MODE self.paused = False self.isGameSequenceActive = False self.gameActions = GameActions(self) self.fsm = None self.gameTask = None self.console = None self.consoleVisible = False self.saveLoad = None self.saveRequest = None self.loadRequest = None self.persistence = None self.quitRequested = False # read the boot configuration self._readBootConfig() os.chdir(self.config.get(PanoConstants.CVAR_GAME_DIR)) try: with open('Config.prc') as cfgFile: loadPrcFileData('game-config', cfgFile.read()) except IOError: self.log.exception( 'Could not find Config.prc in the game directory') def initialise(self, task): # setup the game's FSM self.fsm = FSM('panoFSM', self) statesFactory = self.fsm.getFactory() statesFactory.registerState(PanoConstants.STATE_INIT, InitGameState) statesFactory.registerState(PanoConstants.STATE_EXPLORE, ExploreState) statesFactory.registerState(PanoConstants.STATE_PAUSED, PausedState) statesFactory.registerState(PanoConstants.STATE_CONSOLE, ConsoleState) statesFactory.registerState(PanoConstants.STATE_INTRO, IntroState) statesFactory.registerState(PanoConstants.STATE_INVENTORY, InventoryState) statesFactory.registerState(PanoConstants.STATE_CREDITS, CreditsState) self.fsm.changeState(PanoConstants.STATE_INIT) self.persistence = PersistenceManager() self.saveLoad = GameSaveLoad(game=self, savesDir=self.config.get( PanoConstants.CVAR_SAVES_DIR)) # create and start the main game loop task globalClock.setMaxDt(0.1) self.gameTask = taskMgr.add(self.gameLoop, PanoConstants.TASK_GAME_LOOP) return Task.done def getConfigVar(self, var, default): if self.config.has_key(var): return self.config[var] else: return default def setConfigVar(self, var, value): self.config[var] = value def gameLoop(self, task): """ Manages the game loop. It is invoked every time a frame is drawn through a Panda Task. """ if self.quitRequested: return sys.exit() millis = globalClock.getDt() * 1000.0 self._doSaveLoad() # process input events events = self.inputMappings.getEvents() if len(events) > 0: for ev, act in events: try: processed = self.fsm.onInputAction(act) if not (processed): if self.gameActions.isAction(act): self.gameActions.execute(act) else: self.log.warning( "Ignored unknown input action %s" % act) except: self.log.exception( "Unexpected error while processing input action %s" % act) if self.paused: millis = 0 # update state self.fsm.update(millis) # update view self.gameView.update(millis) # update sounds self.soundsFx.update(millis) return Task.cont def initGameSequence(self): ''' Called to prepare the state for a game sequence. For example it could disable user input or hide the mouse pointer. ''' self.gameView.getMousePointer().hide() self.inputMappings.disable() self.isGameSequenceActive = True def endGameSequence(self): ''' Called to restore the state after the completion of a game sequence. ''' self.gameView.getMousePointer().show() self.inputMappings.enable() self.isGameSequenceActive = False def getName(self): return self.name def setInitialNode(self, nodeName): self.initialNode = nodeName def getConfig(self): return self.config def getResources(self): return self.resources def getState(self): return self.fsm def getInput(self): return self.inputMappings def getView(self): return self.gameView def getI18n(self): return self.i18n def getMusic(self): return self.music def getSoundsFx(self): return self.soundsFx def getInventory(self): return self.inventory def getPersistence(self): return self.persistence def actions(self): return self.gameActions def canPause(self): """ Use this method to probe whether the game can be paused. It returns a boolean that indicates whether the game can be paused at this moment or not. @todo: Publish the pause-request event and gather any vetos, for now accept it always. Will probably use the messenger object for this. """ # the current state will decide... return self.fsm.allowPausing() and not self.isGameSequenceActive def pause(self): """ Use this method to signal the pausing of the game. @todo: Publish the pause-request event and gather any vetos, for now accept it always. """ if self.canPause(): # self.paused = self.fsm.changeGlobalState(PausedState.NAME) self.paused = self.fsm.changeGlobalState('pausedState') self.paused = True def resume(self): """ Use this method to signal the un-pausing of the game. @todo: Publish the pause-request event and gather any vetos, for now accept it always. """ self.fsm.changeGlobalState(self.fsm.getPreviousGlobalState()) self.paused = False def isPaused(self): return self.paused def enableDebugConsole(self): if self.console is None: self.console = pandaConsole(self, INPUT_GUI | OUTPUT_PYTHON, locals()) self.console.toggle() def showDebugConsole(self): if self.console is not None: self.console.toggle() self.consoleVisible = True self.fsm.pushState('consoleState') def hideDebugConsole(self): if self.console is not None: self.console.toggle() self.consoleVisible = False self.fsm.popState() def isDebugConsoleVisible(self): return self.consoleVisible def quit(self): self.quitRequested = True def requestSave(self, request): self.saveRequest = request def requestLoad(self, request): self.loadRequest = request def getInitialNode(self): return self.initialNode def _readBootConfig(self): ''' Reads the boot configuration from the user's directory in ~/.pano/<game_name>/.config ''' self.config.add(PanoConstants.CVAR_GAME_DIR, '.') self.config.add(PanoConstants.CVAR_SAVES_DIR, 'saves') # userDir = os.path.expanduser('~') # bootCfgPath = os.path.join(os.path.join(userDir, self.name), '.config') # if os.path.exists(bootCfgPath): # cfg = SafeConfigParser() # istream = None # try: # try: # istream = codecs.open(bootCfgPath, 'r', "utf-8") # cfg.readfp(istream) # if cfg.has_option('config', 'game_dir'): # self.gameDir = cfg.get('config', 'game_dir') # else: # self.log.critical('Missing game directory configuration option') # raise GameError('Missing boot config') # if cfg.has_option('config', 'saves_dir'): # self.savesDir = cfg.get('config', 'saves_dir') # else: # self.log.critical('Missing saves directory configuration option') # raise GameError('Missing boot config') # except (MissingSectionHeaderError, ParsingError): # self.log.exception('Unexpected error while reading boot config') # raise GameError('Corrupted boot configuration') # except IOError, e: # self.log.exception('Unexpected I/O error while reading boot config') # raise GameError('Failed to read boot configuration') # finally: # if istream is not None: # istream.close() # else: # self.log.critical('Missing boot configuration, cannot properly initialize') # raise GameError('Failed boot init') def _doSaveLoad(self): ''' Checks for a pending save or load request and performs the respective operation. If the operation succeeds the PanoConstants.EVENT_LOAD_COMPLETED or PanoConstants.EVENT_SAVE_COMPLETED event will be send. If the operation failed the PanoConstants.EVENT_SAVELOAD_ERROR event will be send. ''' if self.saveRequest is not None: try: self.saveLoad.save(self.persistence, self.saveRequest) except SaveGameError: self.log.exception( 'Save action failed due to unexpected error.') self.msn.sendMessage(PanoConstants.EVENT_SAVELOAD_ERROR, [self.saveRequest]) else: self.msn.sendMessage(PanoConstants.EVENT_SAVE_COMPLETED, [self.saveRequest]) finally: self.saveRequest = None elif self.loadRequest is not None: try: self.saveLoad.load(self.persistence, self.loadRequest) except LoadGameError: self.log.exception( 'Load action failed due to unexpected error.') self.msn.sendMessage(PanoConstants.EVENT_SAVELOAD_ERROR, [self.loadRequest]) else: self.msn.sendMessage(PanoConstants.EVENT_LOAD_COMPLETED, [self.loadRequest]) finally: self.loadRequest = None