loader.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 core.api
  20. import core.db.loader
  21. import os
  22. import fnmatch
  23. class Loader(object):
  24. def __init__(self):
  25. self.plugins = []
  26. self.parsers = []
  27. self.hash = {}
  28. self.db = core.db.loader.Loader()
  29. def get_database_loader(self):
  30. return self.db
  31. def get_plugin(self, name):
  32. return self.hash[name]['instance']
  33. def iterate_plugins(self, is_reversed = False):
  34. if is_reversed == False:
  35. for item in self.plugins:
  36. yield item['instance']
  37. else:
  38. for item in reversed(self.plugins):
  39. yield item['instance']
  40. def register_parser(self, fnmatch_exp_list, parser):
  41. self.parsers.append((fnmatch_exp_list, parser))
  42. def get_parser(self, file_path):
  43. for parser in self.parsers:
  44. for fnmatch_exp in parser[0]:
  45. if fnmatch.fnmatch(file_path, fnmatch_exp):
  46. return parser[1]
  47. return None
  48. def load(self, directory, optparser, args):
  49. import sys
  50. sys.path.append(directory)
  51. def load_recursively(manager, directory):
  52. import ConfigParser
  53. import re
  54. pattern = re.compile(r'.*[.]ini$', flags=re.IGNORECASE)
  55. dirList = os.listdir(directory)
  56. for fname in dirList:
  57. fname = os.path.join(directory, fname)
  58. if os.path.isdir(fname):
  59. load_recursively(manager, fname)
  60. elif re.match(pattern, fname):
  61. config = ConfigParser.ConfigParser()
  62. config.read(fname)
  63. item = {'package': config.get('Plugin', 'package'),
  64. 'module': config.get('Plugin', 'module'),
  65. 'class': config.get('Plugin', 'class'),
  66. 'version': config.get('Plugin', 'version'),
  67. 'depends': config.get('Plugin', 'depends'),
  68. 'enabled': config.getboolean('Plugin', 'enabled')}
  69. if item['enabled']:
  70. manager.plugins.append(item)
  71. manager.hash[item['package'] + '.' + item['module']] = item
  72. load_recursively(self, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ext-priority'))
  73. load_recursively(self, directory)
  74. # TODO check dependencies
  75. for item in self.plugins:
  76. plugin = __import__(item['package'], globals(), locals(), [item['module']], -1)
  77. module_attr = plugin.__getattribute__(item['module'])
  78. class_attr = module_attr.__getattribute__(item['class'])
  79. item['instance'] = class_attr.__new__(class_attr)
  80. item['instance'].__init__()
  81. item['instance'].set_name(item['package'] + "." + item['module'])
  82. item['instance'].set_version(item['version'])
  83. item['instance'].set_plugin_loader(self)
  84. for item in self.iterate_plugins():
  85. if (isinstance(item, core.api.IConfigurable)):
  86. item.declare_configuration(optparser)
  87. (options, args) = optparser.parse_args(args)
  88. for item in self.iterate_plugins():
  89. if (isinstance(item, core.api.IConfigurable)):
  90. item.configure(options)
  91. for item in self.iterate_plugins():
  92. item.initialize()
  93. return args
  94. def unload(self):
  95. for item in self.iterate_plugins(is_reversed = True):
  96. item.terminate()
  97. def run(self, args):
  98. exit_code = 0
  99. for item in self.iterate_plugins():
  100. if (isinstance(item, core.api.IRunable)):
  101. exit_code += item.run(args)
  102. return exit_code
  103. def __repr__(self):
  104. result = object.__repr__(self) + ' with loaded:'
  105. for item in self.iterate_plugins():
  106. result += '\n\t' + item.__repr__()
  107. if isinstance(item, core.api.Parent):
  108. result += ' with subscribed:'
  109. for child in item.iterate_children():
  110. result += '\n\t\t' + child.__repr__()
  111. return result