-
Notifications
You must be signed in to change notification settings - Fork 4
Allow local values when constructing the injector. #18
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
import functools | ||
import inspect | ||
import re | ||
import warnings | ||
import sys | ||
|
||
import six | ||
|
@@ -366,7 +365,7 @@ class Injector(object): | |
generator_provider = GeneratorProvider | ||
re_note = re.compile(r'^(.*?)(?::(.*))?$') # annotation is 'object:name' | ||
|
||
def __init__(self, provide_self=True): | ||
def __init__(self, *dicts, **values): | ||
"""A subclass could take arguments, but should pass keywords to super. | ||
|
||
An Injector subclass inherits the provider registry of its base | ||
|
@@ -399,13 +398,23 @@ class Injector(BaseInjector): | |
|
||
with Injector() as injector: | ||
injector.get('injector') | ||
|
||
Local values can be provided when constructing the injector:: | ||
|
||
with RequestInjector(request=request) as injector: | ||
injector.get('form:username') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doc example doesn't make sense without more context. A more trivial illustration would be clearer: with Injector(foo='bar') as injector:
injector.get('foo') # 'bar' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sort of on the fence here. The docs could use some more concrete examples that demonstrate useful patterns. This hints at such an example, but it's obviously incomplete. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe include both? Start with the trivial, then mention that a useful pattern is in creating request lifecycles. This allows values to be provided trivially: with Injector(foo='bar') as injector:
injector.get('foo') # 'bar' A useful pattern is to have the injector's providers key off input data: with RequestInjector(request=request) as injector:
injector.get('form:username')
injector.apply(handler) |
||
|
||
""" | ||
|
||
self.annotator = self.annotator_class() | ||
|
||
self.closed = False | ||
self.instances = {} | ||
self.values = {} | ||
for d in dicts: | ||
self.values.update(d) | ||
self.values.update(values) | ||
self.values['injector'] = self | ||
|
||
self.finalizers = [] | ||
|
||
|
@@ -417,12 +426,6 @@ class Injector(BaseInjector): | |
#: This allows for dependency cycle checks. | ||
self.instantiating = [] | ||
|
||
if provide_self: | ||
self.values['injector'] = self | ||
else: | ||
warnings.warn( | ||
DeprecationWarning('provide_self=False is not supported')) | ||
|
||
@classmethod | ||
def provider(cls, note, provider=None, name=False): | ||
"""Register a provider, either a Provider class or a generator. | ||
|
@@ -802,15 +805,8 @@ class SubInjector(cls): | |
SubInjector.__bases__ = tuple(mixins) + SubInjector.__bases__ | ||
|
||
dicts = [ x for x in mixins_and_dicts if not isinstance(x, type) ] | ||
for d in reversed(dicts): | ||
for k,v in d.items(): | ||
if k not in values: | ||
values[k] = v | ||
|
||
for k,v in values.items(): | ||
SubInjector.value(k, v) | ||
|
||
return SubInjector() | ||
return SubInjector(*dicts, **values) | ||
|
||
|
||
class InjectorProxy(object): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes should go into README.rst.in given the project's custom doc builder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the built doc generated from the
jeni.Injector
docstring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right, I see that now. Reading the diff view isn't always optimal.