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

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