Skip to content

Core Classes

illuminate.manager.assistant.Assistant

Bases: IAssistant

Assistant class, creates objects from project files.

Used by Manager class in its static methods and cli start function to initialize Manager.

Source code in illuminate/manager/assistant.py
class Assistant(IAssistant):
    """
    Assistant class, creates objects from project files.

    Used by Manager class in its static methods and cli start function to
    initialize Manager.
    """

    @staticmethod
    def provide_alembic_config(
        path: str, selector: str, url: Optional[str] = None
    ) -> Config:
        """
        Creates Alembic's configuration object.

        :param path: Migration directory path
        :param selector: Database name in settings.py module DB attribute
        :param url: SQLAlchemy Database URL
        :return: Alembic configuration object
        """
        if not url:
            url = Assistant._provide_db_url(selector)
        config = Config()
        config.set_main_option(
            "script_location", os.path.join(path, "migrations")
        )
        config.set_main_option("sqlalchemy.url", url)
        return config

    @staticmethod
    def provide_alembic_operations(
        selector: str, url: Optional[str] = None
    ) -> Operations:
        """
        Creates Alembic's operations object.
        NOTE: Currently unused after switching to SQLAlchemy 2.0.31. Alembic's
        Operations.bulk_insert is not working at the time of this note
        creation. Still, bulk_insert is considered the proper way to populate
        tables and should be used again when this issue is resolved.

        :param selector: Database name in settings.py module DB attribute
        :param url: SQLAlchemy Database URL
        :return: Alembic operations object
        """
        if not url:
            url = Assistant._provide_db_url(selector)
        engine = create_engine(url)
        context = MigrationContext.configure(engine.connect())
        return Operations(context)

    @staticmethod
    def provide_context(
        sessions: bool = True,
        _filter: Optional[tuple[str]] = None,
    ) -> dict[
        str,
        Union[
            dict[str, dict[str, Type[AsyncSession]]],
            str,
            list[Union[Type[Observer], Type[Adapter]]],
            ModuleType,
        ],
    ]:
        """
        Creates Manager's constructor kwargs.

        :param sessions: Sessions option
        :param _filter: Optional tuple of Observer's names or class names
        :return: Manager's constractor parameters
        :raises BasicManagerException:
        """
        settings = Assistant._provide_settings()
        context = {
            "adapters": [],
            "name": settings.NAME,
            "observers": [],
            "path": os.getcwd(),
            "settings": settings,
        }
        if sessions:
            context["sessions"] = Assistant._provide_sessions()

        for folder in ("adapters", "observers"):
            directory = os.path.join(os.getcwd(), folder)
            files = [
                f
                for f in next(os.walk(directory))[2]
                if f.endswith(".py") and not f.startswith("__init__")
            ]
            for file in files:
                _module = f"{folder}.{file}"
                spec = importlib.util.spec_from_file_location(
                    _module, os.path.join(directory, file)
                )
                module = importlib.util.module_from_spec(spec)  # type: ignore
                sys.modules[_module] = module
                spec.loader.exec_module(module)  # type: ignore

                for name, cls in inspect.getmembers(module, inspect.isclass):
                    proper_class = name.startswith(folder.capitalize()[:-1])
                    basic_import = len(name) == len(folder[:-1])
                    if proper_class and not basic_import:
                        context[folder].append(cls)

        if _filter:
            context["observers"] = list(
                filter(
                    lambda x: x.NAME in _filter  # type: ignore
                    or x.__name__ in _filter,
                    context["observers"],
                )
            )

        if not context["observers"]:
            raise BasicManagerException(
                "No observers found or left after filtering"
            )

        return context

    @staticmethod
    def provide_models() -> list[object]:
        """
        Gathers project models.

        :return: Models list
        """
        settings = Assistant._provide_settings()
        return [locate(i) for i in settings.MODELS]

    @staticmethod
    def _provide_db_url(selector: str, _async: bool = False) -> str:
        """
        Creates database URL.

        :param selector: Database name in settings.py module DB attribute
        :param _async: Async URL flag
        :return: Database URL string
        :raises BasicManagerException:
        """
        settings = Assistant._provide_settings()
        try:
            db = settings.DB[selector]
        except KeyError:
            raise BasicManagerException(
                f"Database {selector} is not defined in settings.py"
            )
        if not db.get("name"):
            db["name"] = settings.NAME
        if _async:
            async_drivers = {
                "mysql": "asyncmy",
                "postgresql": "asyncpg",
            }
            driver = async_drivers[db["type"]]
            return "{type}+{driver}://{user}:{pass}@{host}/{name}".format(
                driver=driver, **db
            )

        return "{type}://{user}:{pass}@{host}/{name}".format(**db)

    @staticmethod
    def _provide_sessions() -> dict[
        str, Union[Type[AsyncSession], InfluxDBClient]
    ]:
        """
        Creates a dictionary of database sessions.

        :return: Database sessions dictionary
        """
        _sessions: dict = {}
        settings = Assistant._provide_settings()
        logger.opt(colors=True).info(
            f"Number of expected db connections: "
            f"<yellow>{len(settings.DB)}</yellow>"
        )
        for db in settings.DB:
            _type = settings.DB[db]["type"]
            if _type in SUPPORTED_NOSQL_DATABASES:
                session = Assistant.__provide_nosql_sessions(db, settings)
                _sessions.update({db: session})
            elif _type in SUPPORTED_SQL_DATABASES:
                session = Assistant.__provide_sql_sessions(db, settings)
                _sessions.update({db: session})
            else:
                logger.warning(f"Database type {_type} is not supported")

        return _sessions

    @staticmethod
    def _provide_settings() -> ModuleType:
        """
        Imports project's settings.py module and returns it.

        :return: Project's settings.py module
        :raises BasicManagerException:
        """
        try:
            import settings  # type: ignore

            return settings
        except ImportError:
            raise BasicManagerException(
                "Framework did not found settings.py in the current directory"
            )

    @staticmethod
    def __log_database_connection(db: str, settings: ModuleType) -> None:
        """
        Log database connection.

        :param db: database name from settings.py module
        :param settings: settings.py module
        :return: None
        """
        host = settings.DB[db]["host"]
        port = settings.DB[db]["port"]
        logger.opt(colors=True).info(
            f"Adding session with <yellow>{db}</yellow> at "
            f"<magenta>{host}:{port}</magenta> to context"
        )

    @staticmethod
    def __provide_nosql_sessions(
        db: str, settings: ModuleType
    ) -> InfluxDBClient:
        """
        Provides NoSQL database session.

        :param db: database name from settings.py module
        :param settings: settings.py module
        :return: InfluxDBClient object
        """
        Assistant.__log_database_connection(db, settings)
        if settings.DB[db]["type"] == "influxdb":
            return InfluxDBClient(
                host=settings.DB[db]["host"],
                port=settings.DB[db]["port"],
                db=settings.DB[db].get("name", settings.NAME),
                username=settings.DB[db]["user"],
                password=settings.DB[db]["pass"],
            )

    @staticmethod
    def __provide_sql_sessions(
        db: str, settings: ModuleType
    ) -> sessionmaker[AsyncSession]:
        """
        Provides SQL database session.

        :param db: database name from settings.py module
        :param settings: settings.py module
        :return: AsyncSession created with session maker
        """
        Assistant.__log_database_connection(db, settings)
        return sessionmaker(
            create_async_engine(Assistant._provide_db_url(db, _async=True)),
            class_=AsyncSession,
            expire_on_commit=False,
        )

