def main(): parser = ArgumentParser(description='Instabot', add_help=False) parser.add_argument('--version', action='version', version=__version__) choices = [ func for func in dir(Instabot) if callable(getattr(Instabot, func)) and not func.startswith('_') ] parser.add_argument('action', metavar='|'.join(choices), choices=choices) defaults = { 'config42': OrderedDict([ ('argv', dict(handler=ArgParse, schema=schema, parents=[parser])), ('env', { 'prefix': 'INSTABOT' }), ('file', { 'path': 'config.yml' }), ]) } config = ConfigManager(schema=schema, defaults=defaults) config_file = config.get('config.file') _config = ConfigManager(schema=schema, path=config_file) config.set_many(_config.as_dict()) config.commit() logging.config.dictConfig(config.get("logging")) instabot = Instabot(config) getattr(instabot, config.get('action'))()
def test_validation_with_nested_configmanager(): schema = [{ "name": "user id ", "key": "user.id", "type": "integer", "required": True }, { "name": "user name ", "key": "user.name", "type": "string", "required": True } ] config_sample = {'user': {'id': 1234}} with pytest.raises(ValidationError): _ = ConfigManager(schema=schema, defaults=config_sample) os.environ.update({"BARBEROUSSE_USER_NAME": "user"}) config_manager = ConfigManager(schema=schema, prefix="BARBEROUSSE", defaults=config_sample) assert config_manager.get('user.name') == 'user' assert config_manager.get('user.id') == 1234 config_sample.update({'config42': {'env': {'prefix': 'BARBEROUSSE'}}}) config_manager = ConfigManager(schema=schema, defaults=config_sample) assert config_manager.get('user.name') == 'user' assert config_manager.get('user.id') == 1234
def load_configmanager(configuration): if configuration == 'etcd': from config42.handlers.etcd import Etcd return ConfigManager(handler=Etcd, **config.get(configuration)) elif configuration == 'raw': from config42.handlers.raw import RawHandler return ConfigManager(handler=RawHandler, **config.get(configuration)) else: return ConfigManager(path=configuration)
def test_configuration_replace(default_config, sample_config): config_manager = ConfigManager() config_manager.set_many(sample_config) assert sample_config['simple'] == config_manager.get('simple') config_manager.replace(default_config) assert config_manager.get('simple') is None assert default_config['defaultkey1'] == config_manager.get('defaultkey1')
def test_change_nesting_key(cwd): defaults = { 'config43': { 'file': { 'path': cwd + "/files/nested-config.yml" } } } config_manager = ConfigManager(defaults=defaults, nested_configuration_key="config43") assert config_manager.get('config43') is not None assert config_manager.get('nested') == "value"
def test_literals_etcd(script_runner): keyspace = '/' + str(uuid4()) ret = script_runner.run( 'config42', *args('-l key=value nested.key=value -c etcd --etcd-keyspace ' + keyspace)) assert ret.success config_manager = ConfigManager(keyspace=keyspace) assert config_manager.get('key') == 'value' assert config_manager.get('nested').get('key') == 'value' assert config_manager.get('nested.key') == 'value'
def test_configuration_setting_raise_exception(sample_config): config_manager = ConfigManager() config_manager.set_many(sample_config) with pytest.raises(AttributeError): config_manager.set('nested_list.0.1', 'simple') config_manager.set('absentkey4', "value") with pytest.raises(AttributeError): assert config_manager.get('absentkey4.absentkey3.absentkey3') == "value"
def test_apply_literals(script_runner): keyspace = '/' + str(uuid4()) _ = script_runner.run( 'config42', *args('-l key=value key2=value -c etcd --etcd-keyspace ' + keyspace)) ret = script_runner.run( 'config42', *args('-a apply -l key=value2 -c etcd --etcd-keyspace ' + keyspace)) assert ret.success config_manager = ConfigManager(keyspace=keyspace) assert config_manager.get('key') == 'value2' assert config_manager.get('key2') is None assert len(config_manager.as_dict()) == 1
def test_configuration_update_default_value(default_config): config_manager = ConfigManager(defaults=default_config) assert config_manager.get('absentkey3.absentkey3.absentkey3') is None config_manager.set('absentkey3.absentkey3.absentkey3', "value", default=True) config_manager.set('absentkey4', "value", default=True) assert config_manager.as_dict().get('absentkey3') is None
def test_apply_raw(script_runner, tmp_path): tmp_dir = tmp_path / 'raw' tmp_dir.mkdir() path = str(tmp_dir) _ = script_runner.run( 'config42', *args('-l key=value key2=value -c raw --raw-path ' + path)) ret = script_runner.run( 'config42', *args('-a apply -l key=value2 -c raw --raw-path ' + path)) assert ret.success config_manager = ConfigManager(path=path) assert config_manager.get('key') == 'value2' assert config_manager.get('key') == 'value2' assert config_manager.get('key2') is None assert len(config_manager.as_dict()) == 1
def test_load_and_dump_flush(sample_config): config_manager = ConfigManager() config_manager.set_many(sample_config, trigger_commit=False) assert len(config_manager.handler.load()) == 0 assert config_manager.handler.updated is True config_manager.handler.dump() assert config_manager.handler.updated is False assert len(config_manager.handler.load()) > 0 config_manager.handler.flush() assert len(config_manager.handler.load()) >= 0 assert config_manager.handler.updated is True config_manager.handler.destroy() assert len(config_manager.handler.load()) == 0
def bot(tmp_path, cwd): from instabot_py import InstaBot import logging.config _config = ConfigManager(defaults={'config42': OrderedDict( [ ('env', {'prefix': 'INSTABOT'}), ('file', {'path': cwd + '/files/instabot.config.yml'}), ] )}) logging.basicConfig(level=logging.DEBUG) return InstaBot(**_config.as_dict(), session_file=str(tmp_path / "requests.session"), database={ "type": "sql", "connection_string": "sqlite:///" + str(tmp_path / "sqlite.db")})
def test_validation_casting(): schema = [{ "name": "user id ", "key": "user.id", "type": "integer", }, { "name": "user code ", "key": "user.code", "type": "string" } ] config_manager = ConfigManager(schema=schema, defaults={'user': {'id': '12345', 'code': 12345}}) assert config_manager.get('user.code') == '12345' assert config_manager.get('user.id') == 12345
def __init__(self, prefix, **kwargs): """ Initialize the handler data store. :param prefix: environment prefix for configuration map :type key: string """ super().__init__() configmanager = ConfigManager() for key, value in os.environ.items(): if key.startswith(prefix + "_"): configmanager.set( key.replace(prefix + "_", "").replace("_", ".").lower(), value) self.in_memory_config = configmanager.as_dict() self.config = self.in_memory_config
def config(cwd): config = ConfigManager(defaults={'config42': OrderedDict( [ ('env', {'prefix': 'TEST'}), ('file', {'path': cwd + '/files/config.yml'}), ] )}) return config
def main(): config = ConfigManager() config.set_many(DEFAULT_CONFIG) _config = ConfigManager(schema=schema, defaults=defaults) config.set_many(_config.as_dict()) config.set_many( ConfigManager(schema=schema, path=_config.get('config.file')).as_dict()) config.set_many(_config.as_dict()) config.commit() if config.get('dump_configuration'): conf = config.as_dict() conf.pop('config42') print(yaml.dump(conf)) exit(0) if config.get('show_version'): print("Installed version {}".format(instabot_py.__version__)) exit(0) if not config.get('ignore_updates'): last_version = get_last_version() if last_version and last_version != instabot_py.__version__: print( "Newer version available: {}, The current version: {}".format( last_version, instabot_py.__version__)) print( "To update, please type \n python3 -m pip install instabot-py --upgrade --no-cache-dir " ) print("") print( " > You can ignore warning, run the instabot with --ignore-updates flag" ) exit(0) if config.get('verbosity'): verbosity = int(config.get('verbosity')) if verbosity == 1: level = logging.INFO elif verbosity > 1: level = logging.DEBUG config.set("logging.root.level", level) logging.config.dictConfig(config.get("logging")) try: bot = InstaBot(config=config) except CredsMissing: print( "You didn't provide your Instagram login & password or you didn't specify the configuration file" ) print("Try again :") print("") print(" instabot-py --login YOUR_LOGIN --password YOUR_PASSWORD") print(" instabot-py -c your-config.yml") print( "You can export and modify the default configuration by typing the command below" ) print(" instabot-py --dump") exit(1) bot.mainloop()
def main(): config = ConfigManager() config.set_many(DEFAULT_CONFIG) _config = ConfigManager(schema=schema, defaults=defaults) config.set_many(_config.as_dict()) config.set_many(ConfigManager(path=_config.get('config.file')).as_dict()) config.set_many(_config.as_dict()) config.commit() if config.get('dump_configuration'): conf = config.as_dict() conf.pop('config42') print(yaml.dump(conf)) exit(0) if config.get('show_version'): print(instabot_py.__version__) exit(0) if config.get('verbosity'): verbosity = int(config.get('verbosity')) if verbosity == 1: level = logging.INFO elif verbosity > 1: level = logging.DEBUG config.set("logging.root.level", level) logging.config.dictConfig(config.get("logging")) bot = InstaBot(config=config) bot.mainloop()
def test_validation_defaults_configmanager(): schema = [{ "name": "user id ", "key": "user.id", "type": "integer", "default": 1234 }, { "name": "user name ", "key": "user.name", } ] with pytest.raises(ValidationError): _ = ConfigManager(schema=schema) config_manager = ConfigManager(schema=schema, defaults={'user': {'name': 'user'}}) assert config_manager.get('user.name') == 'user' assert config_manager.get('user.id') == 1234
def test_collect(tmp_path, mock_request_get): defaults = { 'database': f'sqlite:///{tmp_path}/test_db.sqlite3', 'collect': { 'profiles': ['profile'] } } config = ConfigManager(defaults=defaults) bot = Instabot(config) bot.collect()
def test_validation_with_configmanager(): schema = [{ "name": "user id ", "key": "user.id", "type": "integer", "required": True }, { "name": "user name ", "key": "user.name", "type": "string", "required": True } ] config_sample = {'user': {'id': 1234, 'name': 'user'}} config_manager = ConfigManager(schema=schema, defaults=config_sample) assert config_manager.get('user.name') == 'user' with pytest.raises(ValidationError): _ = ConfigManager(schema=schema, defaults={}) # Disable validator _ = ConfigManager(schema=schema, validator=False, defaults={})
def main(argv: Optional[Sequence[str]] = None) -> int: # tbd; use click ? parser = argparse.ArgumentParser() parser.add_argument( '--config-path', type=str, default='.pairing-matrix.conf.yaml', help='Path to configuration', ) parser.add_argument( '--config-format', type=str, default='yaml', help='Enforce config file parser; <[yaml]|json|ini|raw>', ) parser.add_argument( '--timespan', type=str, default=None, help='\n'.join([ 'Timespan `<from> - <to> <TZ?> <now?>`', 'See https://github.com/nickmaccarthy/python-datemath.' 'If it contains a timezone, that will be taken into account, too.', ]), ) args = parser.parse_args(argv) config_file_path = find_dotenv(filename=args.config_path, raise_error_if_not_found=True) user_config = ConfigManager(path=config_file_path, extension=args.config_format).as_dict() config = merge_config(DEFAULT_CONFIG, user_config) config = merge_config(config, vars(args)) try: output_opts = config.get('output', {}) highlight = output_opts.get('highlight_user') if highlight and not isinstance(highlight, str): user_email = (Repo( os.path.dirname(config_file_path)).config_reader().get( 'user', 'email')) output_opts['highlight_user'] = user_email config = merge_config(config, {'output': output_opts}) except: # noqa pass return Main(config)
def get_config() -> Dict[str, any]: """Read environment config vars """ schema = [ dict( key="verbose", name="Verbose", source=dict(argv=["-v"], argv_options=dict(action="count")), description="Turn on verbose logging", default=0, required=False, ), dict( key="url", name="URL", description="The target URL to, uh, test.", source=dict(argv=["-u"]), default="", required=False, type="string", ), dict( key="iter", name="Iteration", description="How many times to send the cookies", source=dict(argv=["-i"]), type="integer", default=1, required=False, ), dict( key="dryrun", name="DryRun", description="Just fetch the cookies", source=dict(argv=["-d"], argv_options=dict(action="count")), default=0, required=False, ), ] config = ConfigManager(schema=schema, defaults={ "config42": OrderedDict([ ('argv', dict(handler=ArgParse, schema=schema)), ('env', dict(prefix="CM")), ]) }) return config
def main(): config = ConfigManager() config.set_many(DEFAULT_CONFIG) _config = ConfigManager(schema=schema, defaults=defaults) config.set_many(_config.as_dict()) config_file = _config.get('config.file') config.set_many(ConfigManager(schema=schema, path=config_file).as_dict()) config.set_many(_config.as_dict()) config.commit() configure_logging(config) if config.get('dump_configuration'): conf = config.as_dict() conf.pop('config42') conf.pop('dump_configuration') print(yaml.dump(conf)) exit(0) if config.get('show_version'): print("Installed version {}".format(src.__version__)) exit(0) if not config.get('ignore_updates'): last_version = get_last_version() if last_version and last_version != src.__version__: print("Newer version available: {}, The current version: {}".format(last_version, src.__version__)) print("To update, please type \n python3 -m pip install instabot-py --upgrade --no-cache-dir ") print("") print(" > You can ignore warning, run the instabot with --ignore-updates flag") exit(0) try: bot = InstaBot(config=config) if config_file: bot.logger.info(f"Reading configuration ({len(_config.as_dict())} settings) from {config_file}") else: bot.logger.info(f"Use the default configuration, add '-c your-config.yml' to specify your config") except CredsMissing: print("You didn't provide your Instagram login & password or you didn't specify the configuration file") print("Try again :") print("") print(" instabot-py --login YOUR_LOGIN --password YOUR_PASSWORD") print(" instabot-py -c your-config.yml") print("You can export and modify the default configuration by typing the command below") print(" instabot-py --dump") exit(1) bot.mainloop()
def test_configuration_content(cwd): defaults = { 'config42': { 'file': { 'path': cwd + "/files/nested-config.yml" } } } os.environ.update({"BARBEROUSSE_ENV1": "TEST1"}) config_manager = ConfigManager(defaults=defaults) assert config_manager.get('config42') is not None assert len(config_manager.get('config42')) > 1 assert config_manager.get('env1') == "TEST1" assert config_manager.get('nested') == "value"
def main(): config = ConfigManager() config.set_many(DEFAULT_CONFIG) _config = ConfigManager(schema=schema, defaults=defaults) config.set_many(_config.as_dict()) config_file = _config.get('config.file') config.set_many(ConfigManager(schema=schema, path=config_file).as_dict()) config.set_many(_config.as_dict()) config.commit() configure_logging(config) if config.get('dump_configuration'): conf = config.as_dict() conf.pop('config42') conf.pop('dump_configuration') print(yaml.dump(conf)) exit(0) if config.get('create_configuration'): create_configuration() exit(0) if config.get('show_version'): print("Installed version {}".format(instabot_py.__version__)) exit(0) if not config.get('ignore_updates'): last_version = get_last_version() current_version = instabot_py.__version__ if last_version and last_version != current_version: print(f"""Newer version is available: {last_version}. The current \ version is: {current_version}. To update instabot-py, please perform: python3 -m pip install instabot-py --upgrade --no-cache-dir > You can also ignore this warning and upgrade instabot-py later. In this \ case, please run the instabot with '--ignore-updates' flag.""") exit(0) if config_file: print(f"Reading configuration ({len(_config.as_dict())} settings) from" f" {config_file}") elif os.path.isfile('instabot.config.yml'): print("Using 'instabot.config.yml' as a configuration, add " "'-c your-config.yml' if you want to use your config file") else: print("Configuration file has not been found. Please run the instabot " "with '--create-config' flag.") exit(0) try: bot = InstaBot(config=config) except CredsMissing: print( """We could not find your Instagram login and/or password. Maybe \ you did not change the default ones in the config file. You can specify them either directly, correct them in the default config file \ or use your own config file: instabot-py --login YOUR_LOGIN --password YOUR_PASSWORD or instabot-py -c your-config.yml """) exit(1) bot.mainloop()
def test_configuration_content_index_error(sample_config): config_manager = ConfigManager() config_manager.set_many(sample_config) assert config_manager.get('nested_list.0.4') is None
def test_configuration_content_absent_keys(): config_manager = ConfigManager() assert config_manager.get('absentkey1') is None assert config_manager.get('absentkey2.absentkey2') is None assert config_manager.get('absentkey3.absentkey3.absentkey3') is None
def test_configuration_trigger_commit(sample_config): config_manager = ConfigManager() config_manager.set_many(sample_config) config_manager.commit() assert sample_config['simple'] == config_manager.get('simple') config_manager.set('new_key', 'new_value') assert config_manager.handler.updated is False config_manager.set('new_key2', 'new_value2', trigger_commit=False) assert config_manager.handler.updated is True config_manager.commit() assert config_manager.handler.updated is False config_manager.set('new_key', 'new_value', trigger_commit=True) assert config_manager.handler.updated is False
def test_nested_key(): config_manager = ConfigManager() config_manager.set('key1.key2', 'value') assert config_manager.handler.config.get('key1') is not None assert config_manager.handler.config.get('key1').get('key2') == 'value'
def test_configuration_default_values(default_config): config_manager = ConfigManager(defaults=default_config) assert default_config['defaultkey1'] == config_manager.get('defaultkey1') assert default_config['defaultkey2']['defaultkey2'] == config_manager.get('defaultkey2.defaultkey2') assert config_manager.get('absentkey3.absentkey3.absentkey3') is None