Changeset 130:67f18e72024d


Ignore:
Timestamp:
Sep 2, 2013, 3:03:25 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
20d152c11a9c4f093c1eb1ff883f6596fca93d5b
Message:

Handle corner cases around quotes in strings

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/tests/test_yamlish.py

    r129 r130  
    102102        self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
    103103
     104    def test_inline(self):
     105        self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]])
     106
    104107
    105108class TestFromPyYAML(TestRoundTrip):
  • nagslang/yamlish.py

    r129 r130  
    6767        if data in ('true', 'false', 'null'):
    6868            data = "'%s'" % data
     69        elif "'" in data:
     70            data = "'%s'" % data.replace("'", "''")
     71        elif data == '':
     72            data = "''"
    6973        return [' ' * indent + data]
    7074
     
    8892    _list_re = re.compile(r'^(-\s+)(.*)')
    8993    _dict_re = re.compile(r'^((?![{[])[^-:]+):\s?(.*)')
    90     _inline_list_re = re.compile(r"^([^',]+|''|'.+?[^'](?:'')*')(?:, (.*))?$")
     94    _inline_list_re = re.compile(r"^([^',]+|(?:'')+|'.+?[^'](?:'')*')(?:, (.*))?$")
    9195
    9296    def __init__(self):
     
    189193    def _parse_value(self, value):
    190194        if value.startswith("'") and value.endswith("'"):
    191             return value[1:-1]
     195            return value[1:-1].replace("''", "'")
    192196        if value.startswith('[') and value.endswith(']'):
    193197            value = value[1:-1]
     
    195199            while value:
    196200                m = self._inline_list_re.match(value)
    197                 assert m
     201                assert m, value
    198202                output.append(self._parse_value(m.group(1)))
    199203                value = m.group(2)
Note: See TracChangeset for help on using the changeset viewer.