MCPcopy
hub / github.com/apache/caldera / BaseApiManager

Class BaseApiManager

app/api/v2/managers/base_api_manager.py:16–156  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14
15
16class BaseApiManager(BaseWorld):
17 def __init__(self, data_svc, file_svc, logger=None):
18 self._data_svc = data_svc
19 self._file_svc = file_svc
20 self._log = logger or self._create_default_logger()
21
22 @property
23 def log(self):
24 return self._log
25
26 def find_objects(self, ram_key: str, search: dict = None):
27 """Find objects matching the given criteria"""
28 for obj in self._data_svc.ram[ram_key]:
29 if not search or obj.match(search):
30 yield obj
31
32 def find_object(self, ram_key: str, search: dict = None):
33 for obj in self.find_objects(ram_key, search):
34 return obj
35
36 def find_and_dump_objects(self, ram_key: str, search: dict = None, sort: str = None, include: List[str] = None,
37 exclude: List[str] = None):
38 matched_objs = []
39 for obj in self.find_objects(ram_key, search):
40 dumped_obj = self.dump_object_with_filters(obj, include, exclude)
41 matched_objs.append(dumped_obj)
42 sorted_objs = sorted(matched_objs, key=lambda p: p.get(sort, 0))
43 if sorted_objs and sort in sorted_objs[0]:
44 return sorted(sorted_objs,
45 key=lambda x: 0 if x[sort] == self._data_svc.get_config(f"objects.{ram_key}.default") else 1)
46 return sorted_objs
47
48 @staticmethod
49 def dump_object_with_filters(obj: Any, include: List[str] = None, exclude: List[str] = None) -> dict:
50 dumped = obj.display
51 if include:
52 exclude_attributes = list(set(dumped.keys()) - set(include))
53 exclude = set(exclude + exclude_attributes) if exclude else exclude_attributes
54 if exclude:
55 for exclude_attribute in exclude:
56 dumped.pop(exclude_attribute, None)
57 return dumped
58
59 def create_object_from_schema(self, schema: SchemaMeta, data: dict, access: BaseWorld.Access):
60 obj_schema = schema()
61 obj = obj_schema.load(data)
62 obj.access = self._get_allowed_from_access(access)
63 return obj.store(self._data_svc.ram)
64
65 async def create_on_disk_object(self, data: dict, access: dict, ram_key: str, id_property: str, obj_class: type):
66 obj_id = data.get(id_property) or str(uuid.uuid4())
67 data[id_property] = obj_id
68
69 file_path = await self._get_new_object_file_path(data[id_property], ram_key)
70 allowed = self._get_allowed_from_access(access)
71 await self._save_and_reload_object(file_path, data, obj_class, allowed)
72 return next(self.find_objects(ram_key, {id_property: obj_id}))
73

Callers 15

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
test_find_objectsFunction · 0.90
test_find_objectFunction · 0.90
test_dumpFunction · 0.90
test_dump_with_excludeFunction · 0.90
test_dump_with_includeFunction · 0.90

Calls

no outgoing calls