__log_database_connection(db: str, settings: ModuleType) -> None staticmethod

Log database connection.

PARAMETER DESCRIPTION
db

database name from settings.py module

TYPE: str

settings

settings.py module

TYPE: ModuleType

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/assistant.py
@staticmethod
def __log_database_connection(db: str, settings: ModuleType) -> None:
    """
    Log database connection.

    :param db: database name from settings.py module
    :param settings: settings.py module
    :return: None
    """
    host = settings.DB[db]["host"]
    port = settings.DB[db]["port"]
    logger.opt(colors=True).info(
        f"Adding session with <yellow>{db}</yellow> at "
        f"<magenta>{host}:{port}</magenta> to context"
    )

__provide_nosql_sessions(db: str, settings: ModuleType) -> InfluxDBClient staticmethod

Provides NoSQL database session.

PARAMETER DESCRIPTION
db

database name from settings.py module

TYPE: str

settings

settings.py module

TYPE: ModuleType

RETURNS DESCRIPTION
InfluxDBClient

InfluxDBClient object

Source code in illuminate/manager/assistant.py
@staticmethod
def __provide_nosql_sessions(
    db: str, settings: ModuleType
) -> InfluxDBClient:
    """
    Provides NoSQL database session.

    :param db: database name from settings.py module
    :param settings: settings.py module
    :return: InfluxDBClient object
    """
    Assistant.__log_database_connection(db, settings)
    if settings.DB[db]["type"] == "influxdb":
        return InfluxDBClient(
            host=settings.DB[db]["host"],
            port=settings.DB[db]["port"],
            db=settings.DB[db].get("name", settings.NAME),
            username=settings.DB[db]["user"],
            password=settings.DB[db]["pass"],
        )

__provide_sql_sessions(db: str, settings: ModuleType) -> sessionmaker[AsyncSession] staticmethod

Provides SQL database session.

PARAMETER DESCRIPTION
db

database name from settings.py module

TYPE: str

settings

settings.py module

TYPE: ModuleType

RETURNS DESCRIPTION
sessionmaker[AsyncSession]

AsyncSession created with session maker

Source code in illuminate/manager/assistant.py
@staticmethod
def __provide_sql_sessions(
    db: str, settings: ModuleType
) -> sessionmaker[AsyncSession]:
    """
    Provides SQL database session.

    :param db: database name from settings.py module
    :param settings: settings.py module
    :return: AsyncSession created with session maker
    """
    Assistant.__log_database_connection(db, settings)
    return sessionmaker(
        create_async_engine(Assistant._provide_db_url(db, _async=True)),
        class_=AsyncSession,
        expire_on_commit=False,
    )

provide_alembic_config(path: str, selector: str, url: Optional[str] = None) -> Config staticmethod

Creates Alembic's configuration object.

PARAMETER DESCRIPTION
path

Migration directory path

TYPE: str

selector

Database name in settings.py module DB attribute

TYPE: str

url

SQLAlchemy Database URL

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Config

Alembic configuration object

Source code in illuminate/manager/assistant.py
@staticmethod
def provide_alembic_config(
    path: str, selector: str, url: Optional[str] = None
) -> Config:
    """
    Creates Alembic's configuration object.

    :param path: Migration directory path
    :param selector: Database name in settings.py module DB attribute
    :param url: SQLAlchemy Database URL
    :return: Alembic configuration object
    """
    if not url:
        url = Assistant._provide_db_url(selector)
    config = Config()
    config.set_main_option(
        "script_location", os.path.join(path, "migrations")
    )
    config.set_main_option("sqlalchemy.url", url)
    return config

