Skip to content

Commit

Permalink
Merged ResourceCollection to Context and reworked lazy resources
Browse files Browse the repository at this point in the history
Lazy resources now work almost like normal resources.
Added plenty of missing docstrings too.
  • Loading branch information
agronholm committed Sep 8, 2015
1 parent 1878137 commit 815609a
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 563 deletions.
8 changes: 4 additions & 4 deletions asphalt/core/application.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from concurrent.futures import ThreadPoolExecutor
import threading
from typing import Dict, Any, Union
from logging import getLogger
import logging.config
import threading
import asyncio

from pkg_resources import iter_entry_points, EntryPoint

from .component import Component
from .context import ApplicationContext
from .context import Context, ApplicationContext
from . import util

__all__ = 'Application',
Expand Down Expand Up @@ -47,7 +47,7 @@ def add_component(self, component_alias: str, component_class: type=None, **comp
"""
Instantiates a component and adds it to the component list used by this application.
The first argument can either be a :cls:`~asphalt.core.component.Component` subclass or a
The first argument can either be a :class:`~asphalt.core.component.Component` subclass or a
component type name, declared as an ``asphalt.components`` entry point, in which case the
component class is retrieved by loading the entry point.
Expand Down Expand Up @@ -75,7 +75,7 @@ def add_component(self, component_alias: str, component_class: type=None, **comp
component = component_class(**component_kwargs)
self.components.append(component)

def start(self, app_ctx: ApplicationContext):
def start(self, ctx: Context):
"""
This method can be overridden to provide additional resources to the components.
It can be a coroutine.
Expand Down
6 changes: 3 additions & 3 deletions asphalt/core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import yaml

from .application import Application
from .context import ApplicationContext
from .context import Context
from .util import resolve_reference


Expand Down Expand Up @@ -46,10 +46,10 @@ def __init__(self, **config):
# ADD COMPONENTS HERE
@coroutine
def start(app_ctx: ApplicationContext):
def start(ctx: Context):
# ADD ADDITIONAL RESOURCES HERE (if needed)
pass
""".format(app_cls=Application, context_cls=ApplicationContext, app_subclass=app_subclass))
""".format(app_cls=Application, context_cls=Context, app_subclass=app_subclass))

with (project / 'config.yml').open('w') as f:
f.write("""\
Expand Down
22 changes: 11 additions & 11 deletions asphalt/core/component.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABCMeta, abstractmethod

from .context import ApplicationContext
from .context import Context

__all__ = 'Component',

Expand All @@ -11,19 +11,19 @@ class Component(metaclass=ABCMeta):
__slots__ = ()

@abstractmethod
def start(self, app_ctx: ApplicationContext):
def start(self, ctx: Context):
"""
This method is called on application start. It can be a coroutine.
The application context can be used to:
* add application start/finish callbacks
* add default callbacks for other contexts
* add context getters
* add resources (via app_ctx.resources)
* request resources (via app_ctx.resources)
The context can be used to:
* add context event listeners (:meth:`~Context.add_event_listener`)
* add resources (:meth:`~Context.add_resource`)
* add lazily created resources (:meth:`~Context.add_lazy_resource`)
* request resources (:meth:`~Context.request_resource`)
When dealing with resources, it is advisable to add as many resources as possible
before requesting any. This will speed up the dependency resolution.
If the component requests any resources, it is advisable to first add all the resources
it can before requesting any. This will speed up the dependency resolution and prevent
deadlocks.
:param app_ctx: the application context
:param ctx: the context for which the component was created
"""

0 comments on commit 815609a

Please sign in to comment.