loader.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://swi.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 os
  21. import core.db.loader
  22. class Loader(object):
  23. def __init__(self):
  24. self.plugins = []
  25. self.hash = {}
  26. self.exit_code = 0
  27. self.db = core.db.loader.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 load(self, directory, optparser):
  40. import sys
  41. sys.path.append(directory)
  42. def load_recursively(manager, directory):
  43. import ConfigParser
  44. import re
  45. pattern = re.compile(r'.*[.]ini$', flags=re.IGNORECASE)
  46. dirList = os.listdir(directory)
  47. for fname in dirList:
  48. fname = os.path.join(directory, fname)
  49. if os.path.isdir(fname):
  50. load_recursively(manager, fname)
  51. elif re.match(pattern, fname):
  52. config = ConfigParser.ConfigParser()
  53. config.read(fname)
  54. item = {'package': config.get('Plugin', 'package'),
  55. 'module': config.get('Plugin', 'module'),
  56. 'class': config.get('Plugin', 'class'),
  57. 'version': config.get('Plugin', 'version'),
  58. 'depends': config.get('Plugin', 'depends'),
  59. 'enabled': config.getboolean('Plugin', 'enabled')}
  60. if item['enabled']:
  61. manager.plugins.append(item)
  62. manager.hash[item['package'] + '.' + item['module']] = item
  63. load_recursively(self, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ext-priority'))
  64. load_recursively(self, directory)
  65. # TODO check dependencies
  66. for item in self.plugins:
  67. plugin = __import__(item['package'], globals(), locals(), [item['module']], -1)
  68. module_attr = plugin.__getattribute__(item['module'])
  69. class_attr = module_attr.__getattribute__(item['class'])
  70. item['instance'] = class_attr.__new__(class_attr)
  71. item['instance'].__init__()
  72. item['instance'].set_name(item['package'] + "." + item['module'])
  73. item['instance'].set_plugin_loader(self)
  74. for item in self.iterate_plugins():
  75. if (isinstance(item, core.api.IConfigurable)):
  76. item.declare_configuration(optparser)
  77. (options, args) = optparser.parse_args()
  78. for item in self.iterate_plugins():
  79. if (isinstance(item, core.api.IConfigurable)):
  80. item.configure(options)
  81. for item in self.iterate_plugins():
  82. item.initialize()
  83. return args
  84. def unload(self):
  85. for item in self.iterate_plugins(is_reversed = True):
  86. item.terminate()
  87. def run(self, args):
  88. for item in self.iterate_plugins():
  89. if (isinstance(item, core.api.IRunable)):
  90. item.run(args)
  91. return self.exit_code
  92. def inc_exit_code(self):
  93. self.exit_code += 1
  94. def __repr__(self):
  95. result = object.__repr__(self) + ' with loaded:'
  96. for item in self.iterate_plugins():
  97. result += '\n\t' + item.__repr__()
  98. if isinstance(item, core.api.Parent):
  99. result += ' with subscribed:'
  100. for child in item.iterate_children():
  101. result += '\n\t\t' + child.__repr__()
  102. return result