python_arango_ogm.db.pao_model_discovery

 1import importlib, inspect
 2import json
 3import os
 4from typing import Sequence, Type, Dict
 5
 6from python_arango_ogm.db.pao_model import PAOModel
 7
 8class PAOModelDiscovery:
 9    def __init__(self):
10        app_package = os.getenv('PAO_APP_PACKAGE')
11        if not app_package:
12            raise RuntimeError("PAO_APP_PACKAGE must be defined in the environment (or a .env.test file)")
13        self.models_module_name = f"{app_package}.models"
14
15    def is_valid_model(self, model: Type) -> bool:
16        return (not model is PAOModel) and issubclass(model, PAOModel)
17
18    def discover(self) -> Dict[str, type[PAOModel]]:
19        print("IMPORTING MODELS MODULE FROM", self.models_module_name)
20        module = importlib.import_module(self.models_module_name)
21        models = [cls for _, cls in inspect.getmembers(module, inspect.isclass) if self.is_valid_model(cls)]
22
23        # Sort by line number; this is important as we should
24        # generate migrations in the defined order:
25        sorted_models = sorted(models, key=self.__get_model_line_num);
26
27        model_dict = {m.__name__: m for m in sorted_models}
28
29        print('MODEL_DICT:', model_dict)
30        return model_dict
31
32    def __get_model_line_num(self, model):
33        _, line_num = inspect.getsourcelines(model)
34        return line_num
class PAOModelDiscovery:
 9class PAOModelDiscovery:
10    def __init__(self):
11        app_package = os.getenv('PAO_APP_PACKAGE')
12        if not app_package:
13            raise RuntimeError("PAO_APP_PACKAGE must be defined in the environment (or a .env.test file)")
14        self.models_module_name = f"{app_package}.models"
15
16    def is_valid_model(self, model: Type) -> bool:
17        return (not model is PAOModel) and issubclass(model, PAOModel)
18
19    def discover(self) -> Dict[str, type[PAOModel]]:
20        print("IMPORTING MODELS MODULE FROM", self.models_module_name)
21        module = importlib.import_module(self.models_module_name)
22        models = [cls for _, cls in inspect.getmembers(module, inspect.isclass) if self.is_valid_model(cls)]
23
24        # Sort by line number; this is important as we should
25        # generate migrations in the defined order:
26        sorted_models = sorted(models, key=self.__get_model_line_num);
27
28        model_dict = {m.__name__: m for m in sorted_models}
29
30        print('MODEL_DICT:', model_dict)
31        return model_dict
32
33    def __get_model_line_num(self, model):
34        _, line_num = inspect.getsourcelines(model)
35        return line_num
models_module_name
def is_valid_model(self, model: Type) -> bool:
16    def is_valid_model(self, model: Type) -> bool:
17        return (not model is PAOModel) and issubclass(model, PAOModel)
def discover(self) -> Dict[str, type[python_arango_ogm.db.pao_model.PAOModel]]:
19    def discover(self) -> Dict[str, type[PAOModel]]:
20        print("IMPORTING MODELS MODULE FROM", self.models_module_name)
21        module = importlib.import_module(self.models_module_name)
22        models = [cls for _, cls in inspect.getmembers(module, inspect.isclass) if self.is_valid_model(cls)]
23
24        # Sort by line number; this is important as we should
25        # generate migrations in the defined order:
26        sorted_models = sorted(models, key=self.__get_model_line_num);
27
28        model_dict = {m.__name__: m for m in sorted_models}
29
30        print('MODEL_DICT:', model_dict)
31        return model_dict