Skip to content

Commit

Permalink
Merge branch 'devel' into rtfd
Browse files Browse the repository at this point in the history
  • Loading branch information
s-kostyuk committed Sep 12, 2017
2 parents c17dc15 + 26c7cd0 commit 9713e00
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 75 deletions.
10 changes: 5 additions & 5 deletions dpl/api/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Include DPL modules
from . import exceptions
from dpl.auth import AuthManager
from dpl.platforms import PlatformManager
from dpl.integrations import BindingManager
from dpl.core import Placement, PlacementManager
from dpl.utils import obj_to_dict
from dpl.things import Thing, Actuator
Expand All @@ -19,9 +19,9 @@ class ApiGateway(object):
and pass requests further to corresponding components (to execute some command
of fetch information about a specific thing, for example)
"""
def __init__(self, auth_manager: AuthManager, platform_manager: PlatformManager, placement_manager: PlacementManager):
def __init__(self, auth_manager: AuthManager, binding_manager: BindingManager, placement_manager: PlacementManager):
self._am = auth_manager
self._pm = platform_manager
self._bm = binding_manager
self._placements = placement_manager

def auth(self, username: str, password: str) -> str:
Expand Down Expand Up @@ -91,7 +91,7 @@ def get_things(self, token: str) -> List[Dict]:
self._check_permission(token, None)

result = list()
things = self._pm.fetch_all_things()
things = self._bm.fetch_all_things()

for thing in things:
result.append(self._thing_to_dict_legacy(thing))
Expand All @@ -110,7 +110,7 @@ def _get_thing(self, token: str, thing_id: str) -> Thing:
self._check_permission(token, None)

try:
thing = self._pm.fetch_thing(thing_id)
thing = self._bm.fetch_thing(thing_id)
except KeyError as e:
raise exceptions.ThingNotFoundError("Thing with the specified id was not found") from e

Expand Down
18 changes: 9 additions & 9 deletions dpl/core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
from dpl import api
from dpl import auth
from dpl.core import Configuration
from dpl.platforms import PlatformManager
from dpl.integrations import BindingManager
from dpl.core.placement_manager import PlacementManager


class Controller(object):
def __init__(self):
self._conf = Configuration(path="../samples/config")
self._placements = PlacementManager()
self._pm = PlatformManager()
self._bm = BindingManager()

self._auth_manager = auth.AuthManager()

self._api_gateway = api.ApiGateway(self._auth_manager, self._pm, self._placements)
self._api_gateway = api.ApiGateway(self._auth_manager, self._bm, self._placements)
self._rest_api = api.RestApi(self._api_gateway)

async def start(self):
Expand All @@ -31,13 +31,13 @@ async def start(self):

self._placements.init_placements(placement_settings)

enabled_platforms = core_settings["enabled_platforms"]
enabled_integrations = core_settings["enabled_integrations"]

self._pm.init_platforms(enabled_platforms)
self._pm.init_connections(connection_settings)
self._pm.init_things(thing_settings)
self._bm.init_integrations(enabled_integrations)
self._bm.init_connections(connection_settings)
self._bm.init_things(thing_settings)

self._pm.enable_all_things()
self._bm.enable_all_things()

# FIXME: Only for testing purposes
self._auth_manager.create_root_user("admin", "admin")
Expand All @@ -48,4 +48,4 @@ async def start(self):

async def shutdown(self):
await self._rest_api.shutdown_server()
self._pm.disable_all_things()
self._bm.disable_all_things()
4 changes: 2 additions & 2 deletions dpl/platforms/__init__.py → dpl/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from .connection_registry import ConnectionRegistry
from .thing_factory import ThingFactory
from .thing_registry import ThingRegistry
from .platform_manager import PlatformManager
from .binding_manager import BindingManager

__all__ = ['ConnectionFactory', 'ConnectionRegistry', 'ThingFactory', 'ThingRegistry',
'PlatformManager']
'BindingManager']

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
LOGGER = logging.getLogger(__name__)


# FIXME: CC11: Consider splitting of PlatformManager to ThingManager and ConnectionManager
# FIXME: CC11: Consider splitting of BindingManager to ThingManager and ConnectionManager
# FIXME: CC12: Consider splitting of Managers to Repositories and Loaders
class PlatformManager(object):
class BindingManager(object):
"""
PlatformManager is a class that is responsible for initiation, storage, fetching and deletion
BindingManager is a class that is responsible for initiation, storage, fetching and deletion
of Things and Connections.
"""
def __init__(self):
Expand All @@ -27,18 +27,18 @@ def __init__(self):
self._connections = dict() # type: Dict[str, Connection]
self._things = dict() # type: Dict[str, Thing]

def init_platforms(self, platform_names: List[str]) -> None:
def init_integrations(self, integration_names: List[str]) -> None:
"""
Load all enabled platforms from the specified list
Load all enabled integrations from the specified list
:param platform_names: a name of platforms to be loaded
:param integration_names: a name of integrations to be loaded
:return: None
"""
for item in platform_names:
for item in integration_names:
try:
importlib.import_module(name='.'+item, package="dpl.platforms")
importlib.import_module(name='.'+item, package="dpl.integrations")
except ImportError as e:
LOGGER.warning("Failed to load platform \"%s\": %s",
LOGGER.warning("Failed to load integration \"%s\": %s",
item, e)

def init_connections(self, config: List[Dict]) -> None:
Expand All @@ -50,7 +50,7 @@ def init_connections(self, config: List[Dict]) -> None:
"""
for item in config:
con_id = item["id"]
platform_name = item["platform"]
integration_name = item["integration"]
con_type = item["con_type"]
con_params = item["con_params"] # type: dict