provide_alembic_operations(selector: str, url: Optional[str] = None) -> Operations staticmethod

Creates Alembic's operations object. NOTE: Currently unused after switching to SQLAlchemy 2.0.31. Alembic's Operations.bulk_insert is not working at the time of this note creation. Still, bulk_insert is considered the proper way to populate tables and should be used again when this issue is resolved.

PARAMETER DESCRIPTION
selector

Database name in settings.py module DB attribute

TYPE: str

url

SQLAlchemy Database URL

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Operations

Alembic operations object

Source code in illuminate/manager/assistant.py
@staticmethod
def provide_alembic_operations(
    selector: str, url: Optional[str] = None
) -> Operations:
    """
    Creates Alembic's operations object.
    NOTE: Currently unused after switching to SQLAlchemy 2.0.31. Alembic's
    Operations.bulk_insert is not working at the time of this note
    creation. Still, bulk_insert is considered the proper way to populate
    tables and should be used again when this issue is resolved.

    :param selector: Database name in settings.py module DB attribute
    :param url: SQLAlchemy Database URL
    :return: Alembic operations object
    """
    if not url:
        url = Assistant._provide_db_url(selector)
    engine = create_engine(url)
    context = MigrationContext.configure(engine.connect())
    return Operations(context)

provide_context(sessions: bool = True, _filter: Optional[tuple[str]] = None) -> dict[str, Union[dict[str, dict[str, Type[AsyncSession]]], str, list[Union[Type[Observer], Type[Adapter]]], ModuleType]] staticmethod

Creates Manager's constructor kwargs.

PARAMETER DESCRIPTION
sessions

Sessions option

TYPE: bool DEFAULT: True

_filter

Optional tuple of Observer's names or class names

TYPE: Optional[tuple[str]] DEFAULT: None

RETURNS DESCRIPTION
dict[str, Union[dict[str, dict[str, Type[AsyncSession]]], str, list[Union[Type[Observer], Type[Adapter]]], ModuleType]]

Manager's constractor parameters

RAISES DESCRIPTION
BasicManagerException
Source code in illuminate/manager/assistant.py
@staticmethod
def provide_context(
    sessions: bool = True,
    _filter: Optional[tuple[str]] = None,
) -> dict[
    str,
    Union[
        dict[str, dict[str, Type[AsyncSession]]],
        str,
        list[Union[Type[Observer], Type[Adapter]]],
        ModuleType,
    ],
]:
    """
    Creates Manager's constructor kwargs.

    :param sessions: Sessions option
    :param _filter: Optional tuple of Observer's names or class names
    :return: Manager's constractor parameters
    :raises BasicManagerException:
    """
    settings = Assistant._provide_settings()
    context = {
        "adapters": [],
        "name": settings.NAME,
        "observers": [],
        "path": os.getcwd(),
        "settings": settings,
    }
    if sessions:
        context["sessions"] = Assistant._provide_sessions()

    for folder in ("adapters", "observers"):
        directory = os.path.join(os.getcwd(), folder)
        files = [
            f
            for f in next(os.walk(directory))[2]
            if f.endswith(".py") and not f.startswith("__init__")
        ]
        for file in files:
            _module = f"{folder}.{file}"
            spec = importlib.util.spec_from_file_location(
                _module, os.path.join(directory, file)
            )
            module = importlib.util.module_from_spec(spec)  # type: ignore
            sys.modules[_module] = module
            spec.loader.exec_module(module)  # type: ignore

            for name, cls in inspect.getmembers(module, inspect.isclass):
                proper_class = name.startswith(folder.capitalize()[:-1])
                basic_import = len(name) == len(folder[:-1])
                if proper_class and not basic_import:
                    context[folder].append(cls)

    if _filter:
        context["observers"] = list(
            filter(
                lambda x: x.NAME in _filter  # type: ignore
                or x.__name__ in _filter,
                context["observers"],
            )
        )

    if not context["observers"]:
        raise BasicManagerException(
            "No observers found or left after filtering"
        )

    return context

provide_models() -> list[object] staticmethod

Gathers project models.

RETURNS DESCRIPTION
list[object]

Models list

Source code in illuminate/manager/assistant.py
@staticmethod
def provide_models() -> list[object]:
    """
    Gathers project models.

    :return: Models list
    """
    settings = Assistant._provide_settings()
    return [locate(i) for i in settings.MODELS]

illuminate.manager.manager.Manager

Bases: IManager

Manager class, executes framework's cli commands.

All public methods correspond to cli commands. It should only be instantiated when 'illuminate observe start' command is used with kwargs provided by Assistant class.

