Skip to content

Commit

Permalink
:fix: model UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
b3j0f committed Mar 8, 2016
1 parent dacc069 commit 75293cf
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 384 deletions.
8 changes: 4 additions & 4 deletions b3j0f/conf/configurable/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ def clsconf(cls):
result = Configuration(
Category(
Configurable.CATEGORY,
Parameter(name=Configurable.DRIVERS, vtype=tuple),
Parameter(name=Configurable.CONFPATHS, vtype=tuple),
Parameter(name=Configurable.INHERITEDCONF, vtype=bool),
Parameter(name=Configurable.STORE, vtype=bool)
Parameter(name=Configurable.DRIVERS, ptype=tuple),
Parameter(name=Configurable.CONFPATHS, ptype=tuple),
Parameter(name=Configurable.INHERITEDCONF, ptype=bool),
Parameter(name=Configurable.STORE, ptype=bool)
)
)

Expand Down
10 changes: 5 additions & 5 deletions b3j0f/conf/configurable/test/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def setUp(self):
self.conf = Configuration(
Category(
'A',
Parameter('a', value='a', vtype=str),
Parameter('_', value=2, vtype=int),
Parameter('error', vtype=float, svalue='error')
Parameter('a', value='a', ptype=str),
Parameter('_', value=2, ptype=int),
Parameter('error', ptype=float, svalue='error')
),
Category(
'B',
Parameter('a', value='b', vtype=str),
Parameter('b', value='b', vtype=str)
Parameter('a', value='b', ptype=str),
Parameter('b', value='b', ptype=str)
)
)

Expand Down
6 changes: 3 additions & 3 deletions b3j0f/conf/driver/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ def setUp(self):
self.conf = Configuration(
Category(
'A',
Parameter('a', value=0, vtype=int), # a is 0
Parameter('b', value=True, vtype=bool)
Parameter('a', value=0, ptype=int), # a is 0
Parameter('b', value=True, ptype=bool)
), # b is overriden
Category(
'B',
Parameter('b', value=1, vtype=int), # b is 1
Parameter('b', value=1, ptype=int), # b is 1
)
)

Expand Down
12 changes: 6 additions & 6 deletions b3j0f/conf/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,18 @@ def copy(self, cleaned=False, *args, **kwargs):

@property
def params(self):
"""Get set of parameters.
"""Get set of parameters by names.
:rtype: list"""
:rtype: dict"""

result = set()
result = {}

for content in list(self.values()):

if isinstance(content, CompositeModelElement):
result = content.params | result
result.update(content.params)

else:
result.add(content)
result[content.name] = content

return list(result)
return result
10 changes: 5 additions & 5 deletions b3j0f/conf/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Configuration(CompositeModelElement):
__slots__ = CompositeModelElement.__slots__

def resolve(
self, configurable=None, scope=None, safe=True
self, configurable=None, scope=None, safe=True, besteffort=True
):
"""Resolve all parameters.
Expand All @@ -56,11 +56,11 @@ def resolve(

for category in self.values():

for param in category:
for param in category.values():

param.resolve(
configurable=configurable, conf=self,
scope=scope, safe=safe
scope=scope, safe=safe, besteffort=besteffort
)

def param(self, pname, cname=None, history=0):
Expand Down Expand Up @@ -109,13 +109,13 @@ def update(self, conf):
:param Configuration conf: configuration from where get parameter values
and svalues."""

for cat in conf:
for cat in conf.values():

if cat.name in self:

selfcat = self[cat.name]

for param in cat:
for param in list(cat.values()):

paramstoupdate = selfcat.getparams(param=param)

Expand Down
83 changes: 42 additions & 41 deletions b3j0f/conf/model/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from __future__ import absolute_import

__all__ = ['Parameter', 'VType']
__all__ = ['Parameter', 'PType']

from .base import ModelElement
from ..parser.core import parse, serialize
Expand All @@ -43,45 +43,49 @@

from copy import deepcopy

from ..parser.resolver.core import (
DEFAULT_SAFE, DEFAULT_BESTEFFORT, DEFAULT_SCOPE
)

class VType(object):

class PType(object):
"""Dedicated to embed a specific type such as a parameter type in order to
instanciate easily this embedded type from a parameter serialized value.
Instanciation depends on type of serialiazed value (by order):
- str: first arg to this _type.
- dict: it is given such as a kwargs to this _type.
- iterable: it is given such as an args to this _type.
- object: it is given such as the only one argument to this _type.
- str: first arg to this ptype.
- dict: it is given such as a kwargs to this ptype.
- iterable: it is given such as an args to this ptype.
- object: it is given such as the only one argument to this ptype.
"""

__slots__ = ('_type')
__slots__ = ('ptype')

def __init__(self, _type, *args, **kwargs):
def __init__(self, ptype, *args, **kwargs):

super(VType, self).__init__(*args, **kwargs)
super(PType, self).__init__(*args, **kwargs)

self._type = _type
self.ptype = ptype

def __instancecheck__(self, instance):
"""Check instance such as this instance or self _type instance."""
"""Check instance such as this instance or self ptype instance."""

return isinstance(instance, (VType, self._type))
return isinstance(instance, (PType, self.ptype))

def __subclasscheck__(self, subclass):
"""Check subclass such as this subclass or self _type subclass."""
"""Check subclass such as this subclass or self ptype subclass."""

return issubclass(subclass, (VType, self._type))
return issubclass(subclass, (PType, self.ptype))

def __call__(self, svalue):
"""Instantiate a new instance of this _type related to input value.
"""Instantiate a new instance of this ptype related to input value.
:param ssvalue: Instanciation depends on type of serialiazed value (by order):
- dict: it is given such as a kwargs to this _type.
- iterable: it is given such as an args to this _type.
- object: it is given such as the only one argument to this _type."""
- dict: it is given such as a kwargs to this ptype.
- iterable: it is given such as an args to this ptype.
- object: it is given such as the only one argument to this ptype."""