Expand All @@ -63,8 +63,8 @@ def init_connections(self, config: List[Dict]) -> None:

if factory is None:
LOGGER.warning(
"Failed to create connection \"%s\". Is platform \"%s\" enabled?",
con_id, platform_name
"Failed to create connection \"%s\". Is integration \"%s\" enabled?",
con_id, integration_name
)

continue
Expand All @@ -84,15 +84,15 @@ def init_things(self, config: List[Dict]) -> None:
"""
for item in config:
thing_id = item["id"]
thing_platform = item["platform"]
thing_integration = item["integration"]
thing_type = item["type"]
thing_friendly_name = item.get("friendly_name", None)
thing_placement = item["placement"]
con_id = item["con_id"]
con_params = item["con_params"]

factory = ThingRegistry.resolve_factory( # type: ThingFactory
platform_name=thing_platform,
integration_name=thing_integration,
thing_type=thing_type,
default=None
)
Expand All @@ -109,8 +109,8 @@ def init_things(self, config: List[Dict]) -> None:

if factory is None:
LOGGER.warning(
"Failed to create thing \"%s\". Is platform \"%s\" enabled?",
thing_id, thing_platform
"Failed to create thing \"%s\". Is integration \"%s\" enabled?",
thing_id, thing_integration
)

continue
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
class ConnectionRegistry(object):
"""
ConnectionRegistry is a class that registers all connections, implemented
in specific platforms (dpl.platforms module), and returns a corresponding
in specific integrations (dpl.integrations module), and returns a corresponding
ConnectionFactory for building of instance of this connection.
"""
__registry = dict() # contains references to all factories

@classmethod
def register_factory(cls, connection_type: str, factory: ConnectionFactory) -> None:
# FIXME: CC10: Add additional platform argument?
# FIXME: CC10: Add additional integration argument?
"""
Register a factory for building of instance of corresponding connection type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Dummy platform contains a reference implementations of all thing types.
Dummy integration contains a reference implementations of all thing types.
Those reference implementations just print some data on display in a response
to sent commands and use DummyConnection connection type.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Include 3rd-party modules
# Include DPL modules
from dpl.connections import Connection
from dpl.platforms import ConnectionFactory, ConnectionRegistry
from dpl.integrations import ConnectionFactory, ConnectionRegistry


class DummyConnection(Connection):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Include DPL modules
from dpl.things import Player
from dpl.platforms import ThingFactory, ThingRegistry
from dpl.integrations import ThingFactory, ThingRegistry
from . import DummyConnection


Expand Down Expand Up @@ -124,7 +124,7 @@ def build(*args, **kwargs) -> DummyPlayer:


ThingRegistry.register_factory(
platform_name="dummy",
integration_name="dummy",
thing_type="player",
factory=DummyPlayerFactory()
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Include 3rd-party modules
# Include DPL modules
from dpl.things import Slider
from dpl.platforms import ThingFactory, ThingRegistry
from dpl.integrations import ThingFactory, ThingRegistry
from . import DummyConnection


Expand Down Expand Up @@ -135,7 +135,7 @@ def build(*args, **kwargs) -> DummySlider:


ThingRegistry.register_factory(
platform_name="dummy",
integration_name="dummy",
thing_type="slider",
factory=DummySliderFactory()
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Include DPL modules
from dpl.things import Switch
from dpl.platforms import ThingFactory, ThingRegistry
from dpl.integrations import ThingFactory, ThingRegistry
from . import DummyConnection


Expand Down Expand Up @@ -109,7 +109,7 @@ def build(*args, **kwargs) -> DummySwitch:


ThingRegistry.register_factory(
platform_name="dummy",
integration_name="dummy",
thing_type="switch",
factory=DummySwitchFactory()
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,52 @@
class ThingRegistry(object):
"""
ThingFactory is a class that registers all things, implemented
in specific platforms (dpl.platforms module), and returns a corresponding
in specific integrations (dpl.integrations module), and returns a corresponding
ThingFactory for building of instance of this thing.
"""
# contains references to all factories:
__registry = dict() # type: Dict['str', Dict['str', ThingFactory]]

@classmethod
def register_factory(cls, platform_name: str, thing_type: str, factory: ThingFactory) -> None:
def register_factory(cls, integration_name: str, thing_type: str, factory: ThingFactory) -> None:
"""
Register a factory for building of instance of corresponding thing type
:param platform_name: a name of platform to which this ThingFactory belongs
:param integration_name: a name of integration to which this ThingFactory belongs
:param thing_type: a name of thing type, for which thing factory is registered.
:param factory: an instance of thing factory that will be used for building
of those type of things.
:return: None
"""
platform_factories = cls.__registry.get(platform_name, dict())
platform_factories[thing_type] = factory
integration_factories = cls.__registry.get(integration_name, dict())
integration_factories[thing_type] = factory

cls.__registry[platform_name] = platform_factories
cls.__registry[integration_name] = integration_factories

@classmethod
def resolve_factory(cls, platform_name: str, thing_type: str, default: ThingFactory or None = None) -> ThingFactory or None:
def resolve_factory(cls, integration_name: str, thing_type: str, default: ThingFactory or None = None) -> ThingFactory or None:
"""
Returns an instance of ThingFactory that must be used for building of
specified type of things.
:param platform_name: a name of platform to which this ThingFactory belongs
:param integration_name: a name of integration to which this ThingFactory belongs
:param thing_type: a type of thing that ThingFactory is responsible for
:param default: object to be returned if related ThingFactory is not found
:return: an instance of ThingFactory or None if it's not found
"""
platform_factories = cls.__registry.get(platform_name, dict())
integration_factories = cls.__registry.get(integration_name, dict())

return platform_factories.get(thing_type, default)
return integration_factories.get(thing_type, default)

@classmethod
def remove_factory(cls, platform_name: str, thing_type: str) -> None:
def remove_factory(cls, integration_name: str, thing_type: str) -> None:
"""
Removes an instance of thing factory, that is associated with specified thing type
:param platform_name: a name of platform to which this ThingFactory belongs
:param integration_name: a name of integration to which this ThingFactory belongs
:param thing_type: a type of thing that ThingFactory was responsible for
:return: None
"""
platform_factories = cls.__registry.get(platform_name, dict())
integration_factories = cls.__registry.get(integration_name, dict())

platform_factories.pop(thing_type)
integration_factories.pop(thing_type)
2 changes: 1 addition & 1 deletion dpl/things/thing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Thing(object):
items, things that uses some specific protocol (connection) and can communicate
with the system in any way.
Specific implementations of things are grouped into 'platforms'.
Specific implementations of things are grouped into 'integrations'.
Every thing implementation must guarantee that:
Expand Down
4 changes: 2 additions & 2 deletions samples/config/connections/connections.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[
{
"id": "DC1",
"platform": "dummy",
"integration": "dummy",
"con_type": "dummy_connection",
"con_params": {}
},
{
"id": "DC2",
"platform": "dummy",
"integration": "dummy",
"con_type": "dummy_connection",
"con_params": {}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/config/core/core.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"enabled_platforms": ["dummy"],
"enabled_integrations": ["dummy"],
"debug": true
}
6 changes: 3 additions & 3 deletions samples/config/things/doors.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"id": "D1",
"platform": "dummy",
"integration": "dummy",
"type": "slider",
"friendly_name": "Entrance door",
"placement": "R1",
Expand All @@ -12,7 +12,7 @@
},
{
"id": "D2",
"platform": "dummy",
"integration": "dummy",
"type": "slider",
"friendly_name": "Bedroom door",
"placement": "R4",
Expand All @@ -23,7 +23,7 @@
},
{
"id": "D3",
"platform": "dummy",
"integration": "dummy",
"type": "slider",
"friendly_name": "Office door",
"placement": "R5",
Expand Down

0 comments on commit 9713e00

Please sign in to comment.