Source code in illuminate/manager/manager.py
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
class Manager(IManager):
    """
    Manager class, executes framework's cli commands.

    All public methods correspond to cli commands. It should only be
    instantiated when 'illuminate observe start' command is used with kwargs
    provided by Assistant class.
    """

    def __init__(
        self,
        adapters: list[Type[Adapter]],
        name: str,
        observers: list[Type[Observer]],
        path: str,
        sessions: dict[str, Union[Type[AsyncSession], InfluxDBClient]],
        settings: ModuleType,
        *args,
        **kwargs,
    ):
        """
        Manager's __init__ method.

        :param adapters: List of Adapters found in project files
        :param name: Project's name
        :param observers: List of Observers found in project files after
        filtering
        :param path: Path to project files
        :param sessions: Database sessions
        :param settings: Project's settings.py module
        """
        self.adapters = adapters
        self.name = name
        self.observers = observers
        self.path = path
        self.sessions = sessions
        self.settings = settings
        self._adapters: list[Adapter] = []
        self._observers: list[Observer] = []
        self.__observe_queue: queues.Queue = queues.Queue()
        self.__adapt_queue: queues.Queue = queues.Queue()
        self.__export_queue: queues.Queue = queues.Queue()
        self.__exported: set = set()
        self.__not_observed: set = set()
        self.__observed: set = set()
        self.__observing: set = set()

    @property
    def exported(self) -> set:
        return self.__exported

    @property
    def not_observed(self) -> set:
        return self.__not_observed

    @property
    def observed(self) -> set:
        return self.__observed

    @staticmethod
    @adapt("populate")
    def db_populate(
        fixtures: tuple[str],
        selector: str,
        url: str,
    ) -> None:
        """
        Populates database with fixtures.

        :param fixtures: Tuple of fixture files
        :param selector: Database name in settings.py module
        :param url: SQLAlchemy Database URL
        :return: None
        """
        models = Assistant.provide_models()
        table_data = {}
        for _file in fixtures:
            with open(_file, "r") as file:  # type: ignore
                content = json.load(file)  # type: ignore
                for table in content:
                    table_data.update({table["name"]: table["data"]})
        with Session(create_engine(url)) as session:
            for model in models:
                if model.__tablename__ in table_data:  # type: ignore
                    data = table_data[model.__tablename__]  # type: ignore
                    for record in data:
                        session.add(model(**record))  # type: ignore
                        logger.debug(
                            f"Row {record} added to "  # type: ignore
                            f"table buffer {model.__tablename__}"
                        )
                    session.commit()
        logger.success(f"Database {selector} populated")

    @staticmethod
    @adapt("revision")
    def db_revision(
        config: Config,
        revision: str,
    ) -> None:
        """
        Creates Alembic's revision file in migration directory.

        :param config: Alembic's configuration object
        :param revision: Parent revision

        :return: None
        """
        command.revision(
            config,
            autogenerate=True,
            head=revision,
        )
        logger.success("Revision created")

    @staticmethod
    @adapt("upgrade")
    def db_upgrade(
        config: Config,
        revision: str,
        selector: str,
    ) -> None:
        """
        Applies migration file to a database.

        :param config: Alembic's configuration object
        :param revision: Revision to apply to database
        :param selector: Database name in settings.py module
        :return: None
        """
        command.upgrade(config, revision)
        logger.success(f"Database {selector} upgraded")

    @staticmethod
    def project_setup(name: str, path: str) -> None:
        """
        Creates a project directory with all needed files.

        :param name: Project's name
        :param path: Path to project files
        :return: None
        :raises BasicManagerException:
        """
        if path != ".":
            path = os.path.join(path, name)
            if os.path.exists(path):
                raise BasicManagerException("Directory already exists")
            logger.opt(colors=True).info(
                f"Creating project directory for project "
                f"<yellow>{name}</yellow>"
            )
            os.mkdir(path)

        for _name, content in FILES.items():
            file_path = os.path.join(path, _name)
            if os.sep in _name:
                os.makedirs(
                    os.sep.join(file_path.split(os.sep)[:-1]), exist_ok=True
                )
            with open(file_path, "w") as file:
                logger.debug(f"Creating project file {_name} at {file_path}")
                file.write(f"{content.format(name=name).strip()}\n")

        logger.success(f"Project structure created for {name}")

    @staticmethod
    @show_observer_catalogue
    def observe_catalogue(**context) -> dict:
        """
        Pass context dict to illuminate.decorators.cli.show_observe_catalogue.

        :return: dict
        """
        return context

    @show_logo
    @show_info
    def observe_start(self) -> None:
        """
        Starts producer/consumer ETL process.

        :return: None
        """
        io_loop = ioloop.IOLoop.current()
        io_loop.run_sync(self._observe_start)

    async def __start(self) -> None:
        """
        Initializes Adapters and Observers and pass initial Observation
        objects to self.__observation.

        :return: None
        """
        for adapter in self.adapters:
            self._adapters.append(adapter(manager=self))
            logger.opt(colors=True).info(
                f"Adapter <yellow>{adapter.__name__}</yellow> initialized"
            )

        for observer in self.observers:
            instance = observer(manager=self)
            self._observers.append(instance)
            logger.opt(colors=True).info(
                f"Observer <yellow>{observer.__name__}</yellow> initialized"
            )
            for _observation in instance.initial_observations:
                await self.__router(_observation)

    async def __router(
        self, item: Union[Exporter, Finding, Observation]
    ) -> None:
        """
        Routes object based on its class to proper queue.

        :param item: Exporter, Finding or Observation object
        :return: None
        """
        if isinstance(item, Exporter):
            await self.__export_queue.put(item)
        elif isinstance(item, Finding):
            if inspect.stack()[1][3] != "__adaptation":
                await self.__adapt_queue.put(item)
            else:
                logger.warning(
                    f"Findings can only yield Exporters and Observations "
                    f"thus rejecting item {item}"
                )
        elif isinstance(item, Observation):
            _hash = hash(item)
            if _hash not in self.__observing:
                self.__observing.add(_hash)
                if isinstance(item, HTTPObservation) and not item.allowed:
                    return
                await self.__observe_queue.put(item)
        else:
            logger.warning(
                f"Manager rejected item {item} due to unsupported "
                f"item type {type(item)}"
            )

    @logger.catch
    async def __observe(self) -> None:
        """
        Takes Observation object from self.__observe_queue and, after delay,
        pass it to self.__observation method.

        :return: None
        """
        async for item in self.__observe_queue:
            if not item:
                return
            await asyncio.sleep(
                self.settings.OBSERVATION_CONFIGURATION["delay"]
            )
            await self.__observation_switch(item)
            logger.debug(f"Coroutine observed {item}")
            del item
            self.__observe_queue.task_done()

    async def __observe_file(self, item: FileObservation) -> None:
        """
        Calls FileObservation's observe method and pass result to resolve
        function.

        :param item: FileObservation object
        :return: None
        """
        async with item.observe(xcom=item.xcom) as result:
            await self.__observation_resolve(result, item.url)

    async def __observe_http(self, item: HTTPObservation) -> None:
        """
        Prepares HTTPObservation configuration, calls observe method and pass
        result to resolve function.

        :param item: HTTPObservation object
        :return: None
        """
        item.configuration = {
            **self.settings.OBSERVATION_CONFIGURATION["http"],
            **item.configuration,
        }
        result = await item.observe(xcom=item.xcom)
        await self.__observation_resolve(result, item.url)

    async def __observe_sql(self, item: SQLObservation) -> None:
        """
        Calls SQLObservation's observe method and pass result to resolve
        function.

        :param item: SQLObservation object
        :return: None
        """
        result = await item.observe(self.sessions[item.url], xcom=item.xcom)
        await self.__observation_resolve(result, f"{item.url}:{item.query}")

    async def __observe_splash(self, item: SplashObservation) -> None:
        """
        Prepares SplashObservation configuration, calls observe method and pass
        result to resolve function.

        :param item: SplashObservation object
        :return: None
        """
        item.configuration = {
            **self.settings.OBSERVATION_CONFIGURATION["splash"],
            **item.configuration,
        }
        result = await item.observe(
            self.settings.OBSERVATION_CONFIGURATION["http"], xcom=item.xcom
        )
        await self.__observation_resolve(result, item.url)

    async def __observation_resolve(
        self, result: Union[None, Result], url: str
    ):
        """
        Resolves Observation.

        :param result: Result
        :param url: URL str
        :return: None
        """
        if not result:
            self.__not_observed.add(url)
            return
        self.__observed.add(url)
        try:
            if inspect.isawaitable(result):
                await result
            if inspect.isasyncgen(result):
                async for _item in result:
                    await self.__router(_item)
        except Exception:  # noqa
            stack = traceback.format_exc().strip().replace("<", "\\<")
            logger.opt(colors=True).warning(
                "Observation callback throws the following exception\n"
                f"<red>{stack}</red>"
            )

    async def __observation_switch(self, item: Observation) -> None:
        """
        Passes Observation object to proper method based on its class.

        :param item: Observation object
        :return: None
        """
        if isinstance(item, HTTPObservation):
            if isinstance(item, SplashObservation):
                await self.__observe_splash(item)
            else:
                await self.__observe_http(item)
        elif isinstance(item, FileObservation):
            await self.__observe_file(item)
        elif isinstance(item, SQLObservation):
            await self.__observe_sql(item)
        else:
            logger.warning(
                f"Observation of a type {type(item)} is not supported"
            )

    @logger.catch
    async def __adapt(self) -> None:
        """
        Takes Finding object from self.__adapt_queue and pass it to
        self.__adaptation method.

        :return: None
        """
        async for item in self.__adapt_queue:
            if not item:
                return
            await self.__adaptation(item)
            logger.debug(f"Coroutine adapted {item}")
            del item
            self.__adapt_queue.task_done()

    async def __adaptation(self, item: Finding) -> None:
        """
        Passes Finding object to Adapter's instance adapt method.

        :param item: Finding object
        :return: None
        """
        for adapter in self._adapters:
            for subscriber in adapter.subscribers:
                if isinstance(item, subscriber):
                    try:
                        items = adapter.adapt(item)
                        async for _item in items:  # type: ignore
                            await self.__router(_item)
                    except Exception as exception:
                        logger.warning(f"{self}.adapt() -> {exception}")

    @logger.catch
    async def __export(self) -> None:
        """
        Takes Exporter object from self.__export_queue and pass it to
        self.__exportation method.

        :return: None
        """
        async for item in self.__export_queue:
            if not item:
                return
            await self.__export_to(item)
            logger.debug(f"Coroutine exported {item}")
            del item
            self.__export_queue.task_done()

    async def __export_to(self, item: Exporter) -> None:
        """
        Passes Exporter object to proper method based on its class.

        :param item: Exporter object
        :return: None
        """
        if isinstance(item, (InfluxDBExporter, SQLExporter)):
            await self.__export_to_database(item)

    async def __export_to_database(
        self, item: Union[InfluxDBExporter, SQLExporter]
    ) -> None:
        """
        Acquires database session based on Exporter's attributes and pass it to
        Exporter's export method.

        :param item: InfluxDBExporter or SQLExporter object
        :return: None
        """
        try:
            session = self.sessions[item.name]
            await item.export(session)
            self.__exported.add(item)
        except BasicExporterException:
            pass
        except KeyError:
            logger.warning(f"Database {item.name} of is not found in context")

    @logger.catch
    async def _observe_start(self) -> None:
        """
        Starts producer/consumer ETL process.

        :return: None
        """
        self.adapters.sort(key=lambda x: x.priority, reverse=True)

        _observers = self.settings.CONCURRENCY["observers"]
        _adapters = self.settings.CONCURRENCY["adapters"]
        _exporters = self.settings.CONCURRENCY["exporters"]

        observers = gen.multi([self.__observe() for _ in range(_observers)])
        adapters = gen.multi([self.__adapt() for _ in range(_adapters)])
        exporters = gen.multi([self.__export() for _ in range(_exporters)])

        await self.__start()
        await self.__observe_queue.join()
        await self.__adapt_queue.join()
        await self.__export_queue.join()

        for _ in range(_observers):
            await self.__observe_queue.put(None)
        for _ in range(_adapters):
            await self.__adapt_queue.put(None)
        for _ in range(_exporters):
            await self.__export_queue.put(None)

        await observers
        await adapters
        await exporters

        for session in self.sessions:
            if isinstance(self.sessions[session], InfluxDBClient):
                await self.sessions[session].close()  # type: ignore

