def app_bootstrap(settings: Settings) -> ServiceRegistry: # Make the registry registry = ServiceRegistry() # Store the settings in the registry so things later can # get to them. registry.register_singleton(settings, Settings) # Scan for registrations scanner = venusian.Scanner(registry=registry, settings=settings) from . import models scanner.scan(models) # Grab the datastore singleton to pass into setup container: ServiceContainer = registry.create_container() datastore: Datastore = container.get(Datastore) # Do setup for the core application features setup(registry, datastore) # Import the add-on and initialize it from . import custom scanner.scan(custom) custom.setup(registry, datastore) return registry
def setup() -> ServiceRegistry: # Make the registry registry = ServiceRegistry() # Make the greeter greeter = Greeter(greeting='Hello') # Register it as a singleton using its class for the "key" registry.register_singleton(greeter, Greeter) return registry
def app_setup(): # Make the application's registry registry = ServiceRegistry() # Greeters are nice...they greet people! greeter = Greeter(greeting='Hello') # Register it as a singleton using its class for the "key" registry.register_singleton(greeter, Greeter) return registry
def test(): assert isinstance(Foo.impl00, AutowireField) assert isinstance(Foo.impl01, AutowireField) assert isinstance(Foo.impl02, AutowireField) assert isinstance(Foo.impl03, AutowireField) registry = ServiceRegistry() enable_autowire(registry) impl00 = object() impl01 = object() impl02 = object() impl03 = object() registry.register_singleton(impl00, Iface00) registry.register_singleton(impl01, name="impl01") registry.register_singleton(impl02, name="r_impl02") registry.register_singleton(impl03, context=Ctxt) registry.register_autowire( Foo, IFoo, namespace=dict(n_impl02="r_impl02", n_impl03=Ctxt()), lazy=False, ) container = registry.create_container() foo = container.get(IFoo) assert foo.impl00 is impl00 assert foo.impl01 is impl01 assert foo.impl02 is impl02 assert foo.impl03 is impl03
def setup(registry: ServiceRegistry, settings: Settings): """ Initialize the features in the core application """ # Make and register the Datastore singleton datastore = Datastore() registry.register_singleton(datastore, Datastore) # Context factory def context_factory(container) -> Resource: # Presumes that "url" is in the container ds: Datastore = container.get(Datastore) url: str = container.get(Url) context: Resource = ds.customers.get(url) return context registry.register_factory(context_factory, Resource) # Request factory def request_factory(container) -> Request: url: str = container.get(Url) request = Request(url=url, container=container) return request registry.register_factory(request_factory, Request) # **** Default View def view_factory(container) -> View: request: Request = container.get(Request) context: Resource = container.get(Resource) greeter: Greeter = container.get(Greeter, context=context) view = View(request=request, context=context, greeter=greeter) return view registry.register_factory(view_factory, View) # **** Default Greeter def default_greeter_factory(container) -> Greeter: # Use the dataclass default for greeting return Greeter(punctuation=settings.punctuation) # Register it as a factory using its class for the "key" registry.register_factory(default_greeter_factory, Greeter) # During bootstrap, make some Customers mary = Customer(name='mary', title='Mary') datastore.customers['mary'] = mary
def app_bootstrap(settings: Settings) -> ServiceRegistry: # Make the registry registry = ServiceRegistry() # Store the settings in the registry so things later can # get to them. registry.register_singleton(settings, Settings) # Make and register the Datastore singleton datastore = Datastore() registry.register_singleton(datastore, Datastore) # Do setup for the core application features setup(registry, datastore) # Import the add-on and initialize it from .custom import setup as addon_setup addon_setup(registry, datastore) return registry
def setup(registry: ServiceRegistry, settings: Settings): """ Initialize the features in the core application """ # Make and register the Datastore datastore = Datastore() registry.register_singleton(datastore, Datastore) # **** Default Greeter # Make the greeter factory, using punctuation from settings punctuation = settings.punctuation def default_greeter_factory(container) -> Greeter: # Use the dataclass default for greeting return Greeter(punctuation=punctuation) # Register it as a factory using its class for the "key" registry.register_factory(default_greeter_factory, Greeter) # During bootstrap, make some Customers customer1 = Customer(name='Mary') datastore.customers.append(customer1)
def make_registry( root: Optional[Root] = None, root_factory: Optional[Callable] = None, scannables: Union[Iterable[Scannable], Scannable] = tuple(), plugins: Union[Iterable[Plugin], Plugin] = tuple(), theme_config: Optional[ThemeConfig] = None, ) -> ServiceRegistry: """ Construct a Themester registry with some defaults """ registry = ServiceRegistry() # Handle the venusian scanner scanner = Scanner(registry=registry) registry.register_singleton(scanner, Scanner) # Handle the root if root is not None: registry.register_singleton(root, Root) # Handle a root factory if root_factory is not None: registry.register_factory(root_factory, Root) # Handle the theme config if theme_config is not None: registry.register_singleton(theme_config, ThemeConfig) # Scan themester factories _scan_target(scanner, factories) # Handle anything that needs to be scanned if isinstance(scannables, Sequence): for scannable in scannables: _scan_target(scanner, scannable) else: _scan_target(scanner, scannables) # Handle any plugins if isinstance(plugins, Sequence): for plugin in plugins: _setup_target(registry, scanner, plugin) else: _setup_target(registry, scanner, plugins) return registry
def wired_setup(registry: ServiceRegistry): scanner = WiredScanner(registry=registry) registry.register_singleton(scanner, IScanner)
def assert_lazy(cls_args, cls_kwargs): assert isinstance(Foo.impl00, AutowireField) assert isinstance(Foo.impl01, AutowireField) assert isinstance(Foo.impl02, AutowireField) assert isinstance(Foo.impl03, AutowireField) assert isinstance(Foo.impl04, AutowireField) assert isinstance(Foo.impl05, AutowireField) assert isinstance(Foo.impl06, AutowireField) registry = ServiceRegistry() enable_autowire(registry) impl00 = object() impl01 = object() impl02 = object() impl03 = object() impl04 = object() impl05 = object() impl06 = object() registry.register_singleton(impl00, Iface00) registry.register_singleton(impl01, name="impl01") registry.register_singleton(impl02, name="r_impl02") registry.register_singleton(impl03, context=Ctxt) registry.register_singleton(impl04, name="pv_impl04") registry.register_singleton(impl05, context=Ctxt2) registry.register_singleton(impl06, context=IFoo) registry.register_autowire( Foo, IFoo, namespace=dict(n_impl02="r_impl02", n_impl03=Ctxt()), cls_args=cls_args, cls_kwargs=cls_kwargs, lazy=True, ) container = registry.create_container() foo = container.get(IFoo) assert foo.impl00 is impl00 assert foo.impl01 is impl01 assert foo.impl02 is impl02 assert foo.impl03 is impl03 assert foo.impl04 is impl04 assert foo.impl05 is impl05 assert foo.impl06 is impl06
def root_setup(registry: ServiceRegistry) -> None: from wired_components import samples d = Path(samples.__file__).parent / 'simple' / 'contents' root: Root = load_resources(d) registry.register_singleton(root, IRoot)