class ConfTestCase(unittest.TestCase): '''Test case for partial template execution ''' def setUp(self): self.settings = Settings('tests/test.cfg') def testGetFromMainConf(self): '''Test getting options from main config file''' assert self.settings.value == 'test', ('Get returns wrong value %s ' 'except test' % self.settings.value) assert self.settings.value == self.settings.get('value'), ( '__getitem__ and get returns different values') assert self.settings.VALUE == 'test', 'Lower case error' def testGetFromSection(self): '''Check getting option value from section''' assert self.settings.get('value', 'DEFAULTS') == 'test', ( 'Error on getting value from DEFAULTS section') def testConfsLoaded(self): '''Test is config files was loaded properly''' assert self.settings.app_var == '2', 'CONFS was not loaded properly' assert self.settings.get('app_var', 'APP1') == '1', ( 'APPS configs was not loaded') assert self.settings.get('app_var', 'APP2') == '2', ( 'APPS configs was not loaded') def testAppLoaded(self): '''Test is application was loaded''' assert self.settings.test == 'value', 'APPS configs was not loaded' def testSections(self): '''Test is all values in sections''' apps_req = TEST_APPS apps = sorted(self.settings.section_options('APPS')) assert apps == apps_req, ('APPS section was not loaded properly: %s' % apps) assert self.settings.section_options('APP1') == ['app_var'], ( 'APP1 section was not loaded properly: %s' % self.settings.section_options('APP1')) assert self.settings.section_options('APP2') == ['app_var'], ( 'APP2 section was not loaded properly: %s' % self.settings.section_options('APP1')) assert sorted(self.settings.section_options('CONFS')) == [ 'tests/conf/app1.cfg', 'tests/conf/app2.cfg'], ( 'CONFS section was not loaded properly')
def manage(default_conf='conf.cfg'): '''Process manage command. Load all the commands from applications in configuration files and add them into autocompletition ''' default_args = ((('arguments', ), {'nargs': 0, 'help': 'command arguments'}), ) def parse_args(command_arg_type=None, args=default_args): parser = CommandParser(description='Lighty manage script') parser.add_option('config', 'c', default=default_conf, flag=False, help='configuration file') if command_arg_type: parser.add_argument('command', type=command_arg_type, default='', optional=False, help='command to execute') else: parser.add_argument('command', default='', help='command to execute') for args, kwargs in args: parser.add_argument(*args, **kwargs) return parser, parser.parse() # Get config file parser, args = parse_args() try: settings = Settings(args['config']) init_db_manager(settings) commands = load_commands(settings.section_options('APPS')) except Exception as e: print_func(e) print_func(parser.help_text()) return # Try to get command name def error_msg(msg): raise TypeError('%s. Commands available:\n\t%s' % (msg, "\n\t".join(sorted(commands.keys())))) def command_name(name): if name == '': error_msg('No command specified') elif name not in commands: error_msg('Wrong command name') return name parser, args = parse_args(command_name) # Parse arguments for code name command = commands[args['command']] code = command.__code__ if code.co_argcount > 0: call_args = {} call_args[code.co_varnames[0]] = settings defaults = command.__defaults__ or () arg_index = 1 defaults_index = 0 defaults_start = code.co_argcount - len(defaults) args = [] for arg_name in code.co_varnames[1:command.__code__.co_argcount]: if defaults_start <= arg_index: args.append(((arg_name, ), {'default': defaults[defaults_index]})) else: args.append(((arg_name, ), {})) arg_index += 1 parser, args = parse_args(command_name, args) for arg_name in code.co_varnames[1:command.__code__.co_argcount]: call_args[arg_name] = args[arg_name] command(**call_args) else: command()