__adapt() -> None async

Takes Finding object from self.__adapt_queue and pass it to self.__adaptation method.

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
@logger.catch
async def __adapt(self) -> None:
    """
    Takes Finding object from self.__adapt_queue and pass it to
    self.__adaptation method.

    :return: None
    """
    async for item in self.__adapt_queue:
        if not item:
            return
        await self.__adaptation(item)
        logger.debug(f"Coroutine adapted {item}")
        del item
        self.__adapt_queue.task_done()

__adaptation(item: Finding) -> None async

Passes Finding object to Adapter's instance adapt method.

PARAMETER DESCRIPTION
item

Finding object

TYPE: Finding

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __adaptation(self, item: Finding) -> None:
    """
    Passes Finding object to Adapter's instance adapt method.

    :param item: Finding object
    :return: None
    """
    for adapter in self._adapters:
        for subscriber in adapter.subscribers:
            if isinstance(item, subscriber):
                try:
                    items = adapter.adapt(item)
                    async for _item in items:  # type: ignore
                        await self.__router(_item)
                except Exception as exception:
                    logger.warning(f"{self}.adapt() -> {exception}")

__export() -> None async

Takes Exporter object from self.__export_queue and pass it to self.__exportation method.

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
@logger.catch
async def __export(self) -> None:
    """
    Takes Exporter object from self.__export_queue and pass it to
    self.__exportation method.

    :return: None
    """
    async for item in self.__export_queue:
        if not item:
            return
        await self.__export_to(item)
        logger.debug(f"Coroutine exported {item}")
        del item
        self.__export_queue.task_done()

