loader.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import mpp.api
  20. import os
  21. import fnmatch
  22. class Loader(object):
  23. def __init__(self):
  24. self.plugins = []
  25. self.parsers = []
  26. self.hash = {}
  27. self.db = mpp.api.Loader()
  28. def get_database_loader(self):
  29. return self.db
  30. def get_plugin(self, name):
  31. return self.hash[name]['instance']
  32. def iterate_plugins(self, is_reversed = False):
  33. if is_reversed == False:
  34. for item in self.plugins:
  35. yield item['instance']
  36. else:
  37. for item in reversed(self.plugins):
  38. yield item['instance']
  39. def register_parser(self, fnmatch_exp_list, parser):
  40. self.parsers.append((fnmatch_exp_list, parser))
  41. def get_parser(self, file_path):
  42. for parser in self.parsers:
  43. for fnmatch_exp in parser[0]:
  44. if fnmatch.fnmatch(file_path, fnmatch_exp):
  45. return parser[1]
  46. return None
  47. def load(self, directory, optparser, args):
  48. import sys
  49. sys.path.append(directory)
  50. def load_recursively(manager, directory):
  51. import ConfigParser
  52. import re
  53. pattern = re.compile(r'.*[.]ini$', flags=re.IGNORECASE)
  54. dirList = os.listdir(directory)
  55. for fname in dirList:
  56. fname = os.path.join(directory, fname)
  57. if os.path.isdir(fname):
  58. load_recursively(manager, fname)
  59. elif re.match(pattern, fname):
  60. config = ConfigParser.ConfigParser()
  61. config.read(fname)
  62. item = {'package': config.get('Plugin', 'package'),
  63. 'module': config.get('Plugin', 'module'),
  64. 'class': config.get('Plugin', 'class'),
  65. 'version': config.get('Plugin', 'version'),
  66. 'depends': config.get('Plugin', 'depends'),
  67. 'enabled': config.getboolean('Plugin', 'enabled')}
  68. if item['enabled']:
  69. manager.plugins.append(item)
  70. manager.hash[item['package'] + '.' + item['module']] = item
  71. load_recursively(self, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ext-priority'))
  72. load_recursively(self, directory)
  73. # TODO check dependencies
  74. for item in self.plugins:
  75. plugin = __import__(item['package'], globals(), locals(), [item['module']], -1)
  76. module_attr = plugin.__getattribute__(item['module'])
  77. class_attr = module_attr.__getattribute__(item['class'])
  78. item['instance'] = class_attr.__new__(class_attr)
  79. item['instance'].__init__()
  80. item['instance'].set_name(item['package'] + "." + item['module'])
  81. item['instance'].set_version(item['version'])
  82. item['instance'].set_plugin_loader(self)
  83. for item in self.iterate_plugins():
  84. if (isinstance(item, mpp.api.IConfigurable)):
  85. item.declare_configuration(optparser)
  86. (options, args) = optparser.parse_args(args)
  87. for item in self.iterate_plugins():
  88. if (isinstance(item, mpp.api.IConfigurable)):
  89. item.configure(options)
  90. for item in self.iterate_plugins():
  91. item.initialize()
  92. return args
  93. def unload(self):
  94. for item in self.iterate_plugins(is_reversed = True):
  95. item.terminate()
  96. def run(self, args):
  97. exit_code = 0
  98. for item in self.iterate_plugins():
  99. if (isinstance(item, mpp.api.IRunable)):
  100. exit_code += item.run(args)
  101. return exit_code
  102. def __repr__(self):
  103. result = object.__repr__(self) + ' with loaded:'
  104. for item in self.iterate_plugins():
  105. result += '\n\t' + item.__repr__()
  106. if isinstance(item, mpp.api.Parent):
  107. result += ' with subscribed:'
  108. for child in item.iterate_children():
  109. result += '\n\t\t' + child.__repr__()
  110. return result