You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5 KiB

  1. # coding: utf-8
  2. # Author: Joel Grand-Guillaume
  3. # Copyright 2011-2012 Camptocamp SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. def itersubclasses(cls, _seen=None):
  6. """
  7. itersubclasses(cls)
  8. Generator over all subclasses of a given class, in depth first order.
  9. >>> list(itersubclasses(int)) == [bool]
  10. True
  11. >>> class A(object): pass
  12. >>> class B(A): pass
  13. >>> class C(A): pass
  14. >>> class D(B,C): pass
  15. >>> class E(D): pass
  16. >>>
  17. >>> for cls in itersubclasses(A):
  18. ... print(cls.__name__)
  19. B
  20. D
  21. E
  22. C
  23. >>> # get ALL (new-style) classes currently defined
  24. >>> [cls.__name__ for cls in itersubclasses(object)] #doctest: +ELLIPSIS
  25. ['type', ...'tuple', ...]
  26. """
  27. if not isinstance(cls, type):
  28. raise TypeError('itersubclasses must be called with '
  29. 'new-style classes, not %.100r' % cls
  30. )
  31. if _seen is None:
  32. _seen = set()
  33. try:
  34. subs = cls.__subclasses__()
  35. except TypeError: # fails only when cls is type
  36. subs = cls.__subclasses__(cls)
  37. for sub in subs:
  38. if sub not in _seen:
  39. _seen.add(sub)
  40. yield sub
  41. for sub in itersubclasses(sub, _seen):
  42. yield sub
  43. def _get_erp_module_name(module_path):
  44. # see this PR for v9 https://github.com/odoo/odoo/pull/11084
  45. """ Extract the name of the Odoo module from the path of the
  46. Python module.
  47. Taken from Odoo server: ``openerp.models.MetaModel``
  48. The (Odoo) module name can be in the ``openerp.addons`` namespace
  49. or not. For instance module ``sale`` can be imported as
  50. ``openerp.addons.sale`` (the good way) or ``sale`` (for backward
  51. compatibility).
  52. """
  53. module_parts = module_path.split('.')
  54. if len(module_parts) > 2 and module_parts[:2] == ['openerp', 'addons']:
  55. module_name = module_parts[2]
  56. else:
  57. module_name = module_parts[0]
  58. return module_name
  59. def is_module_installed(env, module_name):
  60. """ Check if an Odoo addon is installed.
  61. :param module_name: name of the addon
  62. """
  63. # the registry maintains a set of fully loaded modules so we can
  64. # lookup for our module there
  65. return module_name in env.registry._init_modules
  66. def get_erp_module(cls_or_func):
  67. """ For a top level function or class, returns the
  68. name of the Odoo module where it lives.
  69. So we will be able to filter them according to the modules
  70. installation state.
  71. """
  72. return _get_erp_module_name(cls_or_func.__module__)