__export_to(item: Exporter) -> None async

Passes Exporter object to proper method based on its class.

PARAMETER DESCRIPTION
item

Exporter object

TYPE: Exporter

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __export_to(self, item: Exporter) -> None:
    """
    Passes Exporter object to proper method based on its class.

    :param item: Exporter object
    :return: None
    """
    if isinstance(item, (InfluxDBExporter, SQLExporter)):
        await self.__export_to_database(item)

__export_to_database(item: Union[InfluxDBExporter, SQLExporter]) -> None async

Acquires database session based on Exporter's attributes and pass it to Exporter's export method.

PARAMETER DESCRIPTION
item

InfluxDBExporter or SQLExporter object

TYPE: Union[InfluxDBExporter, SQLExporter]

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __export_to_database(
    self, item: Union[InfluxDBExporter, SQLExporter]
) -> None:
    """
    Acquires database session based on Exporter's attributes and pass it to
    Exporter's export method.

    :param item: InfluxDBExporter or SQLExporter object
    :return: None
    """
    try:
        session = self.sessions[item.name]
        await item.export(session)
        self.__exported.add(item)
    except BasicExporterException:
        pass
    except KeyError:
        logger.warning(f"Database {item.name} of is not found in context")

__init__(adapters: list[Type[Adapter]], name: str, observers: list[Type[Observer]], path: str, sessions: dict[str, Union[Type[AsyncSession], InfluxDBClient]], settings: ModuleType, *args, **kwargs)

Manager's init method.

PARAMETER DESCRIPTION
adapters

List of Adapters found in project files

TYPE: list[Type[Adapter]]

name

Project's name

TYPE: str

observers

List of Observers found in project files after filtering

TYPE: list[Type[Observer]]

path

Path to project files

TYPE: str

sessions

Database sessions

TYPE: dict[str, Union[Type[AsyncSession], InfluxDBClient]]

settings

Project's settings.py module

TYPE: ModuleType

Source code in illuminate/manager/manager.py
def __init__(
    self,
    adapters: list[Type[Adapter]],
    name: str,
    observers: list[Type[Observer]],
    path: str,
    sessions: dict[str, Union[Type[AsyncSession], InfluxDBClient]],
    settings: ModuleType,
    *args,
    **kwargs,
):
    """
    Manager's __init__ method.

    :param adapters: List of Adapters found in project files
    :param name: Project's name
    :param observers: List of Observers found in project files after
    filtering
    :param path: Path to project files
    :param sessions: Database sessions
    :param settings: Project's settings.py module
    """
    self.adapters = adapters
    self.name = name
    self.observers = observers
    self.path = path
    self.sessions = sessions
    self.settings = settings
    self._adapters: list[Adapter] = []
    self._observers: list[Observer] = []
    self.__observe_queue: queues.Queue = queues.Queue()
    self.__adapt_queue: queues.Queue = queues.Queue()
    self.__export_queue: queues.Queue = queues.Queue()
    self.__exported: set = set()
    self.__not_observed: set = set()
    self.__observed: set = set()
    self.__observing: set = set()

__observation_resolve(result: Union[None, Result], url: str) async

Resolves Observation.

PARAMETER DESCRIPTION
result

Result

