def setUp(self): super(DictPathTestCase, self).setUp() self.data = { 'request_id': 'abc123', 'object': { 'id': 7, 'name': 'test', 'owner_ids': [ 3, 25, ], 'properties': { 'type': 'document', 'created': datetime.datetime(2013, 10, 18, 15, 16, 12), 'perms': [ {'who': 'owner', 'access': 'read'}, {'who': 'owner', 'access': 'edit'}, {'who': 'owner', 'access': 'delete'}, {'who': 'anyone', 'access': 'read'}, ] } }, 'meta': { 'next': '/?page=3', 'prev': '/?page=1', 'count': 64, } } self.path = DictPath(self.data)
def post_process_results(self, raw_results): dictpath = DictPath(raw_results) for struct in self.resource.structures_to_use: for path in struct.possible_paths: possible_data = dictpath.find(path) if possible_data is None: continue # We've got a match! It's either a single structure itself or # a list of structures (or something crazy). if hasattr(possible_data, 'keys'): # It's a dict (hence, a single Structure). new_struct = struct() new_struct.full_populate(possible_data) dictpath.store(path, new_struct) elif hasattr(possible_data, 'append'): # It's a list. revised_list = [] for item in possible_data: new_struct = struct() new_struct.full_populate(item) revised_list.append(new_struct) dictpath.store(path, revised_list) else: # No clue what's here. # TODO: Perhaps raise an exception, since we have found # data but don't know how to handle it? pass # Because we had a reference to it, this should be updated if any # structures were found. return raw_results