Exemple #1
0
    def set_options_from_dict(self, config):
        """
            Reads the configuration from a dictionary.
        
            raises: UserError: Wrong configuration, user's mistake.
                    Exception: all other exceptions
        """
        params = DecentParams()
        self.define_program_options(params)
        
        try:
            self.options = params.get_dpr_from_dict(config)
        except DecentParamsUserError as e:
            raise QuickAppException(str(e))
        except Exception as e:
            msg = 'Could not interpret:\n'
            msg += indent(pformat(config), '| ') 
            msg += 'according to params spec:\n'
            msg += indent(str(params), '| ') + '\n'
            msg += 'Error is:\n'
#             if isinstance(e, DecentParamsUserError):
#                 msg += indent(str(e), '> ')
#             else:
            msg += indent(traceback.format_exc(e), '> ')
            raise QuickAppException(msg)  # XXX class
Exemple #2
0
    def set_options_from_args(self, args):
        """
            Reads the configuration from command line arguments. 
            
            raises: UserError: Wrong configuration, user's mistake.
                    Exception: all other exceptions
        """
        cls = type(self)
        prog = cls.get_prog_name()
        params = DecentParams()
        self.define_program_options(params)
        
        try:
            usage = cls.get_usage()
            if usage:
                usage = usage.replace('%prog', prog)

            desc = cls.get_program_description()
            epilog = cls.get_epilog()
            self.options = \
                params.get_dpr_from_args(prog=prog, args=args, usage=usage,
                                         description=desc, epilog=epilog)
        except UserError:
            raise
        except Exception as e:
            msg = 'Could not interpret:\n'
            msg += ' args = %s\n' % args
            msg += 'according to params spec:\n'
            msg += indent(str(params), '| ') + '\n'
            msg += 'Error is:\n'
            msg += indent(traceback.format_exc(e), '> ')
            raise Exception(msg)  # XXX class
Exemple #3
0
    def set_options_from_args(self, args: List[str]):
        """
            Reads the configuration from command line arguments.

            raises: UserError: Wrong configuration, user's mistake.
                    Exception: all other exceptions
        """
        cls = type(self)
        prog = cls.get_prog_name()
        params = DecentParams()
        self.define_program_options(params)

        try:
            usage = cls.get_usage()
            if usage:
                usage = usage.replace('%prog', prog)

            desc = cls.get_program_description()
            epilog = cls.get_epilog()
            self.options = \
                params.get_dpr_from_args(prog=prog, args=args, usage=usage,
                                         description=desc, epilog=epilog)
        except UserError:
            raise
        except Exception as e:
            msg = 'Could not interpret:\n'
            msg += ' args = %s\n' % args
            msg += 'according to params spec:\n'
            msg += indent(str(params), '| ') + '\n'
            msg += 'Error is:\n'
            msg += indent(traceback.format_exc(), '> ')
            raise Exception(msg)  # XXX class
Exemple #4
0
 def decent_params_test2(self):
     """ Test compulsory """
     p = DecentParams()
     p.add_string('a')
     p.add_string('b')
     res = p.parse_args(['--b', '1'])
     print res
Exemple #5
0
    def set_options_from_dict(self, config: Dict[str, Any]):
        """
            Reads the configuration from a dictionary.

            raises: UserError: Wrong configuration, user's mistake.
                    Exception: all other exceptions
        """
        params = DecentParams()
        self.define_program_options(params)

        try:
            self.options = params.get_dpr_from_dict(config)
        except DecentParamsUserError as e:
            raise QuickAppException(str(e))
        except Exception as e:
            msg = 'Could not interpret:\n'
            msg += indent(pformat(config), '| ')
            msg += 'according to params spec:\n'
            msg += indent(str(params), '| ') + '\n'
            msg += 'Error is:\n'
            #             if isinstance(e, DecentParamsUserError):
            #                 msg += indent(str(e), '> ')
            #             else:
            msg += indent(traceback.format_exc(), '> ')
            raise QuickAppException(msg)  # XXX class