TYPE: Union[None, Result]

url

URL str

TYPE: str

RETURNS DESCRIPTION

None

Source code in illuminate/manager/manager.py
async def __observation_resolve(
    self, result: Union[None, Result], url: str
):
    """
    Resolves Observation.

    :param result: Result
    :param url: URL str
    :return: None
    """
    if not result:
        self.__not_observed.add(url)
        return
    self.__observed.add(url)
    try:
        if inspect.isawaitable(result):
            await result
        if inspect.isasyncgen(result):
            async for _item in result:
                await self.__router(_item)
    except Exception:  # noqa
        stack = traceback.format_exc().strip().replace("<", "\\<")
        logger.opt(colors=True).warning(
            "Observation callback throws the following exception\n"
            f"<red>{stack}</red>"
        )

__observation_switch(item: Observation) -> None async

Passes Observation object to proper method based on its class.

PARAMETER DESCRIPTION
item

Observation object

TYPE: Observation

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __observation_switch(self, item: Observation) -> None:
    """
    Passes Observation object to proper method based on its class.

    :param item: Observation object
    :return: None
    """
    if isinstance(item, HTTPObservation):
        if isinstance(item, SplashObservation):
            await self.__observe_splash(item)
        else:
            await self.__observe_http(item)
    elif isinstance(item, FileObservation):
        await self.__observe_file(item)
    elif isinstance(item, SQLObservation):
        await self.__observe_sql(item)
    else:
        logger.warning(
            f"Observation of a type {type(item)} is not supported"
        )

__observe() -> None async

Takes Observation object from self.__observe_queue and, after delay, pass it to self.__observation method.

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
@logger.catch
async def __observe(self) -> None:
    """
    Takes Observation object from self.__observe_queue and, after delay,
    pass it to self.__observation method.

    :return: None
    """
    async for item in self.__observe_queue:
        if not item:
            return
        await asyncio.sleep(
            self.settings.OBSERVATION_CONFIGURATION["delay"]
        )
        await self.__observation_switch(item)
        logger.debug(f"Coroutine observed {item}")
        del item
        self.__observe_queue.task_done()

__observe_file(item: FileObservation) -> None async

Calls FileObservation's observe method and pass result to resolve function.

PARAMETER DESCRIPTION
item

FileObservation object

TYPE: FileObservation

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __observe_file(self, item: FileObservation) -> None:
    """
    Calls FileObservation's observe method and pass result to resolve
    function.

    :param item: FileObservation object
    :return: None
    """
    async with item.observe(xcom=item.xcom) as result:
        await self.__observation_resolve(result, item.url)

__observe_http(item: HTTPObservation) -> None async

Prepares HTTPObservation configuration, calls observe method and pass result to resolve function.

PARAMETER DESCRIPTION
item

HTTPObservation object

TYPE: HTTPObservation

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __observe_http(self, item: HTTPObservation) -> None:
    """
    Prepares HTTPObservation configuration, calls observe method and pass
    result to resolve function.

    :param item: HTTPObservation object
    :return: None
    """
    item.configuration = {
        **self.settings.OBSERVATION_CONFIGURATION["http"],
        **item.configuration,
    }
    result = await item.observe(xcom=item.xcom)
    await self.__observation_resolve(result, item.url)

__observe_splash(item: SplashObservation) -> None async

Prepares SplashObservation configuration, calls observe method and pass result to resolve function.

PARAMETER DESCRIPTION
item

SplashObservation object

TYPE: SplashObservation

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __observe_splash(self, item: SplashObservation) -> None:
    """
    Prepares SplashObservation configuration, calls observe method and pass
    result to resolve function.

    :param item: SplashObservation object
    :return: None
    """
    item.configuration = {
        **self.settings.OBSERVATION_CONFIGURATION["splash"],
        **item.configuration,
    }
    result = await item.observe(
        self.settings.OBSERVATION_CONFIGURATION["http"], xcom=item.xcom
    )
    await self.__observation_resolve(result, item.url)

__observe_sql(item: SQLObservation) -> None async

Calls SQLObservation's observe method and pass result to resolve function.

PARAMETER DESCRIPTION
item

SQLObservation object

TYPE: SQLObservation

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __observe_sql(self, item: SQLObservation) -> None:
    """
    Calls SQLObservation's observe method and pass result to resolve
    function.

    :param item: SQLObservation object
    :return: None
    """
    result = await item.observe(self.sessions[item.url], xcom=item.xcom)
    await self.__observation_resolve(result, f"{item.url}:{item.query}")

__router(item: Union[Exporter, Finding, Observation]) -> None async

Routes object based on its class to proper queue.

PARAMETER DESCRIPTION
item

Exporter, Finding or Observation object

TYPE: Union[Exporter, Finding, Observation]

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __router(
    self, item: Union[Exporter, Finding, Observation]
) -> None:
    """
    Routes object based on its class to proper queue.

    :param item: Exporter, Finding or Observation object
    :return: None
    """
    if isinstance(item, Exporter):
        await self.__export_queue.put(item)
    elif isinstance(item, Finding):
        if inspect.stack()[1][3] != "__adaptation":
            await self.__adapt_queue.put(item)
        else:
            logger.warning(
                f"Findings can only yield Exporters and Observations "
                f"thus rejecting item {item}"
            )
    elif isinstance(item, Observation):
        _hash = hash(item)
        if _hash not in self.__observing:
            self.__observing.add(_hash)
            if isinstance(item, HTTPObservation) and not item.allowed:
                return
            await self.__observe_queue.put(item)
    else:
        logger.warning(
            f"Manager rejected item {item} due to unsupported "
            f"item type {type(item)}"
        )

