Example #1
0
 def test_str_to_str(self):
     for string, output in [
         ('\t ', ''),
         (' foo ', 'foo'),
         (' " foo " ', ' foo '),
         ('\t"', '"'),
         ('\nfoo\t\n bar\t', 'foo   bar'),
         ('"a"', 'a'),
         ("'a'", "a"),
         ("'a", "'a"),
         ('"a', '"a'),
         ]:
         self.assertEqual(basics.str_to_str(string), output)
Example #2
0
 def test_str_to_str(self):
     for string, output in [
         ('\t ', ''),
         (' foo ', 'foo'),
         (' " foo " ', ' foo '),
         ('\t"', '"'),
         ('\nfoo\t\n bar\t', 'foo   bar'),
         ('"a"', 'a'),
         ("'a'", "a"),
         ("'a", "'a"),
         ('"a', '"a'),
     ]:
         self.assertEqual(basics.str_to_str(string), output)
Example #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,))
Example #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,))