No Description
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.

plugins.py 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os.path
  4. import importlib
  5. import copy
  6. import json
  7. from warnings import warn #Because the logger is not able to work at some step
  8. from importlib.machinery import SourceFileLoader
  9. from lodel.context import LodelContext
  10. LodelContext.expose_modules(globals(), {
  11. 'lodel.logger': 'logger',
  12. 'lodel.settings.utils': ['SettingsError'],
  13. 'lodel.plugin.hooks': ['LodelHook'],
  14. 'lodel.plugin.exceptions': ['PluginError', 'PluginVersionError',
  15. 'PluginTypeError', 'LodelScriptError', 'DatasourcePluginError'],
  16. 'lodel.exceptions': ['LodelException', 'LodelExceptions',
  17. 'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
  18. ## @package lodel.plugins Lodel2 plugins management
  19. #@ingroup lodel2_plugins
  20. #
  21. # Lodel2 plugins are stored in directories
  22. # A typicall lodel2 plugin directory structure looks like :
  23. # - {{__init__.py}}} containing informations like full_name, authors, licence etc.
  24. # - main.py containing hooks registration etc
  25. # - confspec.py containing a configuration specification dictionary named CONFSPEC
  26. #
  27. # All plugins are expected to be found in multiple locations :
  28. # - in the lodel package (lodel.plugins)
  29. # - in the context directorie in a plugins/ dir (symlink to lodel.plugins) <-
  30. #this is obsolete now, since we enforce ALL plugins to be in the lodel package
  31. #
  32. #@todo Check if the symlink in the lodelcontext dir is obsolete !!!
  33. #@warning The plugins dir is at two locations : in lodel package and in
  34. #instance directory. Some stuff seems to still needs plugins to be in
  35. #the instance directory but it seems to be a really bad idea...
  36. ##@defgroup plugin_init_specs Plugins __init__.py specifications
  37. #@ingroup lodel2_plugins
  38. #@{
  39. ##@brief The package in which we will load plugins modules
  40. VIRTUAL_PACKAGE_NAME = 'lodel.plugins'
  41. ##@brief The temporary package to import python sources
  42. VIRTUAL_TEMP_PACKAGE_NAME = 'lodel.plugin_tmp'
  43. ##@brief Plugin init filename
  44. INIT_FILENAME = '__init__.py' # Loaded with settings
  45. ##@brief Name of the variable containing the plugin name
  46. PLUGIN_NAME_VARNAME = '__plugin_name__'
  47. ##@brief Name of the variable containing the plugin type
  48. PLUGIN_TYPE_VARNAME = '__plugin_type__'
  49. ##@brief Name of the variable containing the plugin version
  50. PLUGIN_VERSION_VARNAME = '__version__'
  51. ##@brief Name of the variable containing the confpsec filename
  52. CONFSPEC_FILENAME_VARNAME = '__confspec__'
  53. ##@brief Name of the variable containing the confspecs
  54. CONFSPEC_VARNAME = 'CONFSPEC'
  55. ##@brief Name of the variable containing the loader filename
  56. LOADER_FILENAME_VARNAME = '__loader__'
  57. ##@brief Name of the variable containing the plugin dependencies
  58. PLUGIN_DEPS_VARNAME = '__plugin_deps__'
  59. ##@brief Name of the optionnal activate method
  60. ACTIVATE_METHOD_NAME = '_activate'
  61. ##@brief Default & failover value for plugins path list
  62. if LodelContext.multisite():
  63. #When in multisite mode we fetch plugins list from context dir in the
  64. #lodel pkg symlink
  65. PLUGINS_PATH = os.path.join(LodelContext.context_dir(),'lodel/plugins')
  66. else:
  67. #When in monosite mode, for the moment, the plugins list is fetched from
  68. #a symlink in the instance dir
  69. PLUGINS_PATH = os.path.join(LodelContext.context_dir(),'plugins')
  70. ##@brief List storing the mandatory variables expected in a plugin __init__.py
  71. #file
  72. MANDATORY_VARNAMES = [PLUGIN_NAME_VARNAME, LOADER_FILENAME_VARNAME,
  73. PLUGIN_VERSION_VARNAME]
  74. ##@brief Default plugin type
  75. DEFAULT_PLUGIN_TYPE = 'extension' #Value found in lodel/plugin/extensions.py::Extensions._type_conf_name
  76. ## @}
  77. ##@brief Describe and handle version numbers
  78. #@ingroup lodel2_plugins
  79. #
  80. #A version number can be represented by a string like MAJOR.MINOR.PATCH
  81. #or by a list [MAJOR, MINOR,PATCH ].
  82. #
  83. #The class implements basics comparison function and string repr
  84. class PluginVersion(object):
  85. PROPERTY_LIST = ['major', 'minor', 'revision' ]
  86. ##@brief Version constructor
  87. #@param *args : You can either give a str that will be splitted on . or you
  88. #can give a iterable containing 3 integer or 3 arguments representing
  89. #major, minor and revision version
  90. def __init__(self, *args):
  91. self.__version = [0 for _ in range(3) ]
  92. if len(args) == 1:
  93. arg = args[0]
  94. if isinstance(arg, str):
  95. #Casting from string to version numbers
  96. spl = arg.split('.')
  97. invalid = False
  98. if len(spl) > 3:
  99. raise PluginVersionError("The string '%s' is not a valid plugin \
  100. version number" % arg)
  101. if len(spl) < 3:
  102. spl += [ 0 for _ in range(3-len(spl))]
  103. try:
  104. self.__version = [int(s) for s in spl]
  105. except (ValueError, TypeError):
  106. raise PluginVersionError("The string '%s' is not a valid lodel2 \
  107. plugin version number" % arg)
  108. else:
  109. try:
  110. if len(arg) >= 1:
  111. if len(arg) > 3:
  112. raise PluginVersionError("Expected maximum 3 value to \
  113. create a plugin version number but found '%s' as argument" % arg)
  114. for i, v in enumerate(arg):
  115. self.__version[i] = int(arg[i])
  116. except (TypeError, ValueError):
  117. raise PluginVersionError("Unable to convert argument into plugin \
  118. version number" % arg)
  119. elif len(args) > 3:
  120. raise PluginError("Expected between 1 and 3 positional arguments \
  121. but %d arguments found" % len(args))
  122. else:
  123. for i,v in enumerate(args):
  124. self.__version[i] = int(v)
  125. #Checks that version numbering is correct
  126. for v in self.__version:
  127. if v < 0:
  128. raise PluginVersionError("No negative version number allowed !")
  129. ##@brief Property to access major version number
  130. @property
  131. def major(self):
  132. return self.__version[0]
  133. ##@brief Property to access minor version number
  134. @property
  135. def minor(self):
  136. return self.__version[1]
  137. ##@brief Property to access patch version number
  138. @property
  139. def revision(self):
  140. return self.__version[2]
  141. ##@brief Check and prepare comparisoon argument
  142. #@return A PluginVersion instance
  143. #@throw PluginError if invalid argument provided
  144. def __cmp_check(self, other):
  145. if not isinstance(other, PluginVersion):
  146. try:
  147. if len(other) <= 3 and len(other) > 0:
  148. return PluginVersion(other)
  149. except TypeError:
  150. raise PluginError("Cannot compare argument '%s' with \
  151. a PluginVerison instance" % other)
  152. return other
  153. ##@brief Allow accessing to versions parts using interger index
  154. #@param key int : index
  155. #@return major for key == 0, minor for key == 1, revision for key == 2
  156. def __getitem__(self, key):
  157. try:
  158. key = int(key)
  159. except (ValueError, TypeError):
  160. raise ValueError("Expected an int as key")
  161. if key < 0 or key > 2:
  162. raise ValueError("Key is expected to be in [0..2]")
  163. return self.__version[key]
  164. def __lt__(self, other):
  165. for i in range(3):
  166. if self[i] < other[i]:
  167. return True
  168. elif self[i] > other[i]:
  169. return False
  170. return False
  171. def __eq__(self, other):
  172. for i in range(3):
  173. if self[i] != other[i]:
  174. return False
  175. return True
  176. def __gt__(self, other):
  177. for i in range(3):
  178. if self[i] > other[i]:
  179. return True
  180. elif self[i] < other[i]:
  181. return False
  182. return False
  183. def __le__(self, other):
  184. return self < other or self == other
  185. def __ne__(self, other):
  186. return not(self == other)
  187. def __ge__(self, other):
  188. return self > other or self == other
  189. def __str__(self):
  190. return '%d.%d.%d' % tuple(self.__version)
  191. def __repr__(self):
  192. return "{'major': %d, 'minor': %d, 'revision': %d}" % tuple(
  193. self.__version)
  194. ##@brief Plugin metaclass that allows to "catch" child class declaration
  195. #@ingroup lodel2_plugins
  196. #
  197. #Automatic script registration on child class declaration
  198. class MetaPlugType(type):
  199. ##@brief Dict storing all plugin types
  200. #
  201. #key is the _type_conf_name and value is the class
  202. _all_ptypes = dict()
  203. ##@brief type constructor reimplementation
  204. def __init__(self, name, bases, attrs):
  205. #Here we can store all child classes of Plugin
  206. super().__init__(name, bases, attrs)
  207. if len(bases) == 1 and bases[0] == object:
  208. return
  209. #Regitering a new plugin type
  210. MetaPlugType._all_ptypes[self._type_conf_name] = self
  211. ##@brief Accessor to the list of plugin types
  212. #@return A copy of _all_ptypes attribute (a dict with typename as key
  213. #and the class as value)
  214. @classmethod
  215. def all_types(cls):
  216. return copy.copy(cls._all_ptypes)
  217. ##@brief Accessor to the list of plugin names
  218. #@return a list of plugin name
  219. @classmethod
  220. def all_ptype_names(cls):
  221. return list(cls._all_ptypes.keys())
  222. ##@brief Given a plugin type name return a Plugin child class
  223. #@param ptype_name str : a plugin type name
  224. #@return A Plugin child class
  225. #@throw PluginError if ptype_name is not an exsiting plugin type name
  226. @classmethod
  227. def type_from_name(cls, ptype_name):
  228. if ptype_name not in cls._all_ptypes:
  229. raise PluginError("Unknown plugin type '%s'" % ptype_name)
  230. return cls._all_ptypes[ptype_name]
  231. ##@brief Call the clear classmethod on each child classes
  232. @classmethod
  233. def clear_cls(cls):
  234. for pcls in cls._all_ptypes.values():
  235. pcls.clear_cls()
  236. ##@brief Handle plugins
  237. #@ingroup lodel2_plugins
  238. #
  239. # An instance represent a loaded plugin. Class methods allow to load/preload
  240. # plugins.
  241. #
  242. # Typicall Plugins load sequence is :
  243. # 1. Settings call start method to instanciate all plugins found in confs
  244. # 2. Settings fetch all confspecs
  245. # 3. the loader call load_all to register hooks etc
  246. class Plugin(object, metaclass=MetaPlugType):
  247. ##@brief Stores Plugin instances indexed by name
  248. _plugin_instances = dict()
  249. ##@brief Attribute used by load_all and load methods to detect circular
  250. #dependencies
  251. _load_called = []
  252. ##@brief Attribute that stores plugins list from discover cache file
  253. _plugin_list = None
  254. #@brief Designed to store, in child classes, the confspec indicating \
  255. #where plugin list is stored
  256. _plist_confspecs = None
  257. ##@brief The name of the plugin type in the confguration
  258. #
  259. #None in abstract classes and implemented by child classes
  260. _type_conf_name = None
  261. ##@brief Stores virtual modules uniq key
  262. #@note When testing if a dir contains a plugin, if we reimport the __init__
  263. #in a module with the same name, all non existing value (plugin_type for
  264. #example) are replaced by previous plugin values
  265. _mod_cnt = 0
  266. ##@brief Plugin class constructor
  267. #
  268. # Called by setting in early stage of lodel2 boot sequence using classmethod
  269. # register
  270. #
  271. # @param plugin_name str : plugin name
  272. # @throw PluginError
  273. def __init__(self, plugin_name):
  274. ##@brief The plugin name
  275. self.name = plugin_name
  276. ##@brief The plugin package path
  277. self.path = self.plugin_path(plugin_name)
  278. ##@brief Stores the plugin module
  279. self.module = None
  280. ##@brief Stores the plugin loader module
  281. self.__loader_module = None
  282. ##@brief The plugin confspecs
  283. self.__confspecs = dict()
  284. ##@brief Boolean flag telling if the plugin is loaded or not
  285. self.loaded = False
  286. # Importing __init__.py infos in it
  287. plugin_module = self.module_name()
  288. self.module = LodelContext.module(plugin_module)
  289. # loading confspecs
  290. try:
  291. # Loading confspec directly from __init__.py
  292. self.__confspecs = getattr(self.module, CONFSPEC_VARNAME)
  293. except AttributeError:
  294. # Loading file in __confspec__ var in __init__.py
  295. try:
  296. module = self._import_from_init_var(CONFSPEC_FILENAME_VARNAME)
  297. except AttributeError:
  298. msg = "Malformed plugin {plugin} . No {varname} not {filevar} found in __init__.py"
  299. msg = msg.format(
  300. plugin = self.name,
  301. varname = CONFSPEC_VARNAME,
  302. filevar = CONFSPEC_FILENAME_VARNAME)
  303. raise PluginError(msg)
  304. except ImportError as e:
  305. msg = "Broken plugin {plugin} : {expt}"
  306. msg = msg.format(
  307. plugin = self.name,
  308. expt = str(e))
  309. raise PluginError(msg)
  310. except Exception as e:
  311. msg = "Plugin '%s' :"+str(e)
  312. raise e.__class__(msg)
  313. try:
  314. # loading confpsecs from file
  315. self.__confspecs = getattr(module, CONFSPEC_VARNAME)
  316. except AttributeError:
  317. msg = "Broken plugin. {varname} not found in '{filename}'"
  318. msg = msg.format(
  319. varname = CONFSPEC_VARNAME,
  320. filename = confspec_filename)
  321. raise PluginError(msg)
  322. # loading plugin version
  323. try:
  324. #this try block should be useless. The existance of
  325. #PLUGIN_VERSION_VARNAME in init file is mandatory
  326. self.__version = getattr(self.module, PLUGIN_VERSION_VARNAME)
  327. except AttributeError:
  328. msg = "Error that should not append while loading plugin '%s': no \
  329. %s found in plugin init file. Malformed plugin"
  330. msg %= (plugin_name, PLUGIN_VERSION_VARNAME)
  331. raise LodelFatalError(msg)
  332. # Load plugin type
  333. try:
  334. self.__type = getattr(self.module, PLUGIN_TYPE_VARNAME)
  335. except AttributeError:
  336. self.__type = DEFAULT_PLUGIN_TYPE
  337. self.__type = str(self.__type).lower()
  338. if self.__type not in MetaPlugType.all_ptype_names():
  339. raise PluginError("Unknown plugin type '%s'" % self.__type)
  340. # Load plugin name from init file (just for checking)
  341. try:
  342. #this try block should be useless. The existance of
  343. #PLUGIN_NAME_VARNAME in init file is mandatory
  344. pname = getattr(self.module, PLUGIN_NAME_VARNAME)
  345. except AttributeError:
  346. msg = "Error that should not append : no %s found in plugin \
  347. init file. Malformed plugin"
  348. msg %= PLUGIN_NAME_VARNAME
  349. raise LodelFatalError(msg)
  350. if pname != plugin_name:
  351. msg = "Plugin's discover cache inconsistency detected ! Cached \
  352. name differ from the one found in plugin's init file"
  353. raise PluginError(msg)
  354. ##@brief Try to import a file from a variable in __init__.py
  355. #@param varname str : The variable name
  356. #@return loaded module
  357. #@throw AttributeError if varname not found
  358. #@throw ImportError if the file fails to be imported
  359. #@throw PluginError if the filename was not valid
  360. #@todo Some strange things append :
  361. #when loading modules in test self.module.__name__ does not contains
  362. #the package... but in prod cases the self.module.__name__ is
  363. #the module fullname... Just a reminder note to explain the dirty
  364. #if on self_modname
  365. def _import_from_init_var(self, varname):
  366. # Read varname
  367. try:
  368. filename = getattr(self.module, varname)
  369. except AttributeError:
  370. msg = "Malformed plugin {plugin}. No {varname} found in __init__.py"
  371. msg = msg.format(
  372. plugin = self.name,
  373. varname = LOADER_FILENAME_VARNAME)
  374. raise PluginError(msg)
  375. #Path are not allowed
  376. if filename != os.path.basename(filename):
  377. msg = "Invalid {varname} content : '{fname}' for plugin {name}"
  378. msg = msg.format(
  379. varname = varname,
  380. fname = filename,
  381. name = self.name)
  382. raise PluginError(msg)
  383. #See the todo
  384. if len(self.module.__name__.split('.')) == 1:
  385. self_modname = self.module.__package__
  386. else:
  387. self_modname = self.module.__name__
  388. #extract module name from filename
  389. base_mod = '.'.join(filename.split('.')[:-1])
  390. module_name = self_modname+"."+base_mod
  391. return importlib.import_module(module_name)
  392. ##@brief Return associated module name
  393. def module_name(self):
  394. #WOOT WE LOVE DIRTY WORKAROUNDS !!!
  395. return "lodel.plugins."+os.path.basename(self.path)
  396. #END OF DIRTY WORKAROUND
  397. if not self.path.startswith('./plugins'):
  398. raise PluginError("Bad path for plugin %s : %s" % (
  399. self.name, self.path))
  400. mod_name = ''
  401. pathbuff = self.path
  402. while pathbuff != '.':
  403. mod_name = os.path.basename(pathbuff) + '.' + mod_name
  404. pathbuff = os.path.dirname(pathbuff)
  405. #removing trailing '.' and add leading lodel.
  406. return 'lodel.'+mod_name[:-1]
  407. ##@brief Check dependencies of plugin
  408. #@return A list of plugin name to be loaded before
  409. def check_deps(self):
  410. try:
  411. res = getattr(self.module, PLUGIN_DEPS_VARNAME)
  412. except AttributeError:
  413. return list()
  414. result = list()
  415. errors = list()
  416. for plugin_name in res:
  417. try:
  418. result.append(self.get(plugin_name))
  419. except PluginError:
  420. errors.append(plugin_name)
  421. if len(errors) > 0:
  422. raise PluginError( "Bad dependencie for '%s' :"%self.name,
  423. ', '.join(errors))
  424. return result
  425. ##@brief Check if the plugin should be activated
  426. #
  427. #Try to fetch a function called @ref ACTIVATE_METHOD_NAME in __init__.py
  428. #of a plugin. If none found assert that the plugin can be loaded, else
  429. #the method is called. If it returns anything else that True, the plugin
  430. #is noted as not activable
  431. #
  432. # @note Maybe we have to exit everything if a plugin cannot be loaded...
  433. def activable(self):
  434. try:
  435. test_fun = getattr(self.module, ACTIVATE_METHOD_NAME)
  436. except AttributeError:
  437. msg = "No %s method found for plugin %s. Assuming plugin is ready to be loaded"
  438. msg %= (ACTIVATE_METHOD_NAME, self.name)
  439. logger.debug(msg)
  440. test_fun = lambda:True
  441. return test_fun()
  442. ret = False
  443. try:
  444. ret = test_fun()
  445. except Exception as e:
  446. logger.error("The plugin %s failed to activate raising an \
  447. exception : %s" % (self.name, e))
  448. return ret
  449. ##@brief Load a plugin
  450. #
  451. #Loading a plugin means importing a file. The filename is defined in the
  452. #plugin's __init__.py file in a LOADER_FILENAME_VARNAME variable.
  453. #
  454. #The loading process has to take care of other things :
  455. #- loading dependencies (other plugins)
  456. #- check that the plugin can be activated using Plugin.activate() method
  457. #- avoid circular dependencies infinite loop
  458. def _load(self):
  459. if self.loaded:
  460. return
  461. #Test that plugin "wants" to be activated
  462. activable = self.activable()
  463. if not(activable is True):
  464. msg = "Plugin %s is not activable : %s"
  465. msg %= (self.name, activable)
  466. raise PluginError(msg)
  467. #Circular dependencie detection
  468. if self.name in self._load_called:
  469. raise PluginError("Circular dependencie in Plugin detected. Abording")
  470. else:
  471. self._load_called.append(self.name)
  472. #Dependencie load
  473. for dependencie in self.check_deps():
  474. activable = dependencie.activable()
  475. if activable is True:
  476. dependencie._load()
  477. else:
  478. msg = "Plugin {plugin_name} not activable because it depends on plugin {dep_name} that is not activable : {reason}"
  479. msg = msg.format(
  480. plugin_name = self.name,
  481. dep_name = dependencie.name,
  482. reason = activable)
  483. #Loading the plugin
  484. try:
  485. self.__loader_module = self._import_from_init_var(LOADER_FILENAME_VARNAME)
  486. except PluginError as e:
  487. raise e
  488. except ImportError as e:
  489. msg = "Broken plugin {plugin} : {expt}"
  490. msg = msg.format(
  491. plugin = self.name,
  492. expt = str(e))
  493. raise PluginError(msg)
  494. logger.debug("Plugin '%s' loaded" % self.name)
  495. self.loaded = True
  496. ##@brief Returns the loader module
  497. #
  498. #Accessor for the __loader__ python module
  499. def loader_module(self):
  500. if not self.loaded:
  501. raise RuntimeError("Plugin %s not loaded yet."%self.name)
  502. return self.__loader_module
  503. def __str__(self):
  504. return "<LodelPlugin '%s' version %s>" % (self.name, self.__version)
  505. ##@brief Call load method on every pre-loaded plugins
  506. #
  507. # Called by loader to trigger hooks registration.
  508. # This method have to avoid circular dependencies infinite loops. For this
  509. # purpose a class attribute _load_called exists.
  510. # @throw PluginError
  511. @classmethod
  512. def load_all(cls):
  513. errors = dict()
  514. cls._load_called = []
  515. for name, plugin in cls._plugin_instances.items():
  516. try:
  517. plugin._load()
  518. except PluginError as e:
  519. errors[name] = e
  520. if len(errors) > 0:
  521. msg = "Errors while loading plugins :"
  522. for name, e in errors.items():
  523. msg += "\n\t%20s : %s" % (name,e)
  524. msg += "\n"
  525. raise PluginError(msg)
  526. LodelHook.call_hook(
  527. "lodel2_plugins_loaded", cls, cls._plugin_instances)
  528. ##@return a copy of __confspecs attr
  529. @property
  530. def confspecs(self):
  531. return copy.copy(self.__confspecs)
  532. ##@brief Accessor to confspec indicating where we can find the plugin list
  533. #@note Abtract method implemented only for Plugin child classes
  534. #This attribute indicate where we fetch the plugin list.
  535. @classmethod
  536. def plist_confspecs(cls):
  537. if cls._plist_confspecs is None:
  538. raise LodelFatalError('Unitialized _plist_confspecs attribute for \
  539. %s' % cls.__name__)
  540. return copy.copy(cls._plist_confspecs)
  541. ##@brief Retrieves plugin list confspecs
  542. #
  543. #This method ask for each Plugin child class the confspecs specifying where
  544. #the wanted plugin list is stored. (For example DatasourcePlugin expect
  545. #that a list of ds plugin to load stored in lodel2 section, datasources key
  546. # etc...
  547. @classmethod
  548. def plugin_list_confspec(cls):
  549. LodelContext.expose_modules(globals(), {
  550. 'lodel.settings.validator': ['confspec_append']})
  551. res = dict()
  552. for pcls in cls.plugin_types():
  553. plcs = pcls.plist_confspec()
  554. confspec_append(res, plcs)
  555. return res
  556. ##@brief Register a new plugin
  557. #
  558. #@param plugin_name str : The plugin name
  559. #@return a Plugin instance
  560. #@throw PluginError
  561. @classmethod
  562. def register(cls, plugin_name):
  563. if plugin_name in cls._plugin_instances:
  564. msg = "Plugin allready registered with same name %s"
  565. msg %= plugin_name
  566. raise PluginError(msg)
  567. #Here we check that previous discover found a plugin with that name
  568. pdcache = cls.discover()
  569. if plugin_name not in pdcache:
  570. raise PluginError("No plugin named '%s' found" % plugin_name)
  571. ptype = pdcache[plugin_name]['type']
  572. pcls = MetaPlugType.type_from_name(ptype)
  573. plugin = pcls(plugin_name)
  574. cls._plugin_instances[plugin_name] = plugin
  575. logger.debug("Plugin %s available." % plugin)
  576. return plugin
  577. ##@brief Plugins instances accessor
  578. #
  579. #@param plugin_name str: The plugin name
  580. #@return a Plugin instance
  581. #@throw PluginError if plugin not found
  582. @classmethod
  583. def get(cls, plugin_name):
  584. try:
  585. return cls._plugin_instances[plugin_name]
  586. except KeyError:
  587. msg = "No plugin named '%s' loaded"
  588. msg %= plugin_name
  589. raise PluginError(msg)
  590. ##@brief Given a plugin name returns the plugin path
  591. # @param plugin_name str : The plugin name
  592. # @return the plugin directory path
  593. @classmethod
  594. def plugin_path(cls, plugin_name):
  595. plist = cls.plugin_list()
  596. if plugin_name not in plist:
  597. raise PluginError("No plugin named '%s' found" % plugin_name)
  598. try:
  599. return cls.get(plugin_name).path
  600. except PluginError:
  601. pass
  602. return plist[plugin_name]['path']
  603. ##@brief Return the plugin module name
  604. #
  605. #This module name is the "virtual" module where we imported the plugin.
  606. #
  607. #Typically composed like VIRTUAL_PACKAGE_NAME.PLUGIN_NAME
  608. #@warning Brokes subdire feature
  609. #@param plugin_name str : a plugin name
  610. #@return a string representing a module name
  611. #@todo fix broken subdir capabilitie ( @see module_name() )
  612. #@todo check if used, else delete it
  613. @classmethod
  614. def plugin_module_name(cls, plugin_name):
  615. return "%s.%s" % (VIRTUAL_PACKAGE_NAME, plugin_name)
  616. ##@brief Start the Plugin class
  617. #
  618. # Called by Settings.__bootstrap()
  619. #
  620. # This method load path and preload plugins
  621. @classmethod
  622. def start(cls, plugins):
  623. for plugin_name in plugins:
  624. cls.register(plugin_name)
  625. ##@brief Attempt to "restart" the Plugin class
  626. @classmethod
  627. def clear(cls):
  628. if cls._plugin_instances != dict():
  629. cls._plugin_instances = dict()
  630. if cls._load_called != []:
  631. cls._load_called = []
  632. MetaPlugType.clear_cls()
  633. ##@brief Designed to be implemented by child classes
  634. @classmethod
  635. def clear_cls(cls):
  636. pass
  637. ##@brief Reccursively walk throught paths to find plugin, then stores
  638. #found plugin in a static var
  639. #
  640. #Found plugins are stored in cls._plugin_list
  641. #@note The discover is run only if no cached datas are found
  642. #@return a list of dict with plugin infos { see @ref _discover }
  643. #@todo add max_depth and no symlink following feature
  644. @classmethod
  645. def discover(cls):
  646. if cls._plugin_list is not None:
  647. return cls._plugin_list
  648. logger.info("Running plugin discover")
  649. tmp_res = cls._discover(PLUGINS_PATH)
  650. #Formating and dedoubloning result
  651. result = dict()
  652. for pinfos in tmp_res:
  653. pname = pinfos['name']
  654. if ( pname in result
  655. and pinfos['version'] > result[pname]['version'])\
  656. or pname not in result:
  657. result[pname] = pinfos
  658. else:
  659. #dropped
  660. pass
  661. cls._plugin_list = result
  662. return result
  663. ##@brief Return discover result
  664. #@param refresh bool : if true invalidate all plugin list cache
  665. #@note If discover cache file not found run discover first
  666. #@note if refresh is set to True discover MUST have been run at least
  667. #one time. In fact refresh action load the list of path to explore
  668. #from the plugin's discover cache
  669. @classmethod
  670. def plugin_list(cls, refresh = False):
  671. return cls._plugin_list
  672. ##@brief Return a list of child Class Plugin
  673. @classmethod
  674. def plugin_types(cls):
  675. return MetaPlugType.all_types()
  676. ##@brief Check if a directory is a plugin module
  677. #@param path str : path to check
  678. #@param assert_in_package bool : if False didn't check that path is
  679. #a subdir of PLUGINS_PATH
  680. #@return a dict with name, version and path if path is a plugin module, else False
  681. @classmethod
  682. def dir_is_plugin(cls, path, assert_in_package = True):
  683. log_msg = "%s is not a plugin directory because : " % path
  684. if assert_in_package:
  685. #Check that path is a subdir of PLUGINS_PATH
  686. abspath = os.path.abspath(path)
  687. if not abspath.startswith(os.path.abspath(PLUGINS_PATH)):
  688. raise PluginError(
  689. "%s is not a subdir of %s" % log_msg, PLUGINS_PATH)
  690. #Checks that path exists
  691. if not os.path.isdir(path):
  692. raise ValueError(
  693. "Expected path to be a directory, but '%s' found" % path)
  694. #Checks that path contains plugin's init file
  695. initfile = os.path.join(path, INIT_FILENAME)
  696. if not os.path.isfile(initfile):
  697. log_msg += "'%s' not found" % (INIT_FILENAME)
  698. logger.debug(log_msg)
  699. return False
  700. #Importing plugin's init file to check contained datas
  701. try:
  702. initmod, modname = cls.import_init(path)
  703. except PluginError as e:
  704. log_msg += "unable to load '%s'. Exception raised : %s"
  705. log_msg %= (INIT_FILENAME, e)
  706. logger.warning(log_msg)
  707. warn(log_msg)
  708. return False
  709. #Checking mandatory init module variables
  710. for attr_name in MANDATORY_VARNAMES:
  711. if not hasattr(initmod,attr_name):
  712. log_msg += " mandatory variable '%s' not found in '%s'"
  713. log_msg %= (attr_name, INIT_FILENAME)
  714. logger.warning(log_msg)
  715. warn(log_msg)
  716. return False
  717. #Fetching plugin's version
  718. try:
  719. pversion = getattr(initmod, PLUGIN_VERSION_VARNAME)
  720. except (NameError, AttributeError) as e:
  721. msg = "Invalid plugin version found in %s : %s"
  722. msg %= (path, e)
  723. raise PluginError(msg)
  724. #Fetching plugin's type
  725. try:
  726. ptype = getattr(initmod, PLUGIN_TYPE_VARNAME)
  727. except (NameError, AttributeError) as e:
  728. ptype = DEFAULT_PLUGIN_TYPE
  729. pname = getattr(initmod, PLUGIN_NAME_VARNAME)
  730. return {'name': pname,
  731. 'version': PluginVersion(pversion),
  732. 'path': path,
  733. 'type': ptype}
  734. ##@brief Import init file from a plugin path
  735. #@param path str : Directory path
  736. #@return a tuple (init_module, module_name)
  737. #@todo replace by LodelContext usage !!! (not mandatory, this fun
  738. #is only used in plugin discover method)
  739. @classmethod
  740. def import_init(cls, path):
  741. cls._mod_cnt += 1 # in order to ensure module name unicity
  742. init_source = os.path.join(path, INIT_FILENAME)
  743. temp_module = '%s.%s.%s%d' % (
  744. VIRTUAL_TEMP_PACKAGE_NAME, os.path.basename(os.path.dirname(path)),
  745. 'test_init', cls._mod_cnt)
  746. try:
  747. loader = SourceFileLoader(temp_module, init_source)
  748. except (ImportError, FileNotFoundError) as e:
  749. raise PluginError("Unable to import init file from '%s' : %s" % (
  750. temp_module, e))
  751. try:
  752. res_module = loader.load_module()
  753. except Exception as e:
  754. raise PluginError("Unable to import initfile : %s" % e)
  755. return (res_module, temp_module)
  756. @classmethod
  757. def debug_wrapper(cls, updglob = None):
  758. if updglob is not None:
  759. for k, v in updglob.items():
  760. globals()[k] = v
  761. print(logger)
  762. ##@brief Reccursiv plugin discover given a path
  763. #@param path str : the path to walk through
  764. #@return A dict with plugin_name as key and {'path':..., 'version':...} as value
  765. @classmethod
  766. def _discover(cls, path):
  767. #Ensure plugins symlink creation
  768. LodelContext.expose_modules(globals(), {
  769. 'lodel.plugins': 'plugins'})
  770. res = []
  771. to_explore = [path]
  772. while len(to_explore) > 0:
  773. cur_path = to_explore.pop()
  774. for f in os.listdir(cur_path):
  775. f_path = os.path.join(cur_path, f)
  776. if f not in ['.', '..'] and os.path.isdir(f_path):
  777. #Check if it is a plugin directory
  778. test_result = cls.dir_is_plugin(f_path)
  779. if not (test_result is False):
  780. logger.info("Plugin '%s' found in %s" % (
  781. test_result['name'],f_path))
  782. res.append(test_result)
  783. else:
  784. to_explore.append(f_path)
  785. return res
  786. def debug_wrapper_mod():
  787. print("MOD : ",logger)
  788. ##@brief Decorator class designed to allow plugins to add custom methods
  789. #to LeObject childs (dyncode objects)
  790. #@ingroup lodel2_plugins
  791. #
  792. class CustomMethod(object):
  793. ##@brief Stores registered custom methods
  794. #
  795. #Key = LeObject child class name
  796. #Value = CustomMethod instance
  797. _custom_methods = dict()
  798. INSTANCE_METHOD = 0
  799. CLASS_METHOD = 1
  800. STATIC_METHOD = 2
  801. ##@brief Decorator constructor
  802. #@param component_name str : the name of the component to enhance
  803. #@param method_name str : the name of the method to inject (if None given
  804. #@param method_type int : take value in one of
  805. #CustomMethod::INSTANCE_METHOD CustomMethod::CLASS_METHOD or
  806. #CustomMethod::STATIC_METHOD
  807. #use the function name
  808. def __init__(self, component_name, method_name = None, method_type=0):
  809. ##@brief The targeted LeObject child class
  810. self._comp_name = component_name
  811. ##@brief The method name
  812. self._method_name = method_name
  813. ##@brief The function (that will be the injected method)
  814. self._fun = None
  815. ##@brief Stores the type of method (instance, class or static)
  816. self._type = int(method_type)
  817. if self._type not in (self.INSTANCE_METHOD, self.CLASS_METHOD,\
  818. self.STATIC_METHOD):
  819. raise ValueError("Excepted value for method_type was one of \
  820. CustomMethod::INSTANCE_METHOD CustomMethod::CLASS_METHOD or \
  821. CustomMethod::STATIC_METHOD, but got %s" % self._type)
  822. ##@brief called just after __init__
  823. #@param fun function : the decorated function
  824. def __call__(self, fun):
  825. if self._method_name is None:
  826. self._method_name = fun.__name__
  827. if self._comp_name not in self._custom_methods:
  828. self._custom_methods[self._comp_name] = list()
  829. if self._method_name in [ scm._method_name for scm in self._custom_methods[self._comp_name]]:
  830. raise RuntimeError("A method named %s allready registered by \
  831. another plugin : %s" % (
  832. self._method_name,
  833. self._custom_methods[self._comp_name].__module__))
  834. self._fun = fun
  835. self._custom_methods[self._comp_name].append(self)
  836. ##@brief Textual representation
  837. #@return textual representation of the CustomMethod instance
  838. def __repr__(self):
  839. res = "<CustomMethod name={method_name} target={classname} \
  840. source={module_name}.{fun_name}>"
  841. return res.format(
  842. method_name = self._method_name,
  843. classname = self._comp_name,
  844. module_name = self._fun.__module__,
  845. fun_name = self._fun.__name__)
  846. ##@brief Return a well formed method
  847. #
  848. #@note the type of method depends on the _type attribute
  849. #@return a method directly injectable in the target class
  850. def __get_method(self):
  851. if self._type == self.INSTANCE_METHOD:
  852. def custom__get__(self, obj, objtype = None):
  853. return types.MethodType(self, obj, objtype)
  854. setattr(self._fun, '__get__', custom__get__)
  855. return self._fun
  856. elif self._type == self.CLASS_METHOD:
  857. return classmethod(self._fun)
  858. elif self._type == self.STATIC_METHOD:
  859. return staticmethod(self._fun)
  860. else:
  861. raise RuntimeError("Attribute _type is not one of \
  862. CustomMethod::INSTANCE_METHOD CustomMethod::CLASS_METHOD \
  863. CustomMethod::STATIC_METHOD")
  864. ##@brief Handle custom method dynamic injection in LeAPI dyncode
  865. #
  866. #Called by lodel2_dyncode_loaded hook defined at
  867. #lodel.plugin.core_hooks.lodel2_plugin_custom_methods()
  868. #
  869. #@param cls
  870. #@param dynclasses LeObject child classes : List of dynamically generated
  871. #LeObject child classes
  872. @classmethod
  873. def set_registered(cls, dynclasses):
  874. from lodel import logger
  875. dyn_cls_dict = { dc.__name__:dc for dc in dynclasses}
  876. for cls_name, custom_methods in cls._custom_methods.items():
  877. for custom_method in custom_methods:
  878. if cls_name not in dyn_cls_dict:
  879. logger.error("Custom method %s adding fails : No dynamic \
  880. LeAPI objects named %s." % (custom_method, cls_name))
  881. elif custom_method._method_name in dir(dyn_cls_dict[cls_name]):
  882. logger.warning("Overriding existing method '%s' on target \
  883. with %s" % (custom_method._method_name, custom_method))
  884. else:
  885. setattr(
  886. dyn_cls_dict[cls_name],
  887. custom_method._method_name,
  888. custom_method.__get_method())
  889. logger.debug(
  890. "Custom method %s added to target" % custom_method)
  891. def wrapper_debug_fun():
  892. print(logger)