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
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 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<