Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_device_may_not_use_both__name_and_device_class(device_factory):
with pytest.raises(WrongUsage) as err:
device_factory.get_device(name='UNIX_LOCAL', device_class='moler.device.unixlocal', connection_desc={},
connection_hops={})
assert "Use either 'name' or 'device_class' parameter (not both)" in str(err.value)
def test_groups_without_match_object():
from moler.cmd import RegexHelper
regex_helper = RegexHelper()
with pytest.raises(WrongUsage) as exc:
regex_helper.groups()
assert "Nothing was matched before calling" in str(exc)
def test_cannot_create_more_objects():
with pytest.raises(WrongUsage):
Scheduler()
Scheduler()
def _get_parser(self):
parsers = {
"any": self._catch_any,
"all": self._catch_all,
"sequence": self._catch_sequence,
}
if self.match in parsers:
return parsers[self.match]
else:
self.set_exception(WrongUsage("'{}' is not supported. Possible choices: 'any', 'all' or 'sequence'".
format(self.match)))
def get_device(cls, name=None, device_class=None, connection_desc=None, connection_hops=None, initial_state=None,
establish_connection=True):
"""
Return connection instance of given io_type/variant.
:param name: name of device defined in configuration.
:param device_class: 'moler.device.unixlocal.UnixLocal', 'moler.device.unixremote.UnixRemote', ...
:param connection_desc: 'io_type' and 'variant' of device connection.
:param connection_hops: connection hops to create device SM.
:param initial_state: initial state for device e.g. UNIX_REMOTE.
:param establish_connection: True to open connection, False if it does not matter.
:return: requested device.
"""
if (not name) and (not device_class):
raise WrongUsage("Provide either 'name' or 'device_class' parameter (none given)")
if name and device_class:
raise WrongUsage("Use either 'name' or 'device_class' parameter (not both)")
with cls._lock_device:
dev = cls._get_device_without_lock(name=name, device_class=device_class, connection_desc=connection_desc,
connection_hops=connection_hops, initial_state=initial_state,
establish_connection=establish_connection)
return dev
def _raise_wrong_usage_of_wait_for(self, connection_observer):
import inspect # don't import if never raising this exception
(_, _, _, caller_name, _, _) = inspect.stack()[1]
(_, _, _, caller_caller_name, _, _) = inspect.stack()[2]
# Prefer to speak in observer API not runner API since user uses observers-API (runner is hidden)
user_call_name = caller_caller_name if caller_caller_name == 'await_done' else caller_name
err_msg = "Can't call {}() from 'async def' - it is blocking call".format(user_call_name)
err_msg += "\n observer = {}()".format(connection_observer.__class__.__name__)
err_msg += "\n observer.start()"
err_msg += "\nconsider using:"
err_msg += "\n await observer"
err_msg += "\ninstead of:"
err_msg += "\n observer.await_done()"
self.logger.error(msg=err_msg)
raise WrongUsage(err_msg)
def _create_scheduler(self, scheduler_type):
"""
:param scheduler_type: type of new scheduler: 'thread' or 'asyncio'
:return: instance of scheduler
"""
if self._scheduler_type == scheduler_type:
return self._scheduler
if scheduler_type == 'thread':
scheduler = MolerThreadScheduler()
elif scheduler_type == 'asyncio':
scheduler = MolerAsyncioScheduler()
else:
raise WrongUsage(
"Wrong value of 'scheduler_type': '{}'. Allowed are 'thread' or 'asyncio'".format(scheduler_type))
scheduler.start()
return scheduler
def _validate_start(self, *args, **kwargs):
# check base class invariants first
if self.done():
raise WrongUsage("You can't run same {} multiple times. It is already done.".format(self))
if not self.connection:
# only if we have connection we can expect some data on it
# at the latest "just before start" we need connection
raise NoConnectionProvided(self)
# ----------------------------------------------------------------------
# We intentionally do not check if connection is open here.
# In such case net result anyway will be failed/timeouted observer -
# - so, user will need to investigate "why".
# Checking connection state would benefit in early detection of:
# "error condition - no chance to succeed since connection is closed".
# However, drawback is a requirement on connection to have is_open() API
# We choose minimalistic dependency over better troubleshooting support.
# ----------------------------------------------------------------------
if self.timeout <= 0.0:
raise ConnectionObserverTimeout(self, self.timeout, "before run", "timeout is not positive value")
def connection_closed_handler(self):
"""
Called by Moler (ThreadedMolerConnection) when connection is closed.
:return: None
"""
if not self.done():
connection_name = self.get_logger_name()
msg = "'{}' is not done but connection '{}' is about to be closed.".format(self, connection_name)
ex = WrongUsage(msg)
self.set_exception(ex)
self.cancel()