class RootContainer(DeclarativeContainer): """Application IoC container""" config = Configuration('config') loop = Singleton(get_event_loop) # Remote services engine = Singleton(_create_engine_wrapper, config.db.connect_string, config.db.options) http = Factory(ClientSession, loop=loop, raise_for_status=True) sessionmaker = Singleton(_sessionmaker, bind=engine) scoped_session = Singleton(_scoped_session, sessionmaker, scopefunc=event_context.get) context_factory = DelegatedFactory(Context, scoped_session=scoped_session) bot = Singleton(Bot, command_prefix=config.cmd_prefix, context_factory=context_factory, default_game=config.default_game, loop=loop, scoped_session=scoped_session) # Main run_bot = Callable(Bot.run, bot, config.token)
class Container(DeclarativeContainer): """DI container.""" mail_service = Singleton(example.MailService, host='localhost', port=587, login='******', password='******') add_user = Callable(example.add_user, mailer=mail_service)
class UpdateContainer(DeclarativeContainer): config = Configuration(strict=True) control_actions_predictor = Dependency() temp_graph_updater = Dependency() weather_forecast_updater = Dependency() control_actions_predictor.enable_async_mode() temp_graph_updater.enable_async_mode() weather_forecast_updater.enable_async_mode() temp_graph_updatable_item = Singleton( TempGraphUpdatableItem, provider=temp_graph_updater.provider, update_interval=Callable( pd.Timedelta, seconds=config.update_intervals.temp_graph ) ) weather_forecast_updatable_item = Singleton( WeatherForecastUpdatableItem, provider=weather_forecast_updater.provider, update_interval=Callable( pd.Timedelta, seconds=config.update_intervals.weather_forecast ) ) control_action_updatable_item = Singleton( ControlActionUpdatableItem, provider=control_actions_predictor.provider, dependencies=List( temp_graph_updatable_item, weather_forecast_updatable_item ) ) updater_service = Singleton( SimpleUpdaterService, item_to_update=control_action_updatable_item )
class Gateways(DeclarativeContainer): config = Configuration(strict=True) temp_graph_reader = Factory(SoftMSyncTempGraphJSONReader) temp_graph_loader = Factory(SoftMAsyncTempGraphOnlineLoader, reader=temp_graph_reader) weather_forecast_timezone = Callable( gettz, config.weather_forecast_loader.weather_server_timezone) weather_forecast_reader = Factory( SoftMSyncWeatherForecastJSONReader, weather_data_timezone=weather_forecast_timezone) weather_forecast_loader = Factory(SoftMAsyncWeatherForecastOnlineLoader, reader=weather_forecast_reader) time_delta_loader = Factory( SyncTimedeltaFileLoader, filepath=config.time_delta_loader.heating_objects_time_delta_path, reader=Factory(SyncTimedeltaCSVReader, separator=","))
class Services(DeclarativeContainer): ids = Callable(lambda: [1, 2, 3]) api_url = Object('https://ya.ru') sync_service: t.Callable[[], SyncProto] = Singleton(SyncService).add_attributes(ids=ids, api_url=api_url)
class App(DeclarativeContainer): main = Callable(main)
def callable(self, alias, instance, *args, **kwargs): setattr(self.container, alias, Callable(instance, *args, **kwargs))