diff options
Diffstat (limited to 'module/plugins/hooks/MultiHome.py')
-rw-r--r-- | module/plugins/hooks/MultiHome.py | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/module/plugins/hooks/MultiHome.py b/module/plugins/hooks/MultiHome.py index 929ab9a25..b1e3c8ada 100644 --- a/module/plugins/hooks/MultiHome.py +++ b/module/plugins/hooks/MultiHome.py @@ -5,6 +5,42 @@ import time from module.plugins.internal.Addon import Addon +def get_request(pluginName, account=None): + iface = self.best_interface(pluginName, account) + + if not iface: + return + + iface.useFor(pluginName, account) + requestFactory.iface = lambda: iface.adress + self.log_debug("Using address", iface.adress) + + return oldGetRequest(pluginName, account) + + +class Interface(object): + + def __init__(self, adress): + self.adress = adress + self.history = {} + + + def last_plugin_access(self, pluginName, account): + if (pluginName, account) in self.history: + return self.history[(pluginName, account)] + else: + return 0 + + + def use_for(self, pluginName, account): + self.history[(pluginName, account)] = time.time() + + + def __repr__(self): + return "<Interface - %s>" % self.adress + + + class MultiHome(Addon): __name__ = "MultiHome" __type__ = "hook" @@ -42,44 +78,15 @@ class MultiHome(Addon): def activate(self): requestFactory = self.pyload.requestFactory - oldGetRequest = requestFactory.getRequest - - - def get_request(pluginName, account=None): - iface = self.best_interface(pluginName, account) - if iface: - iface.useFor(pluginName, account) - requestFactory.iface = lambda: iface.adress - self.log_debug("Using address", iface.adress) - return oldGetRequest(pluginName, account) - + oldGetRequest = requestFactory.getRequest requestFactory.getRequest = get_request def best_interface(self, pluginName, account): best = None + for interface in self.interfaces: if not best or interface.lastPluginAccess(pluginName, account) < best.lastPluginAccess(pluginName, account): best = interface - return best - - -class Interface(object): - - def __init__(self, adress): - self.adress = adress - self.history = {} - - def last_plugin_access(self, pluginName, account): - if (pluginName, account) in self.history: - return self.history[(pluginName, account)] - return 0 - - - def use_for(self, pluginName, account): - self.history[(pluginName, account)] = time.time() - - - def __repr__(self): - return "<Interface - %s>" % self.adress + return best |