def remove_ticker(self, ticker: str) -> None: logger.info("Removing ticker ...") if get_company_profile(ticker, self.config): try: remove_ticker(ticker, self.config) except Exception: raise OperationalException( "Something went wrong while deleting the ticker from the registry" ) else: raise OperationalException( "Provided ticker {} does not exist".format(ticker))
def main(sysargv: List[str] = None) -> None: """ This function will initiate all the services. """ return_code: Any = 1 try: arguments = Arguments(sysargv) args = arguments.parsed_args # Call subcommand. if 'func' in args: return_code = args['func'](args) else: # No subcommand was issued. raise OperationalException( "Usage of the bot requires a sub command to be specified.\n" "To see the full list of options available, please use " "`bot --help` or `bot <command> --help`." ) except SystemExit as e: return_code = e except KeyboardInterrupt: logger.info('SIGINT received, aborting ...') return_code = 0 except OperationalException as e: logger.error(str(e)) return_code = 2 except Exception: logger.exception('Fatal exception!') finally: sys.exit(return_code)
def start(self): if self.data_sources is None: raise OperationalException( "Can't run strategies without data sources") super(StrategyExecutor, self).start()
def __init__(self, args: Dict[str, Any], direct=True) -> None: if direct: raise OperationalException( "Direct creation of Configuration is not allowed") self._config: Optional[Dict[str, Any]] = None self._initialize(args)
def create_config(cls, args: Dict[str, Any]): # Check if all dependencies are met if not args['config']: raise OperationalException("Config file is not specified") config_file = args['config'] if not os.path.isfile(config_file): raise OperationalException( "Specified config location is not a file") if not config_file.endswith('.json'): raise OperationalException( "Specified config file is not a JSON file") logger.info("Using configuration file: {}".format(config_file)) return Configuration(args, direct=False)
def _initialize(self): jobs = self.create_jobs() if not jobs or len(jobs) == 0: raise OperationalException( "There where no jobs initialized for the WorkerExecutor instance" ) for job in jobs: self._pending_jobs.put(job)
def load_strategy(self, strategy_class_name: str) -> Strategy: logger.info("Loading remote strategies ...") if not strategy_class_name: raise OperationalException("Provided strategies has it search class name not set") strategies_dir = Path(PLUGIN_STRATEGIES_DIR) modules = self.locate_python_modules(strategies_dir) location = self.locate_class(modules, strategy_class_name) generator = self.create_class_generators(location, strategy_class_name, Strategy) strategy: Strategy = next(generator, None) if strategy and issubclass(strategy, Strategy): return strategy raise OperationalException( f"Impossible to load Strategy '{strategy_class_name}'. This strategy does not exist " "or contains Python code errors." )
def load_data_provider(self, data_provider_class_name: str) -> DataProvider: logger.info("Loading remote data providers ...") if not data_provider_class_name: raise OperationalException( "Provided data provider has it search class name not set") data_providers_dir = Path(PLUGIN_DATA_PROVIDERS_DIR) modules = self.locate_python_modules(data_providers_dir) location = self.locate_class(modules, data_provider_class_name) generator = self.create_class_generators(location, data_provider_class_name, DataProvider) data_provider: DataProvider = next(generator, None) if data_provider and issubclass(data_provider, DataProvider): return data_provider raise OperationalException( f"Impossible to load Strategy '{data_provider_class_name}'. This strategy does not exist " "or contains Python code errors.")
def load_config_file(path: str) -> Dict[str, Any]: """ Loads a config file from the given path :param path: path as str :return: configuration as dictionary """ try: # Read config from stdin if requested in the options with open(path) if path != '-' else sys.stdin as file: config = rapidjson.load(file, parse_mode=CONFIG_PARSE_MODE) except FileNotFoundError: raise OperationalException( f'Config file "{path}" not found!' ' Please create a config file or check whether it exists.') return config
def locate_python_modules(dir_path: Path) -> List[Path]: """ Functions that will search through all the files in a directory to locate the class matching the given class name. """ if not dir_path.is_dir(): raise OperationalException("Given directory path is not a directory") modules: List[Path] = [] logger.info("Searching in directory {} ...".format(dir_path)) for entry in dir_path.iterdir(): if not str(entry).endswith('.py'): continue logger.info("Found module: {}, appending it to search paths".format(str(entry))) modules.append(entry) return modules
def add_ticker(self, ticker: str) -> None: logger.info("Adding ticker ...") if not get_company_profile(ticker, self.config): if self.__data_provider_manager.evaluate_ticker(ticker): profile = self.__data_provider_manager.get_profile(ticker) if not profile: raise OperationalException( "Could not evaluate {} with the data providers".format( ticker)) company_name = profile.get('profile', {}).get('companyName', None) category = profile.get('profile', {}).get('industry', None) if not company_name: raise OperationalException( "Could not evaluate company name for ticker {} with the data providers" ) if not company_name: raise OperationalException( "Could not evaluate category for ticker {} with the data providers" ) try: add_ticker(ticker, company_name=company_name, category=category, config=self.config) except Exception: raise OperationalException( "Something went wrong with adding ticker {} to the registry" .format(ticker)) else: raise OperationalException( "Could not evaluate ticker {} with the data providers". format(ticker)) else: raise OperationalException( "Ticker {} is already present in registry".format(ticker))
def strategy_executor(self) -> StrategyExecutor: if not self._strategy_executor: raise OperationalException("Currently there is no strategy executor defined for the bot context") return self._strategy_executor
def data_provider_executor(self) -> DataProviderExecutor: if not self._data_provider_executor: raise OperationalException("Currently there is no data provider executor defined for the bot context") return self._data_provider_executor
def config(self) -> Dict[str, Any]: if not self._config: raise OperationalException("Config is not specified in the context") return self._config
def reconfigure(self) -> None: if self._state: self._state.reconfigure() else: raise OperationalException("Bot context doesn't have a state")
def stop(self) -> None: if self._state: self._state.stop() else: raise OperationalException("Bot context doesn't have a state")