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 36KB

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