Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Environment wrappers refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Wierzbicki <awierzbicki@golem.network>
  • Loading branch information
Wiezzel committed Nov 7, 2019
1 parent b49093d commit 909a696
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 201 deletions.
8 changes: 4 additions & 4 deletions golem/envs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def supported(cls) -> EnvSupportStatus:
""" Is the Environment supported on this machine? """
raise NotImplementedError

@abstractmethod
def status(self) -> EnvStatus:
""" Get current status of the Environment. """
raise NotImplementedError
Expand All @@ -423,9 +424,8 @@ def run_benchmark(self) -> Deferred:
""" Get the general performance score for this environment. """
raise NotImplementedError

@classmethod
@abstractmethod
def parse_prerequisites(cls, prerequisites_dict: Dict[str, Any]) \
def parse_prerequisites(self, prerequisites_dict: Dict[str, Any]) \
-> Prerequisites:
""" Build Prerequisites struct from supplied dictionary. Returned value
is of appropriate type for calling install_prerequisites(). """
Expand All @@ -438,9 +438,8 @@ def install_prerequisites(self, prerequisites: Prerequisites) -> Deferred:
Returns boolean indicating whether installation was successful. """
raise NotImplementedError

@classmethod
@abstractmethod
def parse_config(cls, config_dict: Dict[str, Any]) -> EnvConfig:
def parse_config(self, config_dict: Dict[str, Any]) -> EnvConfig:
""" Build config struct from supplied dictionary. Returned value
is of appropriate type for calling update_config(). """
raise NotImplementedError
Expand All @@ -455,6 +454,7 @@ def update_config(self, config: EnvConfig) -> None:
""" Update configuration. Assumes current status is 'DISABLED'. """
raise NotImplementedError

@abstractmethod
def listen(
self,
event_type: EnvEventType,
Expand Down
192 changes: 0 additions & 192 deletions golem/envs/auto_setup.py

This file was deleted.

129 changes: 129 additions & 0 deletions golem/envs/wrappers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from typing import (
Any,
Dict,
Optional,
Tuple
)

from twisted.internet.defer import Deferred

from golem.envs import (
CounterId,
CounterUsage,
EnvConfig,
EnvEventListener,
EnvEventType,
Environment,
EnvStatus,
EnvSupportStatus,
Prerequisites,
Runtime,
RuntimeEventListener,
RuntimeEventType,
RuntimeInput,
RuntimeOutput,
RuntimePayload,
RuntimeStatus
)


class RuntimeWrapper(Runtime):
""" A no-op wrapper which proxies all calls to the wrapped Runtime.
Base class for implementing other wrappers. """

def __init__(self, runtime: Runtime) -> None:
self._runtime = runtime

def prepare(self) -> Deferred:
return self._runtime.prepare()

def clean_up(self) -> Deferred:
return self._runtime.clean_up()

def start(self) -> Deferred:
return self._runtime.start()

def wait_until_stopped(self) -> Deferred:
return self._runtime.wait_until_stopped()

def stop(self) -> Deferred:
return self._runtime.stop()

def status(self) -> RuntimeStatus:
return self._runtime.status()

def stdin(self, encoding: Optional[str] = None) -> RuntimeInput:
return self._runtime.stdin(encoding)

def stdout(self, encoding: Optional[str] = None) -> RuntimeOutput:
return self._runtime.stdout(encoding)

def stderr(self, encoding: Optional[str] = None) -> RuntimeOutput:
return self._runtime.stderr(encoding)

def get_port_mapping(self, port: int) -> Tuple[str, int]:
return self._runtime.get_port_mapping(port)

def usage_counters(self) -> Dict[CounterId, CounterUsage]:
return self._runtime.usage_counters()

def listen(
self,
event_type: RuntimeEventType,
listener: RuntimeEventListener
) -> None:
self._runtime.listen(event_type, listener)


class EnvironmentWrapper(Environment):

def __init__(self, env: Environment) -> None:
self._env = env

@classmethod
def supported(cls) -> EnvSupportStatus:
# This method should not be called on a wrapped environment.
raise AttributeError('Method not supported on a wrapped environment')

def status(self) -> EnvStatus:
return self._env.status()

def prepare(self) -> Deferred:
return self._env.prepare()

def clean_up(self) -> Deferred:
return self._env.clean_up()

def run_benchmark(self) -> Deferred:
return self._env.run_benchmark()

def parse_prerequisites(
self, prerequisites_dict: Dict[str, Any]
) -> Prerequisites:
return self._env.parse_prerequisites(prerequisites_dict)

def install_prerequisites(self, prerequisites: Prerequisites) -> Deferred:
return self._env.install_prerequisites(prerequisites)

def parse_config(self, config_dict: Dict[str, Any]) -> EnvConfig:
return self._env.parse_config(config_dict)

def config(self) -> EnvConfig:
return self._env.config()

def update_config(self, config: EnvConfig) -> None:
self._env.update_config(config)

def listen(
self,
event_type: EnvEventType,
listener: EnvEventListener
) -> None:
self._env.listen(event_type, listener)

def runtime(
self,
payload: RuntimePayload,
config: Optional[EnvConfig] = None
) -> Runtime:
return self._env.runtime(payload, config)
Loading

0 comments on commit 909a696

Please sign in to comment.