Skip to content

Commit

Permalink
standardise on 'key' for the name of parameters that take a ResourceKey
Browse files Browse the repository at this point in the history
  • Loading branch information
cjw296 committed Feb 16, 2020
1 parent d2e48ec commit 63e30d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
30 changes: 14 additions & 16 deletions mush/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Optional, Any, Union, Type, Callable, NewType
from typing import Optional

from .declarations import nothing, extract_requires, Requirement, RequiresType
from .declarations import (
nothing, extract_requires, RequiresType,
ResourceKey, ResourceValue, Resolver
)
from .markers import missing
from .resolvers import ValueResolver

Expand Down Expand Up @@ -58,11 +61,6 @@ def type_key(type_tuple):
return type.__name__


ResourceKey = NewType('ResourceKey', Union[Type, str])
ResourceValue = NewType('ResourceValue', Any)
Resolver = Callable[['Context'], ResourceValue]


class Context:
"Stores resources for a particular run."

Expand Down Expand Up @@ -92,16 +90,16 @@ def add(self,
resolver = ValueResolver(resource)
self._store[provides] = resolver

def remove(self, provides: ResourceKey, *, strict: bool = True):
def remove(self, key: ResourceKey, *, strict: bool = True):
"""
Remove the specified resource key from the context.
If ``strict``, then a :class:`ContextError` will be raised if the
specified resource is not present in the context.
"""
if strict and provides not in self._store:
raise ContextError(f'Context does not contain {provides!r}')
self._store.pop(provides, None)
if strict and key not in self._store:
raise ContextError(f'Context does not contain {key!r}')
self._store.pop(key, None)

def __repr__(self):
bits = []
Expand All @@ -125,15 +123,15 @@ def call(self, obj, requires=None):
kw = {}

for requirement in requires:
o = self.get(requirement.base, missing)
o = self.get(requirement.key, missing)

for op in requirement.ops:
o = op(o)
if o is nothing:
break

if o is missing:
if requirement.base is Context:
if requirement.key is Context:
o = self
else:
raise ContextError('No %s in context' % repr(requirement.spec))
Expand All @@ -147,10 +145,10 @@ def call(self, obj, requires=None):

return obj(*args, **kw)

def get(self, requirement: ResourceKey, default=None):
resolver = self._store.get(requirement, None)
def get(self, key: ResourceKey, default=None):
resolver = self._store.get(key, None)
if resolver is None:
if requirement is Context:
if key is Context:
return self
return default
return resolver(self)
9 changes: 7 additions & 2 deletions mush/declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
update_wrapper as functools_update_wrapper,
)
from inspect import signature
from typing import List
from typing import List, Type, Optional, Callable, Sequence, NewType, Union, Any

from .markers import missing


ResourceKey = NewType('ResourceKey', Union[Type, str])
ResourceValue = NewType('ResourceValue', Any)
Resolver = Callable[['Context'], ResourceValue]


def name_or_repr(obj):
return getattr(obj, '__name__', None) or repr(obj)

Expand All @@ -29,7 +34,7 @@ def __init__(self, source, target=None):
while isinstance(source, how):
self.ops.appendleft(source.process)
source = source.type
self.base = source
self.key: ResourceKey = source

def __repr__(self):
requirement_repr = name_or_repr(self.spec)
Expand Down

0 comments on commit 63e30d0

Please sign in to comment.