Beispiel #1
0
    def _parse(lines):
        """Parse config file format into a section list"""
        begin = re.compile(r'([\w-]+)[ \t]*{') # WORD {
        end = re.compile(r'}')                 # }
        attr = re.compile(r'([\w-]+)[ \t]*:[ \t]*(.+)') # WORD1: VALUE

        def sub(line):
            """Do substitutions to make line json-friendly"""
            line = line.split('#')[0].strip() # Strip comments
            line = re.sub(begin, r'["\1", {', line)
            line = re.sub(end, r'}],', line)
            line = re.sub(attr, r'"\1": "\2",', line)
            return line

        js_text = "[%s]"%("\n".join([sub(l) for l in lines]))
        spare_comma = re.compile(r',\s*([]}])') # Strip spare commas
        js_text = re.sub(spare_comma, r'\1', js_text)
        # Convert dictionary keys to camelCase
        sections = json.loads(js_text)
        for s in sections:
            s[0] = camelcase(s[0])
            s[1] = dict((camelcase(k), v) for k, v in s[1].iteritems())
            if s[0] == "address":   s[0] = "router.config.address"
            if s[0] == "linkRoute": s[0] = "router.config.linkRoute"
            if s[0] == "autoLink":  s[0] = "router.config.autoLink"
        return sections
Beispiel #2
0
 def transform_sections(sections):
     for s in sections:
         s[0] = camelcase(s[0])
         s[1] = dict((camelcase(k), v) for k, v in s[1].iteritems())
         if s[0] == "address":   s[0] = "router.config.address"
         if s[0] == "linkRoute": s[0] = "router.config.linkRoute"
         if s[0] == "autoLink":  s[0] = "router.config.autoLink"
Beispiel #3
0
    def _parse(lines):
        """Parse config file format into a section list"""
        begin = re.compile(r'([\w-]+)[ \t]*{')  # WORD {
        end = re.compile(r'}')  # }
        attr = re.compile(r'([\w-]+)[ \t]*:[ \t]*(.+)')  # WORD1: VALUE

        def sub(line):
            """Do substitutions to make line json-friendly"""
            line = line.split('#')[0].strip()  # Strip comments
            line = re.sub(begin, r'["\1", {', line)
            line = re.sub(end, r'}],', line)
            line = re.sub(attr, r'"\1": "\2",', line)
            return line

        js_text = "[%s]" % ("\n".join([sub(l) for l in lines]))
        spare_comma = re.compile(r',\s*([]}])')  # Strip spare commas
        js_text = re.sub(spare_comma, r'\1', js_text)
        # Convert dictionary keys to camelCase
        sections = json.loads(js_text)
        for s in sections:
            s[0] = camelcase(s[0])
            s[1] = dict((camelcase(k), v) for k, v in s[1].iteritems())
            if s[0] == "address": s[0] = "router.config.address"
            if s[0] == "linkRoute": s[0] = "router.config.linkRoute"
            if s[0] == "autoLink": s[0] = "router.config.autoLink"
        return sections
Beispiel #4
0
 def transform_sections(sections):
     for s in sections:
         s[0] = camelcase(s[0])
         s[1] = dict((camelcase(k), v) for k, v in s[1].iteritems())
         if s[0] == "address": s[0] = "router.config.address"
         if s[0] == "linkRoute": s[0] = "router.config.linkRoute"
         if s[0] == "autoLink": s[0] = "router.config.autoLink"
Beispiel #5
0
 def entity_class(self, entity_type):
     """Return the class that implements entity_type"""
     class_name = camelcase(entity_type.short_name, capital=True) + "Entity"
     entity_class = globals().get(class_name)
     if not entity_class:
         raise InternalServerErrorStatus("Can't find implementation '%s' for '%s'" % (class_name, entity_type.name))
     return entity_class
Beispiel #6
0
 def entity_class(self, entity_type):
     """Return the class that implements entity_type"""
     class_name = camelcase(entity_type.short_name, capital=True) + 'Entity'
     entity_class = globals().get(class_name)
     if not entity_class:
         raise InternalServerErrorStatus(
             "Can't find implementation '%s' for '%s'" % (class_name, entity_type.name))
     return entity_class
Beispiel #7
0
 def test_camelcase(self):
     self.assertEqual('', camelcase(''))
     self.assertEqual('foo', camelcase('foo'))
     self.assertEqual('Foo', camelcase('foo', capital=True))
     self.assertEqual('fooBar', camelcase('foo bar'))
     self.assertEqual('fooBar', camelcase('foo.bar'))
     self.assertEqual('fooBar', camelcase('foo-bar'))
     self.assertEqual('fooBar', camelcase('foo_bar'))
     self.assertEqual('fooBarBaz', camelcase('foo_bar.baz'))
     self.assertEqual('FooBarBaz', camelcase('foo_bar.baz', capital=True))
     self.assertEqual('fooBar', camelcase('fooBar'))
     self.assertEqual('FooBar', camelcase('fooBar', capital=True))
Beispiel #8
0
 def test_camelcase(self):
     self.assertEqual('', camelcase(''))
     self.assertEqual('foo', camelcase('foo'))
     self.assertEqual('Foo', camelcase('foo', capital=True))
     self.assertEqual('fooBar', camelcase('foo bar'))
     self.assertEqual('fooBar', camelcase('foo.bar'))
     self.assertEqual('fooBar', camelcase('foo-bar'))
     self.assertEqual('fooBar', camelcase('foo_bar'))
     self.assertEqual('fooBarBaz', camelcase('foo_bar.baz'))
     self.assertEqual('FooBarBaz', camelcase('foo_bar.baz', capital=True))
     self.assertEqual('fooBar', camelcase('fooBar'))
     self.assertEqual('FooBar', camelcase('fooBar', capital=True))