コード例 #1
0
    def test_parse_one_level(self):
        data = """\
... one
    # Comment on two
... two
*** line with some data
... three
"""
        parser = TreeParser(data)
        assert parser._text_generated() == """\
... one
    # Comment on two
... two
*** line with some data
... three
... ~end-marker~
"""
        assert parser.as_nested_list == ['one', '# Comment on two', 'two',
                                         'line with some data', 'three']
        ##pprint(parser.as_json)
        assert parser.as_json == {
            '_leaves': ['one', 'three', 'two', 'z-2cf54f23'],
            '_root': {'members': ['one', 'two', 'z-2cf54f23', 'three']},
            '_tree': [u'... one',
                      '    # Comment on two',
                      '... two',
                      '*** line with some data',
                      '... three'],
            'one': {'groups': ['_root']},
            'three': {'groups': ['_root']},
            'two': {'comments': ['# Comment on two'], 'groups': ['_root']},
            'z-2cf54f23': {'data': 'line with some data', 'groups': ['_root']}
        }
        assert parser.as_tree == data
コード例 #2
0
    def test_parse_one_level_initial_indent(self):
        # Blank line is ignored
        data = """\
    ... one
    ... two

    ... three
"""
        parser = TreeParser(data)
        assert parser._text_generated() == """\
    ... one
    ... two
    ... three
    ... ~end-marker~
"""
        assert parser.as_nested_list == ['one', 'two', 'three']
        # No need to keep the initial indent in the data
        assert parser.as_tree == """\
... one
... two
... three
"""
        print("+++++++++++++++++++++++++++++++++++++++++++++++++")
        pprint(parser.as_json)
        assert parser.as_json == {
            u'_leaves': [u'one', u'three', u'two'],
            u'_root': {u'members': [u'one', u'two', u'three']},
            u'_tree': [u'... one', u'... two', u'... three'],
            u'one': {u'groups': [u'_root']},
            u'three': {u'groups': [u'_root']},
            u'two': {u'groups': [u'_root']}
        }
コード例 #3
0
    def test_parse_to_nested_list(self):
        data = """\
... one
    ... alpha
... two
    ... beta
        ... beta_01
    ... gamma
... three
    ... delta
... four
"""
        parser = TreeParser(data)
        assert parser._text_generated() == """\
... one
    ... alpha
... two
    ... beta
        ... beta_01
    ... gamma
... three
    ... delta
... four
... ~end-marker~
"""
        assert parser.as_nested_list == [
            'one', ['alpha'],
            'two', ['beta', ['beta_01'], 'gamma'],
            'three', ['delta'],
            'four'
        ]
        ##print("+=" * 35)
        ##pprint(parser.as_json)
        assert parser.as_json == {
            '_leaves': ['alpha', 'beta_01', 'delta', 'four', 'gamma'],
            '_root': {'members': ['one', 'two', 'three', 'four']},
            '_tree': ['... one',
                      '    ... alpha',
                      '... two',
                      '    ... beta',
                      '        ... beta_01',
                      '    ... gamma',
                      '... three',
                      '    ... delta',
                      '... four'],
            'alpha': {'groups': ['one']},
            'beta': {'groups': ['two'], 'members': ['beta_01']},
            'beta_01': {'groups': ['beta']},
            'delta': {'groups': ['three']},
            'four': {'groups': ['_root']},
            'gamma': {'groups': ['two']},
            'one': {'groups': ['_root'], 'members': ['alpha']},
            'three': {'groups': ['_root'], 'members': ['delta']},
            'two': {'groups': ['_root'], 'members': ['beta', 'gamma']},
        }
        assert parser.as_tree == data
コード例 #4
0
    def load(cls, data, data_format='tree'):
        """Load riders, ponies and latches from text file in the chosen
        format. Acceptable formats:
            json
            tree
            # bson
            # xml
        """
        cls.validate_format(data_format)

        if data_format == 'tree':
            parser = TreeParser(data)
            json_dict = parser.to_json()
            meta_data_list = json_dict['doc']['members']
            meta_data_dict = {} # TO-DO Dictionary comprehension
            for meta_data in meta_data_list:
                key, value = meta_data.split(':', 1)
                meta_data_dict[key.strip()] = value.strip()
        elif data_format == 'json':
            pass
            #try:
                #data2 = json.loads(data + '')  # OK if data is a string
            #except TypeError:
                #data2 = data.copy()  # data is already a dictionary;
                                     ## copy for safety
            # data is now a Python dict of simple data types

        # TO-DO Organisation missing should be an error -- remove default
        ##competition = cls(org_id=data.get('org_id', 'default_org_id2'),
                          ##name=data['name'], created=data.get('created'))
        competition = cls(**meta_data_dict)

        # Add competitors and latches


        # ==================================


        for creator_class in [Rider, Pony, Action]:
            ##for name, adict in data[creator_class.collection_name].iteritems():
                ##competition.add(creator_class(name, **adict))
            for adict in data[creator_class.collection_name]:
                competition.add(creator_class(**adict))
        for latch_key in data[Latch.collection_name]:
            if latch_key != LATCH_DENY_ALL:
                competition.add(Latch(latch_key))
        # Calculate members and validate integrity of graph
        competition.validate()
        return competition
コード例 #5
0
    def test_parse_one_level_tree_from_json(self):
        json_str = """\
{
  "_leaves": ["one", "three", "two"],
  "_root": {"members": ["one", "two", "three"]},
  "_tree": ["... one",
            "... two",
            "... three"],
  "one": {"groups": ["_root"]},
  "three": {"groups": ["_root"]},
  "two": {"groups": ["_root"]}
}
"""
        parser = TreeParser(json_str)

        assert parser.to_json() == json.loads(json_str)
        return  # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
コード例 #6
0
    def test_parse_one_level_with_comments(self):
        # Check nested list, and that tree formed is identical
        data = """\
    # Comment on one
... one
... two # Inline comment on two
    # Comment on three
... three
"""
        parser = TreeParser(data)
        assert parser._text_generated() == """\
    # Comment on one
... one
    # Inline comment on two
... two
    # Comment on three
... three
... ~end-marker~
"""
        assert parser.as_nested_list == ['# Comment on one', 'one',
                                         '# Inline comment on two', 'two',
                                         '# Comment on three', 'three']

        ##pprint(parser.as_json)
        assert parser.as_json == {
            '_leaves': ['one', 'three', 'two'],
            '_root': {'members': ['one', 'two', 'three']},
            '_tree': ['    # Comment on one',
                      '... one',
                      '    # Inline comment on two',
                      '... two',
                      '    # Comment on three',
                      '... three'],
            'one': {'comments': ['# Comment on one'], 'groups': ['_root']},
            'three': {'comments': ['# Comment on three'], 'groups': ['_root']},
            'two': {'comments': ['# Inline comment on two'],
                    'groups': ['_root']}}
        assert parser.as_tree == """\