api.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. class Marker(object):
  20. class T(object):
  21. NONE = 0x00
  22. COMMENT = 0x01
  23. STRING = 0x02
  24. PREPROCESSOR = 0x04
  25. CODE = 0x08
  26. ALL_EXCEPT_CODE = 0x07
  27. ANY = 0xFF
  28. def to_str(self, group):
  29. if group == self.NONE:
  30. return "none"
  31. elif group == self.COMMENT:
  32. return "comment"
  33. elif group == self.STRING:
  34. return "string"
  35. elif group == self.PREPROCESSOR:
  36. return "preprocessor"
  37. elif group == self.CODE:
  38. return "code"
  39. else:
  40. assert(False)
  41. def __init__(self, offset_begin, offset_end, group):
  42. self.begin = offset_begin
  43. self.end = offset_end
  44. self.group = group
  45. def get_offset_begin(self):
  46. return self.begin
  47. def get_offset_end(self):
  48. return self.end
  49. def get_type(self):
  50. return self.group
  51. class BasePlugin(object):
  52. def initialize(self):
  53. pass
  54. def terminate(self):
  55. pass
  56. def set_name(self, name):
  57. self.name = name
  58. def get_name(self):
  59. if hasattr(self, 'name') == False:
  60. return None
  61. return self.name
  62. def set_version(self, version):
  63. self.version = version
  64. def get_version(self):
  65. if hasattr(self, 'version') == False:
  66. return None
  67. return self.version
  68. def set_plugin_loader(self, loader):
  69. self.plugin_loader = loader
  70. def get_plugin_loader(self):
  71. if hasattr(self, 'plugin_loader') == False:
  72. return None
  73. return self.plugin_loader
  74. class Plugin(BasePlugin):
  75. class Field(object):
  76. def __init__(self, name, ftype, non_zero=False):
  77. self.name = name
  78. self.type = ftype
  79. self.non_zero = non_zero
  80. class Property(object):
  81. def __init__(self, name, value):
  82. self.name = name
  83. self.value = value
  84. def initialize(self, namespace=None, support_regions=True, fields=[], properties=[]):
  85. super(Plugin, self).initialize()
  86. if hasattr(self, 'is_updated') == False:
  87. self.is_updated = False # original initialization
  88. db_loader = self.get_plugin_loader().get_database_loader()
  89. if namespace == None:
  90. namespace = self.get_name()
  91. if (len(fields) != 0 or len(properties) != 0):
  92. prev_version = db_loader.set_property(self.get_name() + ":version", self.get_version())
  93. if prev_version != self.get_version():
  94. self.is_updated = True
  95. for prop in properties:
  96. assert(prop.name != 'version')
  97. prev_prop = db_loader.set_property(self.get_name() + ":" + prop.name, prop.value)
  98. if prev_prop != prop.value:
  99. self.is_updated = True
  100. if len(fields) != 0:
  101. namespace_obj = db_loader.create_namespace(namespace,
  102. support_regions=support_regions,
  103. version=self.get_version())
  104. for field in fields:
  105. is_created = namespace_obj.add_field(field.name, field.type, non_zero=field.non_zero)
  106. assert(is_created != None)
  107. # if field is created (not cloned from the previous db),
  108. # mark the plug-in as updated in order to trigger full rescan
  109. self.is_updated = self.is_updated or is_created
  110. class MetricPlugin(Plugin):
  111. def add_field(self, is_active, field,
  112. pattern_to_search,
  113. marker_type_mask=Marker.T.ANY,
  114. exclude_subregions=True,
  115. merge_markers=False):
  116. if hasattr(self, '_fields') == False:
  117. self._fields = {}
  118. if is_active == True:
  119. self._fields[field.name] = (field,
  120. marker_type_mask,
  121. exclude_subregions,
  122. merge_markers,
  123. pattern_to_search)
  124. def is_active(self, field_name = None):
  125. if field_name == None:
  126. return (len(self._fields.keys()) > 0)
  127. return (field_name in self._fields.keys())
  128. def get_fields(self):
  129. result = []
  130. for key in self._fields.keys():
  131. result.append(self._fields[key][0])
  132. return result
  133. def count_if_active(self, field_name, data, namespace=None):
  134. if self.is_active(field_name) == False:
  135. return
  136. if namespace == None:
  137. namespace = self.get_name()
  138. field_data = self._fields[field_name]
  139. text = data.get_content()
  140. for region in data.iterate_regions():
  141. count = 0
  142. for marker in data.iterate_markers(
  143. filter_group = field_data[1],
  144. region_id = region.get_id(),
  145. exclude_children = field_data[2],
  146. merge=field_data[3]):
  147. count += len(field_data[4].findall(text, marker.get_offset_begin(), marker.get_offset_end()))
  148. region.set_data(namespace, field_name, count)
  149. class InterfaceNotImplemented(Exception):
  150. def __init__(self, obj):
  151. import sys
  152. Exception.__init__(self, "Method '"
  153. + sys._getframe(1).f_code.co_name
  154. + "' has not been implemented for "
  155. + str(obj.__class__))
  156. class IConfigurable(object):
  157. def configure(self, options):
  158. raise InterfaceNotImplemented(self)
  159. def declare_configuration(self, optparser):
  160. raise InterfaceNotImplemented(self)
  161. class IRunable(object):
  162. def run(self, args):
  163. raise InterfaceNotImplemented(self)
  164. class IParser(object):
  165. def process(self, parent, data, is_updated):
  166. raise InterfaceNotImplemented(self)
  167. class ICode(object):
  168. pass
  169. class ITool(object):
  170. def run(self, tool_args):
  171. raise InterfaceNotImplemented(self)
  172. class CallbackNotImplemented(Exception):
  173. def __init__(self, obj, callback_name):
  174. Exception.__init__(self, "Callback '"
  175. + callback_name
  176. + "' has not been implemented for "
  177. + str(obj.__class__))
  178. class Child(object):
  179. def notify(self, parent, callback_name, *args):
  180. if hasattr(self, callback_name) == False:
  181. raise CallbackNotImplemented(self, callback_name)
  182. self.__getattribute__(callback_name)(parent, *args)
  183. def subscribe_by_parents_name(self, parent_name, callback_name='callback'):
  184. self.get_plugin_loader().get_plugin(parent_name).subscribe(self, callback_name)
  185. def subscribe_by_parents_interface(self, interface, callback_name='callback'):
  186. for plugin in self.get_plugin_loader().iterate_plugins():
  187. if isinstance(plugin, interface):
  188. plugin.subscribe(self, callback_name)
  189. class Parent(object):
  190. def init_Parent(self):
  191. if hasattr(self, 'children') == False:
  192. self.children = []
  193. def subscribe(self, obj, callback_name):
  194. self.init_Parent()
  195. if (isinstance(obj, Child) == False):
  196. raise TypeError()
  197. self.children.append((obj,callback_name))
  198. def unsubscribe(self, obj, callback_name):
  199. self.init_Parent()
  200. self.children.remove((obj, callback_name))
  201. def notify_children(self, *args):
  202. self.init_Parent()
  203. for child in self.children:
  204. child[0].notify(self, child[1], *args)
  205. def iterate_children(self):
  206. self.init_Parent()
  207. for child in self.children:
  208. yield child
  209. # TODO re-factor and remove this
  210. class ExitError(Exception):
  211. def __init__(self, plugin, reason):
  212. if plugin != None:
  213. Exception.__init__(self, "Plugin '"
  214. + plugin.get_name()
  215. + "' requested abnormal termination: "
  216. + reason)
  217. else:
  218. Exception.__init__(self, "'Abnormal termination requested: "
  219. + reason)