core.util

immp.core.util.resolve_import(path)

Take the qualified name of a Python class, and return the physical class object.

Parameters
path (str) – Dotted Python class name, e.g. <module path>.<class name>.
Returns
Class object imported from module.
Return type
type
immp.core.util.pretty_str(cls)

Class decorator to provide a default __str__() based on the contents of __dict__.

immp.core.util.escape(raw, *chars)

Prefix special characters with backslashes, suitable for encoding in a larger string delimiting those characters.

Parameters
  • raw (str) – Unsafe input string.
  • chars (str list) – Control characters to be escaped.
Returns

Escaped input string.

Return type

str

immp.core.util.unescape(raw, *chars)

Inverse of escape(), remove backslashes escaping special characters.

Parameters
  • raw (str) – Escaped input string.
  • chars (str list) – Control characters to be unescaped.
Returns

Raw unescaped string.

Return type

str

class immp.core.util.Watchable(watch)

Bases: object

Container mixin to trigger a callback when its contents are changed.

classmethod unwrap(obj)

Recursively replace Watchable subclasses with their native equivalents.

Parameters
obj – Container type, or any value.
Returns
Unwrapped container, or the original value.
classmethod watch(*methods)

Class decorator for mixin users, to wrap methods that modify the underlying container, and should therefore trigger the callback when invoked.

Parameters
methods (str list) – List of method names to individually wrap.
class immp.core.util.WatchedDict(watch, initial, **kwargs)

Bases: dict, immp.core.util.Watchable

Watchable-enabled dict subclass. Lists or dictionaries added as items in this container will be wrapped automatically.

clear() → None. Remove all items from D.
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem() → (k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
class immp.core.util.WatchedList(watch, initial)

Bases: list, immp.core.util.Watchable

Watchable-enabled list subclass. Lists or dictionaries added as items in this container will be wrapped automatically.

clear() → None -- remove all items from L
pop([index]) → item -- remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove(value) → None -- remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

L.reverse() – reverse IN PLACE

sort(key=None, reverse=False) → None -- stable sort *IN PLACE*
class immp.core.util.ConfigProperty(cls=None, key=None)

Bases: object

Data descriptor to present config from Openable instances using the actual objects stored in a Host.

class immp.core.util.IDGen

Bases: object

Generator of generic timestamp-based identifiers.

IDs are guaranteed unique for the lifetime of the application – two successive calls will yield two different identifiers.

class immp.core.util.LocalFilter(name='')

Bases: logging.Filter

Logging filter that restricts capture to loggers within the immp namespace.

class immp.core.util.OpenState

Bases: enum.Enum

Readiness status for instances of Openable.

disabled

Not currently in use, and won’t be started by the host.

inactive

Hasn’t been started yet.

starting

Currently starting up (during Openable.start()).

active

Currently running.

stopping

Currently closing down (during Openable.stop()).

failed

Exception occurred during starting or stopping phases.

class immp.core.util.Configurable(name, config, host)

Bases: object

Superclass for objects managed by a Host and created using configuration.

schema

.Schema – Structure of the config expected by this configurable. If not customised by the subclass, it defaults to dict (that is, any dict structure is valid).

It may also be set to None, to declare that no configuration is accepted.

name

str – User-provided, unique name of the hook, used for config references.

config

dict – Reference to the user-provided configuration. Assigning to this field will validate the data against the class schema, which may raise exceptions on failure as defined by Schema.validate().

host

.Host – Controlling host instance, providing access to plugs.

class immp.core.util.Openable

Bases: object

Abstract class to provide open and close hooks. Subclasses should implement start() and stop(), whilst users should make use of open() and close().

state

.OpenState – Current status of this resource.

open()

Open this resource ready for use. Does nothing if already open, but raises RuntimeError if currently changing state.

start()

Perform any operations needed to open this resource.

close()

Close this resource after it’s used. Does nothing if already closed, but raises RuntimeError if currently changing state.

stop()

Perform any operations needed to close this resource.

disable()

Prevent this openable from being run by the host.

enable()

Restore normal operation of this openable.

class immp.core.util.HTTPOpenable(name, config, host)

Bases: immp.core.util.Openable

Template openable including a aiohttp.ClientSession instance for networking.

session

aiohttp.ClientSession – Managed session object.