result = None

Expand All @@ -97,10 +101,10 @@ def __call__(self, svalue):
args = svalue

try:
result = self._type(*args, **kwargs)
result = self.ptype(*args, **kwargs)

except TypeError:
msg = 'Wrong value ({0}) with ({1}).'.format(svalue, self._type)
msg = 'Wrong value ({0}) with ({1}).'.format(svalue, self.ptype)
reraise(ParserError, ParserError(msg))

return result
Expand All @@ -126,7 +130,7 @@ class Parameter(ModelElement):
"""

__slots__ = (
'_name', 'vtype', 'parser', '_svalue', '_value', '_error', 'conf',
'_name', 'ptype', 'parser', '_svalue', '_value', '_error', 'conf',
'local', 'scope', 'configurable', 'serializer', 'besteffort', 'safe'
) + ModelElement.__slots__

Expand All @@ -140,14 +144,11 @@ class Error(Exception):
_PARAM_NAME_COMPILER_MATCHER = re_compile(PARAM_NAME_REGEX).match

DEFAULT_NAME = re_compile('.*')
DEFAULT_VTYPE = object #: default vtype.
DEFAULT_PTYPE = object #: default ptype.
DEFAULT_LOCAL = True #: default local value.
DEFAULT_BESTEFFORT = False #: default best effort.
DEFAULT_SAFE = True #: default safe
DEFAULT_SCOPE = None #: default scope

def __init__(
self, name=DEFAULT_NAME, vtype=DEFAULT_VTYPE, value=None,
self, name=DEFAULT_NAME, ptype=DEFAULT_PTYPE, value=None,
parser=parse, serializer=serialize, svalue=None,
conf=None, configurable=None,
local=DEFAULT_LOCAL, scope=DEFAULT_SCOPE, safe=DEFAULT_SAFE,
Expand All @@ -161,7 +162,7 @@ def __init__(
common properties (parser, value, conf, etc.).
Default is the regex ``.*``.
:param type vtype: parameter value type.
:param type ptype: parameter value type.
:param callable parser: param value deserializer which takes in param a
str. Default is the expression parser.
:param callable serializer: param serializer which takes in param an
Expand Down Expand Up @@ -189,7 +190,7 @@ def __init__(
# init public attributes
self.parser = parser
self.serializer = serializer
self.vtype = vtype
self.ptype = ptype
self.conf = conf
self.configurable = configurable
self.local = local
Expand Down Expand Up @@ -236,12 +237,12 @@ def __hash__(self):
return hash(self.name) + hash(Parameter)

def __repr__(self):
"""Display self name, value, svalue, vtype and error.
"""Display self name, value, svalue, ptype and error.
:rtype: str"""

return 'Parameter({0}, {1}, {2}, {3}, {4})'.format(
self.name, self._value, self._svalue, self.vtype, self.error
self.name, self._value, self._svalue, self.ptype, self.error
)

@property
Expand Down Expand Up @@ -326,7 +327,7 @@ def svalue(self, value):

def resolve(
self,
configurable=None, conf=None, scope=None, _type=None,
configurable=None, conf=None, scope=None, ptype=None,
parser=None, error=True, svalue=None,
safe=None, besteffort=None
):
Expand All @@ -341,7 +342,7 @@ def resolve(
:param Configuration conf: configuration to use for
cross-value resolution.
:param dict scope: variables to use for local expression evaluation.
:param type _type: return type. Default is this vtype.
:param type ptype: return type. Default is this ptype.
:param parser: specific parser to use. Default this parser.
:param bool error: raise an error if True (False by default).
:param bool safe: if True (default) resolve without builtins functions.
Expand All @@ -357,8 +358,8 @@ def resolve(

self._error = None # nonify error.

if _type is None:
_type = self.vtype
if ptype is None:
ptype = self.ptype

if parser is None: # init parser
parser = self.parser
Expand Down Expand Up @@ -389,7 +390,7 @@ def resolve(
try:
result = self._value = parser(
svalue=svalue, conf=conf,
configurable=configurable, _type=_type,
configurable=configurable, ptype=ptype,
scope=scope, safe=safe, besteffort=besteffort
)

Expand All @@ -411,7 +412,7 @@ def value(self):
calculate the new value from the serialized one.
:return: parameter value.
:raises: TypeError if serialized value is not an instance of self vtype
:raises: TypeError if serialized value is not an instance of self ptype
. ParserError if parsing step raised an error.
"""

Expand All @@ -437,20 +438,20 @@ def value(self, value):
If an error occured, it is stored in this error attribute.
:param value: new value to use. If input value is not an instance of
self.vtype, self error
:raises: TypeError if input value is not an instance of self vtype.
self.ptype, self error
:raises: TypeError if input value is not an instance of self ptype.
"""

if value is None or (
self.vtype is not None and isinstance(value, self.vtype)
self.ptype is not None and isinstance(value, self.ptype)
):
self._value = value

else:
# raise wrong type error
error = TypeError(
'Wrong value type of ({0}). {1} expected.'.format(
value, self.vtype
value, self.ptype
)
)
self._error = error
Expand All @@ -466,7 +467,7 @@ def copy(self, cleaned=False, *args, **kwargs):
"""

props = (
'name', 'parser', 'local', 'vtype', 'conf', 'scope', 'besteffort'
'name', 'parser', 'local', 'ptype', 'conf', 'scope', 'besteffort'
)
for prop in props:
kwargs.setdefault(prop, getattr(self, prop))
Expand Down

0 comments on commit 75293cf

Please sign in to comment.