def test_option_specified_twice(self): """Test schemaconfigglue with option name specified twice.""" class MySchema(Schema): foo = IntOption(short_name='f') parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue( parser, argv=['-f', '42', '--foo', '24']) self.assertEqual(parser.get('__main__', 'foo'), 24) op, options, args = schemaconfigglue( parser, argv=['-f', '24', '--foo', '42']) self.assertEqual(parser.get('__main__', 'foo'), 42)
def test_option_specified_twice(self): """Test schemaconfigglue with option name specified twice.""" class MySchema(Schema): foo = IntOption(short_name='f') parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue(parser, argv=['-f', '42', '--foo', '24']) self.assertEqual(parser.get('__main__', 'foo'), 24) op, options, args = schemaconfigglue(parser, argv=['-f', '24', '--foo', '42']) self.assertEqual(parser.get('__main__', 'foo'), 42)
def test_glue_no_argv(self): """Test schemaconfigglue with the default argv value.""" config = StringIO("[__main__]\nbaz=1") self.parser.readfp(config) self.assertEqual(self.parser.values(), { 'foo': { 'bar': 0 }, '__main__': { 'baz': 1 } }) _argv, sys.argv = sys.argv, [] try: op, options, args = schemaconfigglue(self.parser) self.assertEqual(self.parser.values(), { 'foo': { 'bar': 0 }, '__main__': { 'baz': 1 } }) finally: sys.argv = _argv
def test_option_short_name(self): """Test schemaconfigglue support for short option names.""" class MySchema(Schema): foo = IntOption(short_name='f') parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue(parser, argv=['-f', '42']) self.assertEqual(parser.get('__main__', 'foo'), 42)
def test_parser_unicode(self): s = textwrap.dedent(""" [__main__] bar = zátrapa """) _, cg, _ = configglue(StringIO(s)) _, sg, _ = schemaconfigglue(ini2schema(StringIO(s))) self.assertEqual(vars(cg), vars(sg))
def test_parser_set_with_encoding(self): """Test schemaconfigglue override an option with a non-ascii value.""" class MySchema(Schema): foo = StringOption() parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue(parser, argv=['--foo', 'fóobâr']) self.assertEqual(parser.get('__main__', 'foo', parse=False), 'fóobâr') self.assertEqual(parser.get('__main__', 'foo'), 'fóobâr')
def test_option_short_name(self): """Test schemaconfigglue support for short option names.""" class MySchema(Schema): foo = IntOption(short_name='f') parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue( parser, argv=['-f', '42']) self.assertEqual(parser.get('__main__', 'foo'), 42)
def test_fatal_option_with_config(self): class MySchema(Schema): foo = IntOption(fatal=True) config = StringIO("[__main__]\nfoo=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) op, options, args = schemaconfigglue(parser) self.assertEqual(parser.values(), {'__main__': {'foo': 1}})
def test_parser_unicode(self): s = textwrap.dedent(""" [__main__] bar = zátrapa bar.parser = unicode bar.parser.args = utf-8 """) _, cg, _ = configglue(StringIO(s)) _, sg, _ = schemaconfigglue(ini2schema(StringIO(s))) self.assertEqual(vars(cg), vars(sg))
def test_glue_no_op(self): """Test schemaconfigglue with the default OptionParser value.""" config = StringIO("[__main__]\nbaz=1") self.parser.readfp(config) self.assertEqual(self.parser.values(), {'foo': {'bar': 0}, '__main__': {'baz': 1}}) op, options, args = schemaconfigglue(self.parser, argv=['--baz', '2']) self.assertEqual(self.parser.values(), {'foo': {'bar': 0}, '__main__': {'baz': 2}})
def test_glue_json_dict(self): class MySchema(Schema): foo = DictOption() parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue(parser, argv=['--foo', '{"bar": "baz"}']) self.assertEqual(options, {'foo': '{"bar": "baz"}'}) self.assertEqual(parser.values(), {'__main__': {'foo': {'bar': 'baz'}}})
def test_glue_section_option(self): """Test schemaconfigglue overriding one option.""" config = StringIO("[foo]\nbar=1") self.parser.readfp(config) self.assertEqual(self.parser.values(), {'foo': {'bar': 1}, '__main__': {'baz': 0}}) op, options, args = schemaconfigglue(self.parser, argv=['--foo_bar', '2']) self.assertEqual(self.parser.values(), {'foo': {'bar': 2}, '__main__': {'baz': 0}})
def test_parser_set_with_encoding(self): """Test schemaconfigglue override an option with a non-ascii value.""" class MySchema(Schema): foo = StringOption() parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue( parser, argv=['--foo', 'fóobâr']) self.assertEqual(parser.get('__main__', 'foo', parse=False), 'fóobâr') self.assertEqual(parser.get('__main__', 'foo'), 'fóobâr')
def test_glue_environ(self, mock_os): mock_os.environ = {'CONFIGGLUE_FOO_BAR': '42', 'CONFIGGLUE_BAZ': 3} config = BytesIO(b"[foo]\nbar=1") self.parser.readfp(config) _argv, sys.argv = sys.argv, ['prognam'] try: op, options, args = schemaconfigglue(self.parser) self.assertEqual(self.parser.values(), {'foo': {'bar': 42}, '__main__': {'baz': 3}}) finally: sys.argv = _argv
def test_glue_environ_bad_name(self, mock_os): mock_os.environ = {'FOO_BAR': 2, 'BAZ': 3} config = StringIO("[foo]\nbar=1") self.parser.readfp(config) _argv, sys.argv = sys.argv, ['prognam'] try: op, options, args = schemaconfigglue(self.parser) self.assertEqual(self.parser.values(), {'foo': {'bar': 1}, '__main__': {'baz': 0}}) finally: sys.argv = _argv
def create_parser(self, prog_name, subcommand): """ Add all our SchemaConfigParser's options so they can be shown in help messages and such. """ parser = OptionParser(prog=prog_name, usage=self.usage(subcommand), version=self.get_version(), option_list=self.option_list) configglue_parser = settings.__CONFIGGLUE_PARSER__ op, options, args = schemaconfigglue(configglue_parser, op=parser) return op
def get_options(self): '''With this method we will be pre-parsing options for our program, basically it is a parser to re-use configglue in the vauxoo's way, with the minimal configuration for our scripts, une time you instance the VauxooTools class in your script you will have available the minimal config parameter to be used against any openerp instance avoiding the need to re-implement the wheel any time you write a xml-rpc script with any of the tools availables. Instanciate the config in your application. >>> configuration = VauxooTools(app_name='TestApi', ... options=['hostname', 'port']) Ask for options. >>> result = configuration.get_options() >>> print result {'hostname': 'localhost', 'port': 8069, 'args': []} Where args will be the parameter passed to your script use it to receive parameters from the console. If you don't pass options you will receive an empty dict, with only the args key, you will need to valid both in your code to ensure it is empty if you need it. >>> configuration = VauxooTools(app_name='TestApi') >>> result = configuration.get_options() >>> print result {'args': []} ''' result = {} options = self.options self.scp.read(self.appconfig.config.get_config_files(self.appconfig)) opt, opts, args = glue.schemaconfigglue(self.scp) self.logger.info(opts) is_valid, reasons = self.scp.is_valid(report=True) if not is_valid: opt.error(reasons[0]) values = self.scp.values('__main__') if options is not None: if self.log: options.append('logfile') options.append('loglevel') for option in options: value = values.get(option) result[option] = value else: pass result['args'] = args return result
def test_glue_environ_precedence_null_and_fatal_option(self): class MySchema(Schema): foo = StringOption(null=True, fatal=True) parser = SchemaConfigParser(MySchema()) with patch.object(os, 'environ', {'CONFIGGLUE_FOO': '42'}): _argv, sys.argv = sys.argv, ['prognam'] try: op, options, args = schemaconfigglue(parser) self.assertEqual(parser.get('__main__', 'foo'), '42') finally: sys.argv = _argv
def test_glue_environ_precedence(self): with patch.object(os, 'environ', {'CONFIGGLUE_FOO_BAR': '42', 'BAR': '1'}): config = StringIO("[foo]\nbar=$BAR") self.parser.readfp(config) _argv, sys.argv = sys.argv, ['prognam'] try: op, options, args = schemaconfigglue(self.parser) self.assertEqual(self.parser.get('foo', 'bar'), 42) finally: sys.argv = _argv
def execute(self): """ Given the command-line arguments, this figures out which subcommand is being run, creates a parser appropriate to that command, and runs it. """ # Preprocess options to extract --settings and --pythonpath. # These options could affect the commands that are available, so they # must be processed early. parser = LaxOptionParser(usage="%prog subcommand [options] [args]", version=django.get_version(), option_list=BaseCommand.option_list) try: configglue_parser = settings.__CONFIGGLUE_PARSER__ parser, options, args = schemaconfigglue(configglue_parser, op=parser, argv=self.argv) # remove schema-related options from the argv list self.argv = args utils.update_settings(configglue_parser, settings) except AttributeError: # no __CONFIGGLUE_PARSER__ found, fall back to standard django # options parsing options, args = parser.parse_args(self.argv) handle_default_options(options) except: # Ignore any option errors at this point. args = self.argv try: subcommand = self.argv[1] except IndexError: sys.stderr.write("Type '%s help' for usage.\n" % self.prog_name) sys.exit(1) if subcommand == 'help': if len(args) > 2: self.fetch_command(args[2]).print_help(self.prog_name, args[2]) else: parser.print_lax_help() sys.stderr.write(self.main_help_text() + '\n') sys.exit(1) # Special-cases: We want 'django-admin.py --version' and # 'django-admin.py --help' to work, for backwards compatibility. elif self.argv[1:] == ['--version']: # LaxOptionParser already takes care of printing the version. pass elif self.argv[1:] == ['--help']: parser.print_lax_help() sys.stderr.write(self.main_help_text() + '\n') else: self.fetch_command(subcommand).run_from_argv(self.argv)
def test_glue_no_argv(self): """Test schemaconfigglue with the default argv value.""" config = StringIO("[__main__]\nbaz=1") self.parser.readfp(config) self.assertEqual(self.parser.values(), {'foo': {'bar': 0}, '__main__': {'baz': 1}}) _argv, sys.argv = sys.argv, [] try: op, options, args = schemaconfigglue(self.parser) self.assertEqual(self.parser.values(), {'foo': {'bar': 0}, '__main__': {'baz': 1}}) finally: sys.argv = _argv
def test_glue_json_dict(self): class MySchema(Schema): foo = DictOption() parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue(parser, argv=['--foo', '{"bar": "baz"}']) self.assertEqual(options, {'foo': '{"bar": "baz"}'}) self.assertEqual(parser.values(), {'__main__': { 'foo': { 'bar': 'baz' } }})
def test_glue_environ_precedence(self): with patch.object(os, 'environ', { 'CONFIGGLUE_FOO_BAR': '42', 'BAR': '1' }): config = StringIO("[foo]\nbar=$BAR") self.parser.readfp(config) _argv, sys.argv = sys.argv, ['prognam'] try: op, options, args = schemaconfigglue(self.parser) self.assertEqual(self.parser.get('foo', 'bar'), 42) finally: sys.argv = _argv
def execute(self): """Override the base class to handle the schema-related options. """ configglue_parser = getattr(settings, '__CONFIGGLUE_PARSER__', None) if configglue_parser is not None: # We need a lax option parser that: # - allows the '%prog subcommand [options] [args]' format # - will receive the schema options from configglue # - doesn't attempt to recognize django options lax_parser = LaxOptionParser( usage="%prog subcommand [options] [args]") parser, options, args = schemaconfigglue( configglue_parser, op=lax_parser, argv=self.argv) utils.update_settings(configglue_parser, settings) # remove schema-related options from the argv list self.argv = args super(GlueManagementUtility, self).execute()
def execute(self): """Override the base class to handle the schema-related options. """ configglue_parser = getattr(settings, '__CONFIGGLUE_PARSER__', None) if configglue_parser is not None: # We need a lax option parser that: # - allows the '%prog subcommand [options] [args]' format # - will receive the schema options from configglue # - doesn't attempt to recognize django options lax_parser = LaxOptionParser( usage="%prog subcommand [options] [args]") parser, options, args = schemaconfigglue(configglue_parser, op=lax_parser, argv=self.argv) utils.update_settings(configglue_parser, settings) # remove schema-related options from the argv list self.argv = args super(GlueManagementUtility, self).execute()
def test_glue_missing_section(self): """Test schemaconfigglue with missing section.""" class MySchema(Schema): foo = DictOption() config = StringIO("[__main__]\nfoo = bar") parser = SchemaConfigParser(MySchema()) parser.readfp(config) # hitting the parser directly raises an exception self.assertRaises(NoSectionError, parser.values) self.assertFalse(parser.is_valid()) # which is nicely handled by the glue code, so as not to crash it op, options, args = schemaconfigglue(parser) # there is no value for 'foo' due to the missing section self.assertEqual(options, {'foo': None})
def test_glue_environ_bad_name(self, mock_os): mock_os.environ = {'FOO_BAR': 2, 'BAZ': 3} config = StringIO("[foo]\nbar=1") self.parser.readfp(config) _argv, sys.argv = sys.argv, ['prognam'] try: op, options, args = schemaconfigglue(self.parser) self.assertEqual(self.parser.values(), { 'foo': { 'bar': 1 }, '__main__': { 'baz': 0 } }) finally: sys.argv = _argv
def test_ambiguous_option(self): """Test schemaconfigglue when an ambiguous option is specified.""" class MySchema(Schema): class foo(Section): baz = IntOption() class bar(Section): baz = IntOption() config = StringIO("[foo]\nbaz=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values('foo'), {'baz': 1}) self.assertEqual(parser.values('bar'), {'baz': 0}) op, options, args = schemaconfigglue(parser, argv=['--bar_baz', '2']) self.assertEqual(parser.values('foo'), {'baz': 1}) self.assertEqual(parser.values('bar'), {'baz': 2})
def test_ambiguous_option(self): """Test schemaconfigglue when an ambiguous option is specified.""" class MySchema(Schema): class foo(Section): baz = IntOption() class bar(Section): baz = IntOption() config = StringIO("[foo]\nbaz=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values('foo'), {'baz': 1}) self.assertEqual(parser.values('bar'), {'baz': 0}) op, options, args = schemaconfigglue( parser, argv=['--bar_baz', '2']) self.assertEqual(parser.values('foo'), {'baz': 1}) self.assertEqual(parser.values('bar'), {'baz': 2})
def test_glue_no_op(self): """Test schemaconfigglue with the default OptionParser value.""" config = StringIO("[__main__]\nbaz=1") self.parser.readfp(config) self.assertEqual(self.parser.values(), { 'foo': { 'bar': 0 }, '__main__': { 'baz': 1 } }) op, options, args = schemaconfigglue(self.parser, argv=['--baz', '2']) self.assertEqual(self.parser.values(), { 'foo': { 'bar': 0 }, '__main__': { 'baz': 2 } })
def test_glue_section_option(self): """Test schemaconfigglue overriding one option.""" config = StringIO("[foo]\nbar=1") self.parser.readfp(config) self.assertEqual(self.parser.values(), { 'foo': { 'bar': 1 }, '__main__': { 'baz': 0 } }) op, options, args = schemaconfigglue(self.parser, argv=['--foo_bar', '2']) self.assertEqual(self.parser.values(), { 'foo': { 'bar': 2 }, '__main__': { 'baz': 0 } })
def test_main(self): s = "[__main__]\nbar = 42\n" _, cg, _ = configglue(StringIO(s)) _, sg, _ = schemaconfigglue(ini2schema(StringIO(s))) self.assertEqual(vars(cg), vars(sg))
def configglue(fileobj, *filenames, **kwargs): args = kwargs.pop('args', None) parser, opts, args = schemaconfigglue(ini2schema(fileobj), argv=args) return IniGlue(parser, opts, args)
def test_parser_bool(self): s = "[__main__]\nbar = true\nbar.parser = bool \n" _, cg, _ = configglue(StringIO(s)) _, sg, _ = schemaconfigglue(ini2schema(StringIO(s))) self.assertEqual(vars(cg), vars(sg))
def test_parser_none(self): s = "[__main__]\nbar = meeeeh\nbar.parser = none" _, cg, _ = configglue(StringIO(s), extra_parsers=[("none", str)]) _, sg, _ = schemaconfigglue(ini2schema(StringIO(s))) self.assertEqual(vars(cg), vars(sg))
def test_empty(self): s = "" _, cg, _ = configglue(StringIO(s)) _, sg, _ = schemaconfigglue(ini2schema(StringIO(s))) self.assertEqual(vars(cg), vars(sg))
def test_parser_none(self): s = "[__main__]\nbar = meeeeh\nbar.parser = none" _, cg, _ = configglue(StringIO(s), extra_parsers=[('none', str)]) _, sg, _ = schemaconfigglue(ini2schema(StringIO(s))) self.assertEqual(vars(cg), vars(sg))