2024-07-17 21:30:12 +00:00
|
|
|
import importlib
|
|
|
|
|
|
|
|
|
|
|
|
class PseudoDB:
|
|
|
|
def __init__(self, datasource="default.data"):
|
|
|
|
self.datasource = datasource
|
|
|
|
self.data = self.load_data()
|
|
|
|
|
|
|
|
def load_data(self):
|
|
|
|
module_name = self.datasource.replace('.py', '')
|
|
|
|
try:
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
return module
|
|
|
|
except ImportError:
|
|
|
|
print(f"Data module '{module_name}' not found.")
|
|
|
|
return None
|
|
|
|
|
2024-07-17 21:59:13 +00:00
|
|
|
def get_collections(self):
|
2024-07-17 21:30:12 +00:00
|
|
|
# Check if default has been successfully loaded
|
|
|
|
if self.data is not None:
|
|
|
|
attr_names = []
|
|
|
|
for attr_name in dir(self.data):
|
|
|
|
if not attr_name.startswith('__'):
|
|
|
|
attr_names.append(attr_name)
|
|
|
|
return attr_names
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_keys(self, attr_name):
|
|
|
|
if self.data is not None:
|
|
|
|
collection = getattr(self.data, attr_name)
|
|
|
|
if isinstance(collection, dict):
|
|
|
|
return list(collection.keys())
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def get_values(self, attr_name, key):
|
|
|
|
if self.data is not None:
|
|
|
|
collection = getattr(self.data, attr_name)
|
|
|
|
if isinstance(collection, dict):
|
|
|
|
return list(collection[key])
|
|
|
|
else:
|
|
|
|
return collection
|
|
|
|
else:
|
|
|
|
return None
|