def setup_method(self): config_spec = self.config_spec = ConfigSpec() config_spec.add_option("not_in_json", basestring, default=self.DEFAULT) config_spec.add_option("string", basestring) config_spec.add_option("int", int) config_spec.add_option("bool", bool) config_spec.add_option("dict", dict) self.config_parser = ConfigParser(config_spec)
class TestConfigParser(object): DEFAULT = '_default_' def setup_method(self): config_spec = self.config_spec = ConfigSpec() config_spec.add_option("not_in_json", basestring, default=self.DEFAULT) config_spec.add_option("string", basestring) config_spec.add_option("int", int) config_spec.add_option("bool", bool) config_spec.add_option("dict", dict) self.config_parser = ConfigParser(config_spec) def test_constructor(self): with pytest.raises(TypeError): ConfigParser("not a ConfigSpec") def test_load_config_nonexistent_file(self): # if the config file doesn't exist, error with pytest.raises(IOError): self.config_parser.load_config( os.path.join(FIXTURE_DIRECTORY, 'nonexistent.json')) # nothing loaded assert self.config_parser.config == {} def test_load_config_invalid_file(self): # if the config is invalid, error with pytest.raises(ValueError): self.config_parser.load_config( os.path.join(FIXTURE_DIRECTORY, 'invalid-json-config.json')) # nothing loaded assert self.config_parser.config == {} def test_load_config_picks_up_values(self): self.config_parser.load_config( os.path.join(FIXTURE_DIRECTORY, 'config.json')) assert self.config_parser.config['string'] == 'value' assert self.config_parser.config['int'] == 3 assert self.config_parser.config['bool'] is False assert self.config_parser.config['dict'] == { 'dict': { 'string': 'value' }, 'list': ['foo', 'bar', 'baz'], } # If not found in the file, default assert self.config_parser.config['not_in_json'] == self.DEFAULT # This was in the file but not the spec and should not appear in config assert 'ignored_string' not in self.config_parser.config
def test_constructor(self): with pytest.raises(TypeError): ConfigParser("not a ConfigSpec")
spec.add_option('channels', list, ['#bots']) spec.add_option('plugins', list, [ 'ping', 'help', 'admin', 'join_on_invite', 'urls', 'calculator', 'lastfm', 'remind', 'weather', 'youtube', 'urbandict' ]) parser = ConfigParser(spec) # Parse command-line arguments logger.debug("Parsing command-line arguments") args = arg_parser.parse_args() # Attempt to load config.json for config options config_file = args.config if config_file is None: config_file = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'config.json' ) logger.info("Attempting to load config: %s" % config_file) parser.load_config(config_file)
spec.add_option('network', basestring, 'irc.freenode.net') spec.add_option('port', int, 6667) spec.add_option('server_password', basestring, None) spec.add_option('ssl', bool, False) spec.add_option( 'storage', basestring, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'storage')) spec.add_option('channels', list, ['#bots']) spec.add_option('plugins', list, [ 'ping', 'help', 'admin', 'join_on_invite', 'urls', 'calculator', 'lastfm', 'remind', 'weather', 'youtube', 'urbandict' ]) spec.add_option('logging', dict, None) parser = ConfigParser(spec) # Parse command-line arguments args = arg_parser.parse_args() # Attempt to load config.json for config options config_file = args.config if config_file is None: config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.json') # Load config file parser.load_config(config_file) # If SSL is set to false, set it to None (small hack - action 'store_true' # in arg_parse defaults to False. False instead of None will overwrite our
spec.add_option('port', int, 6697) spec.add_option('ssl', bool, True) spec.add_option('channels', list, ['#57N']) spec.add_option('plugins', list, [ 'ping', 'help', 'admin', 'join_on_invite', 'urls', 'calculator', 'lastfm', 'remind', 'weather', 'youtube' ]) parser = ConfigParser(spec) # First attempt to load config.json for config options logging.info("Looking for and attempting to load config.json") parser.load_config('config.json') # Parse command-line arguments last, as they should override both project # defaults and the user config (if available) logging.info("Parsing command-line arguments") args = arg_parser.parse_args() # If SSL is set to false, set it to None (small hack - action 'store_true' # in arg_parse defaults to False. False instead of None will overwrite our # config settings.) if not args.ssl: args.ssl = True
spec.add_option('port', int, 6667) spec.add_option('server_password', basestring, None) spec.add_option('ssl', bool, False) spec.add_option( 'storage', basestring, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'storage')) spec.add_option('channels', list, ['#bots']) spec.add_option('plugins', list, [ 'wikipedia', 'ping', 'help', 'admin', 'join_on_invite', 'urls', 'calculator', 'lastfm', 'remind', 'weather', 'youtube', 'urbandict' ]) spec.add_option('blacklist', dict, {}) spec.add_option('logging', dict, None) parser = ConfigParser(spec) # Load config file try: config = parser.load_config(config_file) except Exception: # Need to setup a logger early logger = setup_logging() logger.exception("Unable to load config: {}".format(config_file)) sys.exit(1) # Config loaded, setup the logger logger = setup_logging(config['logging']) logger.info("Config loaded: {}".format(config_file))