Esempio n. 1
0
 def test_str_to_list(self):
     for string, output in [
         ('foo', ['foo']),
         ('"f\'oo"  \'b"ar\'', ["f'oo", 'b"ar']),
         ('', []),
         (' ', []),
         ('\\"hi ', ['"hi']),
         ('\'"hi\'', ['"hi']),
         ('"\\"hi"', ['"hi']),
         ]:
         self.assertEqual(basics.str_to_list(string), output)
     for string in ['"', "'foo", 'ba"r', 'baz"']:
         self.assertRaises(
             errors.QuoteInterpretationError, basics.str_to_list, string)
     # make sure this explodes instead of returning something
     # confusing so we explode much later
     self.assertRaises(TypeError, basics.str_to_list, ['no', 'string'])
Esempio n. 2
0
 def test_str_to_list(self):
     for string, output in [
         ('foo', ['foo']),
         ('"f\'oo"  \'b"ar\'', ["f'oo", 'b"ar']),
         ('', []),
         (' ', []),
         ('\\"hi ', ['"hi']),
         ('\'"hi\'', ['"hi']),
         ('"\\"hi"', ['"hi']),
     ]:
         self.assertEqual(basics.str_to_list(string), output)
     for string in ['"', "'foo", 'ba"r', 'baz"']:
         self.assertRaises(errors.QuoteInterpretationError,
                           basics.str_to_list, string)
     # make sure this explodes instead of returning something
     # confusing so we explode much later
     self.assertRaises(TypeError, basics.str_to_list, ['no', 'string'])
Esempio n. 3
0
 def render_value(self, central, name, arg_type):
     value = self.section[name]
     if arg_type == 'callable':
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         value = value[0]
         if not isinstance(value, basestring):
             raise errors.ConfigurationError(
                 'need a callable, not a section')
         try:
             value = modules.load_attribute(value)
         except modules.FailedImport:
             raise errors.ConfigurationError('cannot import %r' % (value,))
         if not callable(value):
             raise errors.ConfigurationError('%r is not callable' % value)
         return value
     elif arg_type.startswith('ref:'):
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         value = value[0]
         if isinstance(value, basestring):
             # it's a section ref
             return basics.LazyNamedSectionRef(central, arg_type, value)
         else:
             # it's an anonymous inline section
             return basics.LazyUnnamedSectionRef(central, arg_type,
                                                 ConfigSection(value))
     elif arg_type.startswith('refs:'):
         result = []
         for ref in value:
             if isinstance(ref, basestring):
                 # it's a section ref
                 result.append(basics.LazyNamedSectionRef(
                         central, arg_type, ref))
             else:
                 # it's an anonymous inline section
                 result.append(basics.LazyUnnamedSectionRef(
                         central, arg_type, ConfigSection(ref)))
         return None, result, None
     elif arg_type == 'list':
         if not isinstance(value, basestring):
             # sequence
             value = ' '.join(value)
         return None, basics.str_to_list(value), None
     elif arg_type == 'repr':
         if len(value) == 1:
             value = value[0]
             if isinstance(value, basestring):
                 return 'str', value
             return 'ref', ConfigSection(value)
         if all(isinstance(v, basestring) for v in value):
             return 'list', list(value)
         result = []
         for v in value:
             if not isinstance(v, basestring):
                 v = ConfigSection(v)
             result.append(v)
         return 'refs', result
     else:
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         if not isinstance(value[0], basestring):
             raise errors.ConfigurationError(
                 '%r should be a string' % value)
         if arg_type == 'str':
             return [None, basics.str_to_str(value[0]), None]
         elif arg_type == 'bool':
             return basics.str_to_bool(value[0])
         else:
             raise errors.ConfigurationError(
                 'unsupported type %r' % (arg_type,))
Esempio n. 4
0
 def render_value(self, central, name, arg_type):
     value = self.section[name]
     if arg_type == 'callable':
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         value = value[0]
         if not isinstance(value, basestring):
             raise errors.ConfigurationError(
                 'need a callable, not a section')
         try:
             value = modules.load_attribute(value)
         except modules.FailedImport:
             raise errors.ConfigurationError('cannot import %r' % (value,))
         if not callable(value):
             raise errors.ConfigurationError('%r is not callable' % value)
         return value
     elif arg_type.startswith('ref:'):
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         value = value[0]
         if isinstance(value, basestring):
             # it's a section ref
             return basics.LazyNamedSectionRef(central, arg_type, value)
         else:
             # it's an anonymous inline section
             return basics.LazyUnnamedSectionRef(central, arg_type,
                                                 ConfigSection(value))
     elif arg_type.startswith('refs:'):
         result = []
         for ref in value:
             if isinstance(ref, basestring):
                 # it's a section ref
                 result.append(basics.LazyNamedSectionRef(
                         central, arg_type, ref))
             else:
                 # it's an anonymous inline section
                 result.append(basics.LazyUnnamedSectionRef(
                         central, arg_type, ConfigSection(ref)))
         return None, result, None
     elif arg_type == 'list':
         if not isinstance(value, basestring):
             # sequence
             value = ' '.join(value)
         return None, basics.str_to_list(value), None
     elif arg_type == 'repr':
         if len(value) == 1:
             value = value[0]
             if isinstance(value, basestring):
                 return 'str', value
             return 'ref', ConfigSection(value)
         if all(isinstance(v, basestring) for v in value):
             return 'list', list(value)
         result = []
         for v in value:
             if not isinstance(v, basestring):
                 v = ConfigSection(v)
             result.append(v)
         return 'refs', result
     else:
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         if not isinstance(value[0], basestring):
             raise errors.ConfigurationError(
                 '%r should be a string' % value)
         if arg_type == 'str':
             return [None, basics.str_to_str(value[0]), None]
         elif arg_type == 'bool':
             return basics.str_to_bool(value[0])
         else:
             raise errors.ConfigurationError(
                 'unsupported type %r' % (arg_type,))