Exemple #6
0
 def decent_params_test2(self):
     """ Test compulsory """
     p = DecentParams()
     p.add_string('a')
     p.add_string('b')
     res = p.parse_args(['--b', '1'])
     print(res)
 def decent_params_test0(self):
     
     p = DecentParams()
     p.add_float_list('floats')
     
     args = ['--floats', '1.2', '2.3']
     
     try:    
         res = p.parse_args(args)
     except SystemExit as e:
         print(e)
         raise Exception(str(e))
     
     self.assertEqual(res.given('floats'), True)
     self.assertEqual(res.given('int1'), False)
     self.assertEqual(res['floats'], [1.2, 2.3])
Exemple #8
0
    def decent_params_test0(self):

        p = DecentParams()
        p.add_float_list('floats')

        args = ['--floats', '1.2', '2.3']

        try:
            res = p.parse_args(args)
        except SystemExit as e:
            print(e)
            raise Exception(str(e))

        self.assertEqual(res.given('floats'), True)
        self.assertEqual(res.given('int1'), False)
        self.assertEqual(res['floats'], [1.2, 2.3])
Exemple #9
0
 def decent_params_test1(self):
     
     p = DecentParams()
     p.add_string('vehicle', default='x')
     p.add_float('float1')
     p.add_float_list('floats')
     p.add_int('int1', default=0)
     p.add_int_list('ints')
     p.add_string_choice('ciao', ['2', '1'], default='1')
     p.add_int_choice('ciao2', [2, 1], default=1)
     
     args = ['--vehicle', 'y',
             '--float1', '1.2',
             '--floats', '1.2', '2.3',
             '--ints', '1', '2', '3']
     
     try:    
         res = p.parse_args(args)
     except SystemExit as e:
         print e
         raise Exception(str(e))
     
     self.assertEqual(res.given('floats'), True)
     self.assertEqual(res.given('int1'), False)
     self.assertEqual(res['ciao'], '1')
     self.assertEqual(res['ciao2'], 1)
     self.assertEqual(res.ciao, '1')
     self.assertEqual(res.ciao2, 1)
     self.assertEqual(res.floats, [1.2, 2.3])
     self.assertEqual(res.ints, [1, 2, 3])
Exemple #10
0
 def decent_params_test_notdefined(self):
     p = DecentParams()
     p.add_string('vehicle', default='x')
     
     self.assertRaises(DecentParamsUnknownArgs, p.parse_args, ['--b', '1'])
Exemple #11
0
def parse_mcdpweb_params_from_dict(x):
    dp = DecentParams()
    describe_mcdpweb_params(dp)
    return dp.get_dpr_from_dict(x)
Exemple #12
0
    def decent_params_test_notdefined(self):
        p = DecentParams()
        p.add_string('vehicle', default='x')

        self.assertRaises(DecentParamsUnknownArgs, p.parse_args, ['--b', '1'])
Exemple #13
0
    def decent_params_test1(self):

        p = DecentParams()
        p.add_string('vehicle', default='x')
        p.add_float('float1')
        p.add_float_list('floats')
        p.add_int('int1', default=0)
        p.add_int_list('ints')
        p.add_string_choice('ciao', ['2', '1'], default='1')
        p.add_int_choice('ciao2', [2, 1], default=1)

        args = [
            '--vehicle', 'y', '--float1', '1.2', '--floats', '1.2', '2.3',
            '--ints', '1', '2', '3'
        ]

        try:
            res = p.parse_args(args)
        except SystemExit as e:
            print(e)
            raise Exception(str(e))

        self.assertEqual(res.given('floats'), True)
        self.assertEqual(res.given('int1'), False)
        self.assertEqual(res['ciao'], '1')
        self.assertEqual(res['ciao2'], 1)
        self.assertEqual(res.ciao, '1')
        self.assertEqual(res.ciao2, 1)
        self.assertEqual(res.floats, [1.2, 2.3])
        self.assertEqual(res.ints, [1, 2, 3])