Last active
December 13, 2015 19:40
-
-
Save agoose77/f9d84e69a9431fcb9525 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hive | |
class HiveInstantiator_: | |
def __init__(self, hive_cls): | |
self._environment_cls = hive_cls | |
self._plugins = {} | |
self._sockets = {} | |
self.last_created = None | |
def set_plugin(self, identifier, plugin): | |
self._plugins[identifier] = plugin | |
def set_socket(self, identifier, socket): | |
self._sockets[identifier] = socket | |
@classmethod | |
def plugin_setter(cls, identifier): | |
def wrapper(self, func): | |
self.set_plugin(identifier, func) | |
return wrapper | |
@classmethod | |
def socket_setter(cls, identifier): | |
def wrapper(self, func): | |
self.set_socket(identifier, func) | |
return wrapper | |
def instantiate(self): | |
environment = self._environment_cls(self._plugins, self._sockets) | |
self.last_created = environment.hive | |
class HiveEnvironment_: | |
def __init__(self, plugins, sockets): | |
self._plugins = plugins | |
self._sockets = sockets | |
@classmethod | |
def plugin_proxy(cls, identifier): | |
def wrapper(self, *args, **kwargs): | |
return self._plugins[identifier](*args, **kwargs) | |
return wrapper | |
@classmethod | |
def socket_proxy(cls, identifier): | |
def wrapper(self, func): | |
self._sockets[identifier](func) | |
return wrapper | |
from hive.classes import Method | |
def instantiator(hive_cls, plugins=[], sockets=[]): | |
def build_instantiator(cls, i, ex, args): | |
for index, (identifier, policy_cls) in enumerate(plugins): | |
method = Method(HiveInstantiator_, HiveInstantiator_.plugin_setter(identifier)) | |
socket = hive.socket(method, identifier=identifier) | |
setattr(ex, "socket_{}".format(index), socket) | |
for index, (identifier, policy_cls) in enumerate(sockets): | |
method = Method(HiveInstantiator_, HiveInstantiator_.socket_setter(identifier)) | |
plugin = hive.plugin(method, identifier=identifier) | |
setattr(ex, "plugin_{}".format(index), plugin) | |
i.create = hive.triggerable(cls.instantiate) | |
ex.create = hive.entry(i.create) | |
ex.last_created = hive.property(cls, "last_created") | |
i.last_created_out = hive.pull_out(ex.last_created) | |
ex.last_created_out = hive.output(i.last_created_out) | |
InstantiatorHive = hive.hive("Instantiator", build_instantiator, HiveInstantiator_) | |
def build_environment(cls, i, ex, args): | |
for index, (identifier, policy_cls) in enumerate(plugins): | |
method = Method(HiveEnvironment_, HiveEnvironment_.plugin_proxy(identifier)) | |
plugin = hive.plugin(method, identifier=identifier, policy_cls=policy_cls) | |
setattr(ex, "plugin_{}".format(index), plugin) | |
for index, (identifier, policy_cls) in enumerate(sockets): | |
method = Method(HiveEnvironment_, HiveEnvironment_.socket_proxy(identifier)) | |
socket = hive.socket(method, identifier=identifier, policy_cls=policy_cls) | |
setattr(ex, "socket_{}".format(index), socket) | |
ex.hive = hive_cls() | |
EnvironmentHive = hive.hive("Environment", build_environment, HiveEnvironment_) | |
return InstantiatorHive(EnvironmentHive) | |
################## DEMO BELOW ########################## | |
# Child hive | |
def build_child_hive(cls, i, ex, args): | |
ex.get_plugin = hive.socket(lambda p: print(p()), identifier=("some", "plugin")) | |
ex.get_register_func = hive.socket(cls.set_register_func, identifier=("add_client",)) | |
class _ChildCls: | |
def __init__(self): | |
pass | |
def set_register_func(self, func): | |
func(self) | |
ChildHive = hive.hive("SomeHive", build_child_hive, _ChildCls) | |
# A simple, intermediate factory | |
def build_intermediary(i, ex, args): | |
ex.instantiator = instantiator(ChildHive, plugins=[(("some", "plugin"), hive.plugins.SingleRequired), | |
(("add_client",), hive.plugins.SingleRequired)]) | |
IntermediaryHive = hive.hive("IntermediaryHive", build_intermediary) | |
# Top level hive | |
def build_main_hive(cls, i, ex, args): | |
ex.get_self = hive.plugin(cls.get_self, identifier=("some", "plugin")) | |
ex.add_client = hive.plugin(cls.add_client, identifier=("add_client",)) | |
ex.instantiator = instantiator(IntermediaryHive, plugins=[(("some", "plugin"), hive.plugins.SingleRequired), | |
(("add_client",), hive.plugins.SingleRequired)]) | |
class _MainCls: | |
def __init__(self): | |
self.clients = [] | |
def add_client(self, client): | |
self.clients.append(client) | |
print(self, self.clients) | |
def get_self(self): | |
return self | |
MainHive = hive.hive("MainHive", build_main_hive, _MainCls) | |
x = MainHive() | |
x.instantiator.create() | |
intermediary = x.instantiator.last_created | |
intermediary.instantiator.create() | |
intermediary.instantiator.create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment