Skip to content

Commit

Permalink
Replaced deprecated code ; fixed the issue with EnvironmentVariable;
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyuki committed Feb 9, 2020
1 parent 3bf7aaa commit 2fde007
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
30 changes: 19 additions & 11 deletions imagination/decorator/config.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# v3
from abc import ABC, abstractproperty
from os import environ
from typing import Callable
from abc import ABC, abstractmethod
from os import environ, getenv
from typing import Callable, Optional, Any
from imagination.helper.id_naming import fully_qualified_class_name as default_id_naming_strategy


class AbstractParameter(ABC):
@abstractproperty
@property
@abstractmethod
def name(self):
raise NotImplementedError()
...

@abstractproperty
@property
@abstractmethod
def value(self):
raise NotImplementedError()
...


class Parameter(AbstractParameter):
""" Primitive-type Parameter """

def __init__(self, value, name=None):
self._value = value
self._name = name
Expand All @@ -35,10 +38,12 @@ def value(self):


class EnvironmentVariable(AbstractParameter):
def __init__(self, env:str, parse_value:Callable = None, default = None, name=None):
def __init__(self, env: str, parse_value: Callable = None, default: Any = None,
allow_default: Optional[bool] = None, name: str = None):
self._env = env
self._parse_value = parse_value
self._default = default
self._allow_default = allow_default or False
self._name = name

@property
Expand All @@ -51,7 +56,7 @@ def kind(self):

@property
def value(self):
value = environ[self._name]
value = getenv(self._env) if self._allow_default else environ[self._env]

if self._parse_value is not None:
value = self._parse_value(value)
Expand All @@ -69,7 +74,8 @@ class Service(AbstractParameter):
:param service_cls_or_id: Service class (type) or ID (string). When a class (type) is provided, it will try to
figure out by the default ID or ``primary``.
"""
def __init__(self, service_cls_or_id, name=None, id_naming_strategy:Callable = None):

def __init__(self, service_cls_or_id, name=None, id_naming_strategy: Callable = None):
self._service_cls_or_id = service_cls_or_id
self._id_naming_strategy = id_naming_strategy
self._name = name
Expand All @@ -87,12 +93,14 @@ def value(self):

return self._service_cls_or_id


class ClassInfo(AbstractParameter):
"""
Class/Type Parameter
:param fqcn: Fully qualified class name
"""

def __init__(self, fqcn, name=None):
self._fqcn = fqcn
self._name = name
Expand All @@ -103,4 +111,4 @@ def name(self):

@property
def value(self):
return self._fqcn
return self._fqcn
6 changes: 5 additions & 1 deletion imagination/decorator/helper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from .config import Parameter, Service, ClassInfo
from .config import Parameter, Service, ClassInfo, EnvironmentVariable


def set_parameter(definition, param):
if isinstance(param, Parameter):
definition.set_param(param.kind, param.value, param.name)
elif isinstance(param, EnvironmentVariable):
definition.set_param(param.kind, param.value, param.name)
elif isinstance(param, Service):
definition.add_dependency(param.value, param.name)
elif isinstance(param, ClassInfo):
definition.add_classinfo(param.value, param.name)
else:
raise NotImplementedError(f'Unabled to handle set_parameter({definition}, {param})')
8 changes: 4 additions & 4 deletions imagination/decorator/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from .helper import set_parameter


def registered(id:Optional[str] = None, params:Optional[List[Any]] = None, is_primary:Optional[bool] = False,
auto_wired:Optional[bool] = True, wiring_optional:Optional[bool] = False,
id_naming_strategy:Optional[Callable] = None):
def registered(id: Optional[str] = None, params: Optional[List[Any]] = None, is_primary: Optional[bool] = False,
auto_wired: Optional[bool] = True, wiring_optional: Optional[bool] = False,
id_naming_strategy: Optional[Callable] = None):
"""
Define the class as a service.
Expand Down Expand Up @@ -49,4 +49,4 @@ def inner_decorator(cls):

def __define_defintion(definition, params):
for param in (params or []):
set_parameter(definition, param)
set_parameter(definition, param)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name = 'imagination',
version = '3.1.0',
version = '3.2.0',
description = 'Reusable Component Framework',
author = 'Juti Noppornpitak',
author_email = 'juti_n@yahoo.co.jp',
Expand Down

0 comments on commit 2fde007

Please sign in to comment.