__start() -> None async

Initializes Adapters and Observers and pass initial Observation objects to self.__observation.

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
async def __start(self) -> None:
    """
    Initializes Adapters and Observers and pass initial Observation
    objects to self.__observation.

    :return: None
    """
    for adapter in self.adapters:
        self._adapters.append(adapter(manager=self))
        logger.opt(colors=True).info(
            f"Adapter <yellow>{adapter.__name__}</yellow> initialized"
        )

    for observer in self.observers:
        instance = observer(manager=self)
        self._observers.append(instance)
        logger.opt(colors=True).info(
            f"Observer <yellow>{observer.__name__}</yellow> initialized"
        )
        for _observation in instance.initial_observations:
            await self.__router(_observation)

db_populate(fixtures: tuple[str], selector: str, url: str) -> None staticmethod

Populates database with fixtures.

PARAMETER DESCRIPTION
fixtures

Tuple of fixture files

TYPE: tuple[str]

selector

Database name in settings.py module

TYPE: str

url

SQLAlchemy Database URL

TYPE: str

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
@staticmethod
@adapt("populate")
def db_populate(
    fixtures: tuple[str],
    selector: str,
    url: str,
) -> None:
    """
    Populates database with fixtures.

    :param fixtures: Tuple of fixture files
    :param selector: Database name in settings.py module
    :param url: SQLAlchemy Database URL
    :return: None
    """
    models = Assistant.provide_models()
    table_data = {}
    for _file in fixtures:
        with open(_file, "r") as file:  # type: ignore
            content = json.load(file)  # type: ignore
            for table in content:
                table_data.update({table["name"]: table["data"]})
    with Session(create_engine(url)) as session:
        for model in models:
            if model.__tablename__ in table_data:  # type: ignore
                data = table_data[model.__tablename__]  # type: ignore
                for record in data:
                    session.add(model(**record))  # type: ignore
                    logger.debug(
                        f"Row {record} added to "  # type: ignore
                        f"table buffer {model.__tablename__}"
                    )
                session.commit()
    logger.success(f"Database {selector} populated")

db_revision(config: Config, revision: str) -> None staticmethod

Creates Alembic's revision file in migration directory.

PARAMETER DESCRIPTION
config

Alembic's configuration object

TYPE: Config

revision

Parent revision

TYPE: str

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
@staticmethod
@adapt("revision")
def db_revision(
    config: Config,
    revision: str,
) -> None:
    """
    Creates Alembic's revision file in migration directory.

    :param config: Alembic's configuration object
    :param revision: Parent revision

    :return: None
    """
    command.revision(
        config,
        autogenerate=True,
        head=revision,
    )
    logger.success("Revision created")

db_upgrade(config: Config, revision: str, selector: str) -> None staticmethod

Applies migration file to a database.

PARAMETER DESCRIPTION
config

Alembic's configuration object

TYPE: Config

revision

Revision to apply to database

TYPE: str

selector

Database name in settings.py module

TYPE: str

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
@staticmethod
@adapt("upgrade")
def db_upgrade(
    config: Config,
    revision: str,
    selector: str,
) -> None:
    """
    Applies migration file to a database.

    :param config: Alembic's configuration object
    :param revision: Revision to apply to database
    :param selector: Database name in settings.py module
    :return: None
    """
    command.upgrade(config, revision)
    logger.success(f"Database {selector} upgraded")

observe_catalogue(**context) -> dict staticmethod

Pass context dict to illuminate.decorators.cli.show_observe_catalogue.

RETURNS DESCRIPTION
dict

dict

Source code in illuminate/manager/manager.py
@staticmethod
@show_observer_catalogue
def observe_catalogue(**context) -> dict:
    """
    Pass context dict to illuminate.decorators.cli.show_observe_catalogue.

    :return: dict
    """
    return context

observe_start() -> None

Starts producer/consumer ETL process.

RETURNS DESCRIPTION
None

None

Source code in illuminate/manager/manager.py
@show_logo
@show_info
def observe_start(self) -> None:
    """
    Starts producer/consumer ETL process.

    :return: None
    """
    io_loop = ioloop.IOLoop.current()
    io_loop.run_sync(self._observe_start)

project_setup(name: str, path: str) -> None staticmethod

Creates a project directory with all needed files.

PARAMETER DESCRIPTION
name

Project's name

TYPE: str

path

Path to project files

TYPE: str

RETURNS DESCRIPTION
None

None

RAISES DESCRIPTION
BasicManagerException
Source code in illuminate/manager/manager.py
@staticmethod
def project_setup(name: str, path: str) -> None:
    """
    Creates a project directory with all needed files.

    :param name: Project's name
    :param path: Path to project files
    :return: None
    :raises BasicManagerException:
    """
    if path != ".":
        path = os.path.join(path, name)
        if os.path.exists(path):
            raise BasicManagerException("Directory already exists")
        logger.opt(colors=True).info(
            f"Creating project directory for project "
            f"<yellow>{name}</yellow>"
        )
        os.mkdir(path)

    for _name, content in FILES.items():
        file_path = os.path.join(path, _name)
        if os.sep in _name:
            os.makedirs(
                os.sep.join(file_path.split(os.sep)[:-1]), exist_ok=True
            )
        with open(file_path, "w") as file:
            logger.debug(f"Creating project file {_name} at {file_path}")
            file.write(f"{content.format(name=name).strip()}\n")

    logger.success(f"Project structure created for {name}")