Skip to content

Commit

Permalink
:add: exprparser and remove old parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
b3j0f committed Nov 2, 2015
1 parent 4f00d0a commit 70549ab
Show file tree
Hide file tree
Showing 14 changed files with 932 additions and 498 deletions.
402 changes: 277 additions & 125 deletions b3j0f/conf/configurable/core.py

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions b3j0f/conf/configurable/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

__all__ = ['conf_paths', 'add_category']

from b3j0f.annotation import PrivateInterceptor, Annotation

from .core import Configurable
from ..model import Category
Expand Down Expand Up @@ -65,6 +66,17 @@ def _get_conf_paths(self, *args, **kwargs):
return add_conf_paths


class ClassConfiguration(Annotation):

def __init__(self, conf, unified=True, useclsconf=True, *args, **kwargs):

super(ClassConfiguration, self).__init__(*args, **kwargs)

def _set_target(self):

pass


def add_category(name, content=None, unified=True):
"""Add a category to a configurable configuration.
Expand Down Expand Up @@ -162,3 +174,31 @@ def add_conf(cls):
return cls

return add_conf


class Configuration(PrivateInterceptor):
"""Annotation dedicated to bind a configurable to all class instances."""

def __init__(
self, conf_paths=None, conf=None, store=False, configurable=None,
configurablecls=Configurable, confparams=None, *args, **kwargs
):

super(Configuration, self).__init__(*args, **kwargs)

self.confparams = {} if confparams is None else confparams

if configurable is None:
self.configurable = self.configurablecls(
store=store, conf_paths=conf_paths, conf=conf
)
else:
self.configurable = configurable

def _interception(self, joinpoint):

result = joinpoint.proceed()

self.configurable.to_configure.append(result)

return result
32 changes: 11 additions & 21 deletions b3j0f/conf/configurable/test/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from ...model.configuration import Configuration
from ...model.category import Category
from ...model.parameter import Parameter
from ...model.parser import intparser, floatparser
from ...model.parser import intparser, floatparser, strparser

from tempfile import NamedTemporaryFile

Expand All @@ -55,14 +55,14 @@ def setUp(self):
self.conf = Configuration(
Category(
'A',
Parameter('a', value='a'),
Parameter('a', value='a', parser=strparser),
Parameter('_', value=2, parser=intparser),
Parameter('error', parser=floatparser, svalue='error')
),
Category(
'B',
Parameter('a', value='b'),
Parameter('b', value='b')
Parameter('a', value='b', parser=strparser),
Parameter('b', value='b', parser=strparser)
)
)

Expand All @@ -71,9 +71,7 @@ def test_configuration_files(self):
configurable = Configurable()
configurable.conf_paths = self.conf_paths

self.assertEqual(
configurable.conf_paths,
self.conf_paths)
self.assertEqual(configurable.conf_paths, self.conf_paths)

configurable = Configurable(conf_paths=self.conf_paths)

Expand Down Expand Up @@ -180,9 +178,8 @@ def test_reconfigure(self):
self.assertTrue(self.configurable.auto_conf)

conf = Configuration(
Category(
'TEST',
Parameter('auto_conf', value=False)))
Category('TEST', Parameter('auto_conf', value=False))
)

self.configurable.configure(conf=conf)
self.assertFalse(self.configurable.auto_conf)
Expand Down Expand Up @@ -222,12 +219,7 @@ def __init__(self):

param = 'test'

conf = Configuration(
Category(
'TEST',
Parameter(param, value=True)
)
)
conf = Configuration(Category('TEST', Parameter(param, value=True)))

self.configurable.configure(conf=conf, to_configure=to_configure)
self.assertTrue(to_configure.test)
Expand All @@ -251,9 +243,7 @@ def __init__(self):

param = 'test'

conf = Configuration(
Category('TEST', Parameter(param, value=True))
)
conf = Configuration(Category('TEST', Parameter(param, value=True)))

configurable.configure(conf=conf, to_configure=to_configure)
self.assertTrue(to_configure.test)
Expand All @@ -262,9 +252,9 @@ def test_parser_inheritance(self):

class _Configurable(Configurable):

def _conf(self, *args, **kwargs):
def _clsconf(self, *args, **kwargs):

result = super(_Configurable, self)._conf(*args, **kwargs)
result = super(_Configurable, self)._clsconf(*args, **kwargs)

result += Category('PLOP')

Expand Down
4 changes: 3 additions & 1 deletion b3j0f/conf/driver/file/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def _set_category(self, conf_resource, category, logger, *args, **kwargs):

def _set_parameter(self, conf_resource, category, param, *args, **kwargs):

conf_resource.set(category.name, param.name, param.value)
svalue = param.value if param.svalue is None else param.svalue

conf_resource.set(category.name, param.name, param.svalue)

def _update_conf_resource(self, conf_resource, conf_path, *args, **kwargs):

Expand Down
4 changes: 3 additions & 1 deletion b3j0f/conf/driver/file/json_.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ def _set_category(self, conf_resource, category, *args, **kwargs):

def _set_parameter(self, conf_resource, category, param, *args, **kwargs):

conf_resource[category.name][param.name] = param.value
svalue = param.value if param.svalue is None else param.svalue

conf_resource[category.name][param.name] = svalue

def _update_conf_resource(self, conf_resource, conf_path, *args, **kwargs):

Expand Down
4 changes: 3 additions & 1 deletion b3j0f/conf/driver/file/test/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def setUp(self):

def get_conf_file(self):

return '/tmp/b3j0f{0}.conf'.format(self.__class__.__name__)
return '/tmp/b3j0f{0}.conf'.format(type(self).__name__)

def _remove(self):
try:
Expand Down Expand Up @@ -193,6 +193,8 @@ def test_configuration(self):
logger=self.logger
)

self.conf.resolve()

conf = self.manager.get_conf(
conf_path=self.conf_path,
conf=self.conf,
Expand Down

0 comments on commit 70549ab

Please sign in to comment.