From 938e0d457424236c7e3caaf49bdb411e150c7522 Mon Sep 17 00:00:00 2001 From: Jerome Berthier Date: Fri, 16 Aug 2019 15:12:11 +0200 Subject: [PATCH] weboob.browser.browser: allow multiple inheritance with AbstractBrowser When instanciating an AbstractBrowser the 'AbstractBrowser' class was actually replaced with the class defined in the PARENT attribute. This behavior did not allow multiple ineritance as the whole class __basess__ was replaced with the PARENT. Moreover when you try to inherit an AbstractBrowser from another AbstractBrowser, only the first AbstractBrowser class was replaced with the PARENT attribute. This patch fixes these behaviors by walking over the full parents tree to replace AbstractBrowser with the current class defined in the PARENT attributes. --- weboob/browser/browsers.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/weboob/browser/browsers.py b/weboob/browser/browsers.py index 163162528d..b3ac11903d 100644 --- a/weboob/browser/browsers.py +++ b/weboob/browser/browsers.py @@ -943,7 +943,7 @@ class AbstractBrowser(Browser): PARENT is a mandatory attribute, it's the name of the module providing the parent Browser - PARENT_ATTR is an optionnal attribute used when the parent module does not have only one + PARENT_ATTR is an optional attribute used when the parent module does not have only one browser defined as BROWSER class attribute: you can customized the path of the object to load. Note that you must pass a valid weboob instance as first argument of the constructor. @@ -951,9 +951,8 @@ class AbstractBrowser(Browser): PARENT = None PARENT_ATTR = None - def __new__(cls, *args, **kwargs): - weboob = kwargs['weboob'] - + @classmethod + def _resolve_abstract(cls, weboob): if cls.PARENT is None: raise AbstractBrowserMissingParentError("PARENT is not defined for browser %s" % cls) @@ -970,7 +969,15 @@ def __new__(cls, *args, **kwargs): if parent is None: raise AbstractBrowserMissingParentError("Failed to load parent class") + # Parent may be an AbstractBrowser as well + if hasattr(parent, '_resolve_abstract'): + parent._resolve_abstract(weboob) + cls.__bases__ = (parent,) + + def __new__(cls, *args, **kwargs): + weboob = kwargs['weboob'] + cls._resolve_abstract(weboob) return object.__new__(cls) -- GitLab