MCPcopy Create free account
hub / github.com/GerevAI/gerev / DataSourceContext

Class DataSourceContext

app/data_source/api/context.py:26–151  ·  view source on GitHub ↗

This class is responsible for loading data sources and caching them. It dynamically loads data source types from the data_source/sources directory. It loads data sources from the database and caches them.

Source from the content-addressed store, hash-verified

24
25
26class DataSourceContext:
27 """
28 This class is responsible for loading data sources and caching them.
29 It dynamically loads data source types from the data_source/sources directory.
30 It loads data sources from the database and caches them.
31 """
32 _initialized = False
33 _data_source_cache: Dict[int, CachedDataSource] = {}
34 _data_source_classes: Dict[str, BaseDataSource] = {}
35
36 @classmethod
37 def get_data_source_instance(cls, data_source_id: int) -> BaseDataSource:
38 if not cls._initialized:
39 cls.init()
40 cls._initialized = True
41
42 return cls._data_source_cache[data_source_id].instance
43
44 @classmethod
45 def get_data_source_class(cls, data_source_name: str) -> BaseDataSource:
46 if not cls._initialized:
47 cls.init()
48 cls._initialized = True
49
50 return cls._data_source_classes[data_source_name]
51
52 @classmethod
53 def get_data_source_classes(cls) -> Dict[str, BaseDataSource]:
54 if not cls._initialized:
55 cls.init()
56 cls._initialized = True
57
58 return cls._data_source_classes
59
60 @classmethod
61 async def create_data_source(cls, name: str, config: dict) -> BaseDataSource:
62 async with async_session() as session:
63 data_source_type = await session.execute(
64 select(DataSourceType).filter_by(name=name)
65 )
66 data_source_type = data_source_type.scalar_one_or_none()
67 if data_source_type is None:
68 raise KnownException(message=f"Data source type {name} does not exist")
69
70 data_source_class = DynamicLoader.get_data_source_class(name)
71 logger.info(f"validating config for data source {name}")
72 await data_source_class.validate_config(config)
73 config_str = json.dumps(config)
74
75 data_source_row = DataSource(type_id=data_source_type.id, config=config_str, created_at=get_utc_time_now())
76 session.add(data_source_row)
77 await session.commit()
78
79 data_source = data_source_class(config=config, data_source_id=data_source_row.id)
80 cls._data_source_cache[data_source_row.id] = CachedDataSource(indexed_docs=0, failed_tasks=0,
81 instance=data_source)
82
83 return data_source

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected