api.py 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 os.path
  20. import sys
  21. import mpp.internal.dbwrap
  22. import mpp.internal.api_impl
  23. class InterfaceNotImplemented(Exception):
  24. def __init__(self, obj):
  25. Exception.__init__(self, "Method '"
  26. + sys._getframe(1).f_code.co_name
  27. + "' has not been implemented for "
  28. + str(obj.__class__))
  29. class IConfigurable(object):
  30. def configure(self, options):
  31. raise InterfaceNotImplemented(self)
  32. def declare_configuration(self, optparser):
  33. raise InterfaceNotImplemented(self)
  34. class IRunable(object):
  35. def run(self, args):
  36. raise InterfaceNotImplemented(self)
  37. class IParser(object):
  38. def process(self, parent, data, is_updated):
  39. raise InterfaceNotImplemented(self)
  40. class ICode(object):
  41. pass
  42. class CallbackNotImplemented(Exception):
  43. def __init__(self, obj, callback_name):
  44. Exception.__init__(self, "Callback '"
  45. + callback_name
  46. + "' has not been implemented for "
  47. + str(obj.__class__))
  48. class Child(object):
  49. def notify(self, parent, callback_name, *args):
  50. if hasattr(self, callback_name) == False:
  51. raise CallbackNotImplemented(self, callback_name)
  52. self.__getattribute__(callback_name)(parent, *args)
  53. def subscribe_by_parents_name(self, parent_name, callback_name='callback'):
  54. self.get_plugin(parent_name).subscribe(self, callback_name)
  55. def subscribe_by_parents_names(self, parent_names, callback_name='callback'):
  56. for parent_name in parent_names:
  57. self.get_plugin(parent_name).subscribe(self, callback_name)
  58. def subscribe_by_parents_interface(self, interface, callback_name='callback'):
  59. for plugin in self._get_plugin_loader().iterate_plugins():
  60. if isinstance(plugin, interface):
  61. plugin.subscribe(self, callback_name)
  62. class Parent(object):
  63. def init_Parent(self):
  64. if hasattr(self, 'children') == False:
  65. self.children = []
  66. def subscribe(self, obj, callback_name):
  67. self.init_Parent()
  68. if (isinstance(obj, Child) == False):
  69. raise TypeError()
  70. self.children.append((obj,callback_name))
  71. def unsubscribe(self, obj, callback_name):
  72. self.init_Parent()
  73. self.children.remove((obj, callback_name))
  74. def notify_children(self, *args):
  75. self.init_Parent()
  76. for child in self.children:
  77. child[0].notify(self, child[1], *args)
  78. def iterate_children(self):
  79. self.init_Parent()
  80. for child in self.children:
  81. yield child
  82. ##############################################################################
  83. #
  84. #
  85. #
  86. ##############################################################################
  87. class Data(object):
  88. def __init__(self):
  89. self.data = {}
  90. def get_data(self, namespace, field):
  91. if namespace not in self.data.keys():
  92. return None
  93. if field not in self.data[namespace].keys():
  94. return None
  95. return self.data[namespace][field]
  96. def set_data(self, namespace, field, value):
  97. if namespace not in self.data:
  98. self.data[namespace] = {}
  99. self.data[namespace][field] = value
  100. def iterate_namespaces(self):
  101. for namespace in self.data.keys():
  102. yield namespace
  103. def iterate_fields(self, namespace):
  104. for field in self.data[namespace].keys():
  105. yield (field, self.data[namespace][field])
  106. def get_data_tree(self, namespaces=None):
  107. return self.data
  108. def __repr__(self):
  109. return object.__repr__(self) + " with data " + self.data.__repr__()
  110. class LoadableData(Data):
  111. def __init__(self, loader, file_id, region_id):
  112. Data.__init__(self)
  113. self.loader = loader
  114. self.file_id = file_id
  115. self.region_id = region_id
  116. self.loaded_namespaces = []
  117. self.changed_namespaces = []
  118. def load_namespace(self, namespace):
  119. namespace_obj = self.loader.get_namespace(namespace)
  120. if namespace_obj == None:
  121. return
  122. regions_supported = namespace_obj.are_regions_supported()
  123. if ((self.region_id == None and regions_supported == True) or
  124. (self.region_id != None and regions_supported == False)):
  125. return
  126. row = self.loader.db.get_row(namespace, self.file_id, self.region_id)
  127. if row == None:
  128. return
  129. for column_name in row.keys():
  130. try:
  131. packager = namespace_obj._get_field_packager(column_name)
  132. except mpp.internal.api_impl.PackagerError:
  133. continue
  134. if row[column_name] == None:
  135. continue
  136. Data.set_data(self, namespace, column_name, packager.unpack(row[column_name]))
  137. def set_data(self, namespace, field, value):
  138. if namespace not in self.changed_namespaces:
  139. self.changed_namespaces.append(namespace)
  140. return Data.set_data(self, namespace, field, value)
  141. def get_data(self, namespace, field):
  142. if namespace not in self.loaded_namespaces:
  143. self.loaded_namespaces.append(namespace)
  144. self.load_namespace(namespace)
  145. return Data.get_data(self, namespace, field)
  146. def is_namespace_updated(self, namespace):
  147. return namespace in self.changed_namespaces
  148. def is_namespace_loaded(self, namespace):
  149. return namespace in self.loaded_namespaces
  150. def get_data_tree(self, namespaces=None):
  151. if namespaces == None:
  152. namespaces = self.loader.iterate_namespace_names()
  153. for each in namespaces:
  154. self.load_namespace(each)
  155. return Data.get_data_tree(self)
  156. class Region(LoadableData):
  157. class T(object):
  158. NONE = 0x00
  159. GLOBAL = 0x01
  160. CLASS = 0x02
  161. STRUCT = 0x04
  162. NAMESPACE = 0x08
  163. FUNCTION = 0x10
  164. INTERFACE = 0x20
  165. ANY = 0xFF
  166. def to_str(self, group):
  167. if group == self.NONE:
  168. return "none"
  169. if group == self.ANY:
  170. return "any"
  171. result = []
  172. if group & self.GLOBAL != 0:
  173. result.append("global")
  174. if group & self.CLASS != 0:
  175. result.append("class")
  176. if group & self.STRUCT != 0:
  177. result.append("struct")
  178. if group & self.NAMESPACE != 0:
  179. result.append("namespace")
  180. if group & self.FUNCTION != 0:
  181. result.append("function")
  182. if group & self.INTERFACE != 0:
  183. result.append("interface")
  184. assert(len(result) != 0)
  185. return ', '.join(result)
  186. def from_str(self, group):
  187. if group == "global":
  188. return self.GLOBAL
  189. elif group == "class":
  190. return self.CLASS
  191. elif group == "struct":
  192. return self.STRUCT
  193. elif group == "namespace":
  194. return self.NAMESPACE
  195. elif group == "function":
  196. return self.FUNCTION
  197. elif group == "interface":
  198. return self.INTERFACE
  199. elif group == "any":
  200. return self.ANY
  201. else:
  202. return None
  203. def __init__(self, loader, file_id, region_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum):
  204. LoadableData.__init__(self, loader, file_id, region_id)
  205. self.name = region_name
  206. self.begin = offset_begin
  207. self.end = offset_end
  208. self.line_begin = line_begin
  209. self.line_end = line_end
  210. self.cursor = cursor_line
  211. self.group = group
  212. self.checksum = checksum
  213. self.children = []
  214. def get_id(self):
  215. return self.region_id
  216. def get_name(self):
  217. return self.name
  218. def get_offset_begin(self):
  219. return self.begin
  220. def get_offset_end(self):
  221. return self.end
  222. def get_line_begin(self):
  223. return self.line_begin
  224. def get_line_end(self):
  225. return self.line_end
  226. def get_cursor(self):
  227. return self.cursor
  228. def get_type(self):
  229. return self.group
  230. def get_checksum(self):
  231. return self.checksum
  232. def iterate_subregion_ids(self):
  233. return self.children
  234. def _register_subregion_id(self, child_id):
  235. self.children.append(child_id)
  236. class Marker(object):
  237. class T(object):
  238. NONE = 0x00
  239. COMMENT = 0x01
  240. STRING = 0x02
  241. PREPROCESSOR = 0x04
  242. CODE = 0x08
  243. ANY = 0xFF
  244. def to_str(self, group):
  245. if group == self.NONE:
  246. return "none"
  247. elif group == self.COMMENT:
  248. return "comment"
  249. elif group == self.STRING:
  250. return "string"
  251. elif group == self.PREPROCESSOR:
  252. return "preprocessor"
  253. elif group == self.CODE:
  254. return "code"
  255. else:
  256. assert(False)
  257. def __init__(self, offset_begin, offset_end, group):
  258. self.begin = offset_begin
  259. self.end = offset_end
  260. self.group = group
  261. def get_offset_begin(self):
  262. return self.begin
  263. def get_offset_end(self):
  264. return self.end
  265. def get_type(self):
  266. return self.group
  267. class FileData(LoadableData):
  268. def __init__(self, loader, path, file_id, checksum, content):
  269. LoadableData.__init__(self, loader, file_id, None)
  270. self.path = path
  271. self.checksum = checksum
  272. self.content = content
  273. self.regions = None
  274. self.markers = None
  275. self.loader = loader
  276. self.loading_tmp = []
  277. def get_id(self):
  278. return self.file_id
  279. def get_path(self):
  280. return self.path
  281. def get_checksum(self):
  282. return self.checksum
  283. def get_content(self):
  284. return self.content
  285. def _internal_append_region(self, region):
  286. # here we apply some magic - we rely on special ordering of coming regions,
  287. # which is supported by code parsers
  288. prev_id = None
  289. while True:
  290. if len(self.loading_tmp) == 0:
  291. break
  292. prev_id = self.loading_tmp.pop()
  293. if self.get_region(prev_id).get_offset_end() > region.get_offset_begin():
  294. self.loading_tmp.append(prev_id) # return back
  295. break
  296. self.loading_tmp.append(region.get_id())
  297. if prev_id != None:
  298. self.get_region(prev_id)._register_subregion_id(region.get_id())
  299. self.regions.append(region)
  300. def load_regions(self):
  301. if self.regions == None:
  302. self.regions = []
  303. for each in self.loader.db.iterate_regions(self.get_id()):
  304. self._internal_append_region(Region(self.loader,
  305. self.get_id(),
  306. each.region_id,
  307. each.name,
  308. each.begin,
  309. each.end,
  310. each.line_begin,
  311. each.line_end,
  312. each.cursor,
  313. each.group,
  314. each.checksum))
  315. assert(len(self.regions) == each.region_id)
  316. def add_region(self, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum):
  317. if self.regions == None:
  318. # # do not load regions and markers in time of collection
  319. # if region is added first by parser, set markers to empty list as well
  320. # because if there are no markers in a file, it forces loading of markers
  321. # during iterate_markers call
  322. self.regions = []
  323. self.markers = []
  324. new_id = len(self.regions) + 1
  325. self._internal_append_region(Region(self.loader, self.get_id(), new_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum))
  326. self.loader.db.create_region(self.file_id, new_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum)
  327. return new_id
  328. def get_region(self, region_id):
  329. self.load_regions()
  330. return self.regions[region_id - 1]
  331. def iterate_regions(self, filter_group = Region.T.ANY, region_id = None):
  332. self.load_regions()
  333. if region_id == None:
  334. for each in self.regions:
  335. if each.group & filter_group:
  336. yield each
  337. else:
  338. for sub_id in self.get_region(region_id).iterate_subregion_ids():
  339. each = self.get_region(sub_id)
  340. if each.group & filter_group:
  341. yield each
  342. def are_regions_loaded(self):
  343. return self.regions != None
  344. def load_markers(self):
  345. if self.markers == None:
  346. # TODO add assert in case of an attempt to load data during collection
  347. assert(False) # TODO not used in post-processing tools for while, need to be fixed
  348. self.markers = []
  349. for each in self.loader.db.iterate_markers(self.get_id()):
  350. self.markers.append(Marker(each.begin, each.end, each.group))
  351. def add_marker(self, offset_begin, offset_end, group):
  352. if self.markers == None:
  353. # # do not load regions and markers in time of collection
  354. # if marker is added first by parser, set regions to empty list as well
  355. # because if there are no regions in a file, it forces loading of regions
  356. # during iterate_regions call
  357. self.regions = []
  358. self.markers = []
  359. self.markers.append(Marker(offset_begin, offset_end, group))
  360. # TODO drop collecting markers, it is faster to double parse
  361. # it is not the same with regions, it is faster to load regions
  362. # on iterative re-run
  363. #self.loader.db.create_marker(self.file_id, offset_begin, offset_end, group)
  364. def iterate_markers(self, filter_group = Marker.T.ANY,
  365. region_id = None, exclude_children = True, merge = False):
  366. self.load_markers()
  367. # merged markers
  368. if merge == True:
  369. next_marker = None
  370. for marker in self.iterate_markers(filter_group, region_id, exclude_children, merge = False):
  371. if next_marker != None:
  372. if next_marker.get_offset_end() == marker.get_offset_begin():
  373. # sequential markers
  374. next_marker = Marker(next_marker.get_offset_begin(),
  375. marker.get_offset_end(),
  376. next_marker.get_type() | marker.get_type())
  377. else:
  378. yield next_marker
  379. next_marker = None
  380. if next_marker == None:
  381. next_marker = Marker(marker.get_offset_begin(),
  382. marker.get_offset_end(),
  383. marker.get_type())
  384. if next_marker != None:
  385. yield next_marker
  386. # all markers per file
  387. elif region_id == None:
  388. next_code_marker_start = 0
  389. for marker in self.markers:
  390. if Marker.T.CODE & filter_group and next_code_marker_start < marker.get_offset_begin():
  391. yield Marker(next_code_marker_start, marker.get_offset_begin(), Marker.T.CODE)
  392. if marker.group & filter_group:
  393. yield marker
  394. next_code_marker_start = marker.get_offset_end()
  395. if Marker.T.CODE & filter_group and next_code_marker_start < len(self.get_content()):
  396. yield Marker(next_code_marker_start, len(self.get_content()), Marker.T.CODE)
  397. # markers per region
  398. else:
  399. region = self.get_region(region_id)
  400. if region != None:
  401. # code parsers and database know about non-code markers
  402. # clients want to iterate code as markers as well
  403. # so, we embed code markers in run-time
  404. class CodeMarker(Marker):
  405. pass
  406. # cache markers for all regions if it does not exist
  407. if hasattr(region, '_markers_list') == False:
  408. # subroutine to populate _markers_list attribute
  409. # _markers_list does include code markers
  410. def cache_markers_list_rec(data, region_id, marker_start_ind, next_code_marker_start):
  411. region = data.get_region(region_id)
  412. region._markers_list = []
  413. region._first_marker_ind = marker_start_ind
  414. #next_code_marker_start = region.get_offset_begin()
  415. for sub_id in region.iterate_subregion_ids():
  416. subregion = data.get_region(sub_id)
  417. # cache all markers before the subregion
  418. while len(data.markers) > marker_start_ind and \
  419. subregion.get_offset_begin() > data.markers[marker_start_ind].get_offset_begin():
  420. if next_code_marker_start < data.markers[marker_start_ind].get_offset_begin():
  421. # append code markers coming before non-code marker
  422. region._markers_list.append(CodeMarker(next_code_marker_start,
  423. data.markers[marker_start_ind].get_offset_begin(),
  424. Marker.T.CODE))
  425. next_code_marker_start = data.markers[marker_start_ind].get_offset_end()
  426. region._markers_list.append(marker_start_ind)
  427. marker_start_ind += 1
  428. # cache all code markers before the subregion but after the last marker
  429. if next_code_marker_start < subregion.get_offset_begin():
  430. region._markers_list.append(CodeMarker(next_code_marker_start,
  431. subregion.get_offset_begin(),
  432. Marker.T.CODE))
  433. next_code_marker_start = subregion.get_offset_begin()
  434. # here is the recursive call for all sub-regions
  435. (marker_start_ind, next_code_marker_start) = cache_markers_list_rec(data,
  436. sub_id,
  437. marker_start_ind,
  438. next_code_marker_start)
  439. # cache all markers after the last subregion
  440. while len(data.markers) > marker_start_ind and \
  441. region.get_offset_end() > data.markers[marker_start_ind].get_offset_begin():
  442. # append code markers coming before non-code marker
  443. if next_code_marker_start < data.markers[marker_start_ind].get_offset_begin():
  444. region._markers_list.append(CodeMarker(next_code_marker_start,
  445. data.markers[marker_start_ind].get_offset_begin(),
  446. Marker.T.CODE))
  447. next_code_marker_start = data.markers[marker_start_ind].get_offset_end()
  448. region._markers_list.append(marker_start_ind)
  449. marker_start_ind += 1
  450. # cache the last code segment after the last marker
  451. if next_code_marker_start < region.get_offset_end():
  452. region._markers_list.append(CodeMarker(next_code_marker_start,
  453. region.get_offset_end(),
  454. Marker.T.CODE))
  455. next_code_marker_start = region.get_offset_end()
  456. # return the starting point for the next call of this function
  457. return (marker_start_ind, next_code_marker_start)
  458. # append markers list to all regions recursively
  459. (next_marker_pos, next_code_marker_start) = cache_markers_list_rec(self, 1, 0, 0)
  460. assert(next_marker_pos == len(self.markers))
  461. # excluding subregions
  462. if exclude_children == True:
  463. for marker_ind in region._markers_list:
  464. if isinstance(marker_ind, int):
  465. marker = self.markers[marker_ind]
  466. else:
  467. marker = marker_ind # CodeMarker
  468. if marker.group & filter_group:
  469. yield marker
  470. # including subregions
  471. else:
  472. next_code_marker_start = region.get_offset_begin()
  473. for marker in self.markers[region._first_marker_ind:]:
  474. if marker.get_offset_begin() >= region.get_offset_end():
  475. break
  476. if region.get_offset_begin() > marker.get_offset_begin():
  477. continue
  478. if Marker.T.CODE & filter_group and next_code_marker_start < marker.get_offset_begin():
  479. yield Marker(next_code_marker_start, marker.get_offset_begin(), Marker.T.CODE)
  480. if marker.group & filter_group:
  481. yield marker
  482. next_code_marker_start = marker.get_offset_end()
  483. if Marker.T.CODE & filter_group and next_code_marker_start < region.get_offset_end():
  484. yield Marker(next_code_marker_start, region.get_offset_end(), Marker.T.CODE)
  485. def are_markers_loaded(self):
  486. return self.markers != None
  487. def __repr__(self):
  488. return Data.__repr__(self) + " and regions " + self.regions.__repr__()
  489. class AggregatedData(Data):
  490. def __init__(self, loader, path):
  491. Data.__init__(self)
  492. self.path = path
  493. self.loader = loader
  494. self.subdirs = None
  495. self.subfiles = None
  496. def get_subdirs(self):
  497. if self.subdirs != None:
  498. return self.subdirs
  499. self.subdirs = []
  500. if self.path != None:
  501. for subdir in self.loader.db.iterate_dircontent(self.path, include_subdirs = True, include_subfiles = False):
  502. self.subdirs.append(subdir)
  503. return self.subdirs
  504. def get_subfiles(self):
  505. if self.subfiles != None:
  506. return self.subfiles
  507. self.subfiles = []
  508. if self.path != None:
  509. for subfile in self.loader.db.iterate_dircontent(self.path, include_subdirs = False, include_subfiles = True):
  510. self.subfiles.append(subfile)
  511. return self.subfiles
  512. class SelectData(Data):
  513. def __init__(self, loader, path, file_id, region_id):
  514. Data.__init__(self)
  515. self.loader = loader
  516. self.path = path
  517. self.file_id = file_id
  518. self.region_id = region_id
  519. self.region = None
  520. def get_path(self):
  521. return self.path
  522. def get_region(self):
  523. if self.region == None and self.region_id != None:
  524. row = self.loader.db.get_region(self.file_id, self.region_id)
  525. if row != None:
  526. self.region = Region(self.loader,
  527. self.file_id,
  528. self.region_id,
  529. row.name,
  530. row.begin,
  531. row.end,
  532. row.line_begin,
  533. row.line_end,
  534. row.cursor,
  535. row.group,
  536. row.checksum)
  537. return self.region
  538. class DiffData(Data):
  539. def __init__(self, new_data, old_data):
  540. Data.__init__(self)
  541. self.new_data = new_data
  542. self.old_data = old_data
  543. def get_data(self, namespace, field):
  544. new_data = self.new_data.get_data(namespace, field)
  545. old_data = self.old_data.get_data(namespace, field)
  546. if new_data == None:
  547. return None
  548. if old_data == None:
  549. # non_zero fields has got zero value by default if missed
  550. # the data can be also unavailable,
  551. # because previous collection does not include that
  552. # but external tools (like limit.py) should warn about this,
  553. # using list of registered database properties
  554. old_data = 0
  555. return new_data - old_data
  556. ####################################
  557. # Loader
  558. ####################################
  559. class Namespace(object):
  560. class NamespaceError(Exception):
  561. def __init__(self, namespace, reason):
  562. Exception.__init__(self, "Namespace '"
  563. + namespace
  564. + "': '"
  565. + reason
  566. + "'")
  567. class FieldError(Exception):
  568. def __init__(self, field, reason):
  569. Exception.__init__(self, "Field '"
  570. + field
  571. + "': '"
  572. + reason
  573. + "'")
  574. def __init__(self, db_handle, name, support_regions = False, version='1.0'):
  575. if not isinstance(name, str):
  576. raise Namespace.NamespaceError(name, "name not a string")
  577. self.name = name
  578. self.support_regions = support_regions
  579. self.fields = {}
  580. self.db = db_handle
  581. if self.db.check_table(name) == False:
  582. self.db.create_table(name, support_regions, version)
  583. else:
  584. for column in self.db.iterate_columns(name):
  585. self.add_field(column.name,
  586. mpp.internal.api_impl.PackagerFactory().get_python_type(column.sql_type),
  587. non_zero=column.non_zero)
  588. def get_name(self):
  589. return self.name
  590. def are_regions_supported(self):
  591. return self.support_regions
  592. def add_field(self, field_name, python_type, non_zero=False):
  593. if not isinstance(field_name, str):
  594. raise Namespace.FieldError(field_name, "field_name not a string")
  595. packager = mpp.internal.api_impl.PackagerFactory().create(python_type, non_zero)
  596. if field_name in self.fields.keys():
  597. raise Namespace.FieldError(field_name, "double used")
  598. self.fields[field_name] = packager
  599. if self.db.check_column(self.get_name(), field_name) == False:
  600. # - False if cloned
  601. # - True if created
  602. return self.db.create_column(self.name, field_name, packager.get_sql_type(), non_zero=non_zero)
  603. return None # if double request
  604. def iterate_field_names(self):
  605. for name in self.fields.keys():
  606. yield name
  607. def check_field(self, field_name):
  608. try:
  609. self._get_field_packager(field_name)
  610. except mpp.internal.api_impl.PackagerError:
  611. return False
  612. return True
  613. def get_field_sql_type(self, field_name):
  614. try:
  615. return self._get_field_packager(field_name).get_sql_type()
  616. except mpp.internal.api_impl.PackagerError:
  617. raise Namespace.FieldError(field_name, 'does not exist')
  618. def get_field_python_type(self, field_name):
  619. try:
  620. return self._get_field_packager(field_name).get_python_type()
  621. except mpp.internal.api_impl.PackagerError:
  622. raise Namespace.FieldError(field_name, 'does not exist')
  623. def is_field_non_zero(self, field_name):
  624. try:
  625. return self._get_field_packager(field_name).is_non_zero()
  626. except mpp.internal.api_impl.PackagerError:
  627. raise Namespace.FieldError(field_name, 'does not exist')
  628. def _get_field_packager(self, field_name):
  629. if field_name in self.fields.keys():
  630. return self.fields[field_name]
  631. else:
  632. raise mpp.internal.api_impl.PackagerError("unknown field " + field_name + " requested")
  633. class Loader(object):
  634. def __init__(self):
  635. self.namespaces = {}
  636. self.db = None
  637. self.last_file_data = None # for performance boost reasons
  638. def create_database(self, dbfile, previous_db = None):
  639. self.db = mpp.internal.dbwrap.Database()
  640. try:
  641. self.db.create(dbfile, clone_from=previous_db)
  642. except:
  643. return False
  644. return True
  645. def open_database(self, dbfile, read_only = True):
  646. self.db = mpp.internal.dbwrap.Database()
  647. if os.path.exists(dbfile) == False:
  648. return False
  649. try:
  650. self.db.connect(dbfile, read_only=read_only)
  651. except:
  652. return False
  653. for table in self.db.iterate_tables():
  654. self.create_namespace(table.name, table.support_regions)
  655. return True
  656. def set_property(self, property_name, value):
  657. if self.db == None:
  658. return None
  659. return self.db.set_property(property_name, str(value))
  660. def get_property(self, property_name):
  661. if self.db == None:
  662. return None
  663. return self.db.get_property(property_name)
  664. def iterate_properties(self):
  665. if self.db == None:
  666. return None
  667. return self.db.iterate_properties()
  668. def create_namespace(self, name, support_regions = False, version='1.0'):
  669. if self.db == None:
  670. return None
  671. if name in self.namespaces.keys():
  672. raise Namespace.NamespaceError(name, "double used")
  673. new_namespace = Namespace(self.db, name, support_regions, version)
  674. self.namespaces[name] = new_namespace
  675. return new_namespace
  676. def iterate_namespace_names(self):
  677. for name in self.namespaces.keys():
  678. yield name
  679. def get_namespace(self, name):
  680. if name in self.namespaces.keys():
  681. return self.namespaces[name]
  682. else:
  683. return None
  684. def create_file_data(self, path, checksum, content):
  685. if self.db == None:
  686. return None
  687. (new_id, is_updated) = self.db.create_file(path, checksum)
  688. result = FileData(self, path, new_id, checksum, content)
  689. self.last_file_data = result
  690. return (result, is_updated)
  691. def load_file_data(self, path):
  692. if self.db == None:
  693. return None
  694. if self.last_file_data != None and self.last_file_data.get_path() == path:
  695. return self.last_file_data
  696. data = self.db.get_file(path)
  697. if data == None:
  698. return None
  699. result = FileData(self, data.path, data.id, data.checksum, None)
  700. self.last_file_data = result
  701. return result
  702. class DataNotPackable(Exception):
  703. def __init__(self, namespace, field, value, packager, extra_message):
  704. Exception.__init__(self, "Data '"
  705. + str(value)
  706. + "' of type "
  707. + str(value.__class__)
  708. + " referred by '"
  709. + namespace
  710. + "=>"
  711. + field
  712. + "' is not packable by registered packager '"
  713. + str(packager.__class__)
  714. + "': " + extra_message)
  715. def save_file_data(self, file_data):
  716. if self.db == None:
  717. return None
  718. class DataIterator(object):
  719. def iterate_packed_values(self, data, namespace, support_regions = False):
  720. for each in data.iterate_fields(namespace):
  721. space = self.loader.get_namespace(namespace)
  722. if space == None:
  723. raise Loader.DataNotPackable(namespace, each[0], each[1], None, "The namespace has not been found")
  724. try:
  725. packager = space._get_field_packager(each[0])
  726. except mpp.internal.api_impl.PackagerError:
  727. raise Loader.DataNotPackable(namespace, each[0], each[1], None, "The field has not been found")
  728. if space.support_regions != support_regions:
  729. raise Loader.DataNotPackable(namespace, each[0], each[1], packager, "Incompatible support for regions")
  730. try:
  731. packed_data = packager.pack(each[1])
  732. if packed_data == None:
  733. continue
  734. except mpp.internal.api_impl.PackagerError:
  735. raise Loader.DataNotPackable(namespace, each[0], each[1], packager, "Packager raised exception")
  736. yield (each[0], packed_data)
  737. def __init__(self, loader, data, namespace, support_regions = False):
  738. self.loader = loader
  739. self.iterator = self.iterate_packed_values(data, namespace, support_regions)
  740. def __iter__(self):
  741. return self.iterator
  742. # TODO can construct to add multiple rows at one sql query
  743. # to improve the performance
  744. for namespace in file_data.iterate_namespaces():
  745. if file_data.is_namespace_updated(namespace) == False:
  746. continue
  747. self.db.add_row(namespace,
  748. file_data.get_id(),
  749. None,
  750. DataIterator(self, file_data, namespace))
  751. if file_data.are_regions_loaded():
  752. for region in file_data.iterate_regions():
  753. for namespace in region.iterate_namespaces():
  754. if region.is_namespace_updated(namespace) == False:
  755. continue
  756. self.db.add_row(namespace,
  757. file_data.get_id(),
  758. region.get_id(),
  759. DataIterator(self, region, namespace, support_regions = True))
  760. def iterate_file_data(self, path = None, path_like_filter = "%"):
  761. if self.db == None:
  762. return None
  763. final_path_like = path_like_filter
  764. if path != None:
  765. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  766. return None
  767. final_path_like = path + path_like_filter
  768. class FileDataIterator(object):
  769. def iterate_file_data(self, loader, final_path_like):
  770. for data in loader.db.iterate_files(path_like=final_path_like):
  771. yield FileData(loader, data.path, data.id, data.checksum, None)
  772. def __init__(self, loader, final_path_like):
  773. self.iterator = self.iterate_file_data(loader, final_path_like)
  774. def __iter__(self):
  775. return self.iterator
  776. if self.db == None:
  777. return None
  778. return FileDataIterator(self, final_path_like)
  779. def load_aggregated_data(self, path = None, path_like_filter = "%", namespaces = None):
  780. if self.db == None:
  781. return None
  782. final_path_like = path_like_filter
  783. if path != None:
  784. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  785. return None
  786. final_path_like = path + path_like_filter
  787. if namespaces == None:
  788. namespaces = self.namespaces.keys()
  789. result = AggregatedData(self, path)
  790. for name in namespaces:
  791. namespace = self.get_namespace(name)
  792. data = self.db.aggregate_rows(name, path_like = final_path_like)
  793. for field in data.keys():
  794. if namespace.get_field_python_type(field) == str:
  795. continue
  796. data[field]['nonzero'] = namespace.is_field_non_zero(field)
  797. distribution = self.db.count_rows(name, path_like = final_path_like, group_by_column = field)
  798. data[field]['distribution-bars'] = []
  799. for each in distribution:
  800. if each[0] == None:
  801. continue
  802. assert(float(data[field]['count'] != 0))
  803. data[field]['distribution-bars'].append({'metric': each[0],
  804. 'count': each[1],
  805. 'ratio': (float(each[1]) / float(data[field]['count']))})
  806. result.set_data(name, field, data[field])
  807. return result
  808. def load_selected_data(self, namespace, fields = None, path = None, path_like_filter = "%", filters = [],
  809. sort_by = None, limit_by = None):
  810. if self.db == None:
  811. return None
  812. final_path_like = path_like_filter
  813. if path != None:
  814. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  815. return None
  816. final_path_like = path + path_like_filter
  817. namespace_obj = self.get_namespace(namespace)
  818. if namespace_obj == None:
  819. return None
  820. class SelectDataIterator(object):
  821. def iterate_selected_values(self, loader, namespace_obj, final_path_like, fields, filters, sort_by, limit_by):
  822. for row in loader.db.select_rows(namespace_obj.get_name(), path_like=final_path_like, filters=filters,
  823. order_by=sort_by, limit_by=limit_by):
  824. region_id = None
  825. if namespace_obj.are_regions_supported() == True:
  826. region_id = row['region_id']
  827. data = SelectData(loader, row['path'], row['id'], region_id)
  828. field_names = fields
  829. if fields == None:
  830. field_names = namespace_obj.iterate_field_names()
  831. for field in field_names:
  832. data.set_data(namespace, field, row[field])
  833. yield data
  834. def __init__(self, loader, namespace_obj, final_path_like, fields, filters, sort_by, limit_by):
  835. self.iterator = self.iterate_selected_values(loader, namespace_obj, final_path_like, fields, filters, sort_by, limit_by)
  836. def __iter__(self):
  837. return self.iterator
  838. return SelectDataIterator(self, namespace_obj, final_path_like, fields, filters, sort_by, limit_by)
  839. class BasePlugin(object):
  840. def initialize(self):
  841. pass
  842. def terminate(self):
  843. pass
  844. def set_name(self, name):
  845. self.name = name
  846. def get_name(self):
  847. if hasattr(self, 'name') == False:
  848. return None
  849. return self.name
  850. def get_namespace(self):
  851. return self.get_name()
  852. def set_version(self, version):
  853. self.version = version
  854. def get_version(self):
  855. if hasattr(self, 'version') == False:
  856. return None
  857. return self.version
  858. def _set_plugin_loader(self, loader):
  859. self.plugin_loader = loader
  860. def _get_plugin_loader(self):
  861. if hasattr(self, 'plugin_loader') == False:
  862. return None
  863. return self.plugin_loader
  864. def get_plugin(self, plugin_name):
  865. return self._get_plugin_loader().get_plugin(plugin_name)
  866. def get_action(self):
  867. return self._get_plugin_loader().get_action()
  868. class Plugin(BasePlugin):
  869. class Field(object):
  870. def __init__(self, name, ftype, non_zero=False):
  871. self.name = name
  872. self.type = ftype
  873. self.non_zero = non_zero
  874. self._regions_supported = True
  875. class Property(object):
  876. def __init__(self, name, value):
  877. self.name = name
  878. self.value = value
  879. def initialize(self, namespace=None, support_regions=True, fields=[], properties=[]):
  880. super(Plugin, self).initialize()
  881. if hasattr(self, 'is_updated') == False:
  882. self.is_updated = False # original initialization
  883. db_loader = self.get_plugin('mpp.dbf').get_loader()
  884. if namespace == None:
  885. namespace = self.get_name()
  886. if (len(fields) != 0 or len(properties) != 0):
  887. prev_version = db_loader.set_property(self.get_name() + ":version", self.get_version())
  888. if str(prev_version) != str(self.get_version()):
  889. self.is_updated = True
  890. for prop in properties:
  891. assert(prop.name != 'version')
  892. prev_prop = db_loader.set_property(self.get_name() + ":" + prop.name, prop.value)
  893. if str(prev_prop) != str(prop.value):
  894. self.is_updated = True
  895. if len(fields) != 0:
  896. namespace_obj = db_loader.create_namespace(namespace,
  897. support_regions=support_regions,
  898. version=self.get_version())
  899. for field in fields:
  900. is_created = namespace_obj.add_field(field.name, field.type, non_zero=field.non_zero)
  901. field._regions_supported = support_regions
  902. assert(is_created != None)
  903. # if field is created (not cloned from the previous db),
  904. # mark the plug-in as updated in order to trigger full rescan
  905. self.is_updated = self.is_updated or is_created
  906. class MetricPluginMixin(Parent):
  907. class AliasError(Exception):
  908. def __init__(self, alias):
  909. Exception.__init__(self, "Unknown pattern alias: " + str(alias))
  910. class PlainCounter(object):
  911. def __init__(self, namespace, field, plugin, alias, data, region):
  912. self.namespace = namespace
  913. self.field = field
  914. self.plugin = plugin
  915. self.alias = alias
  916. self.data = data
  917. self.region = region
  918. self.result = 0
  919. def count(self, marker, pattern_to_search):
  920. self.result += len(pattern_to_search.findall(self.data.get_content(),
  921. marker.get_offset_begin(),
  922. marker.get_offset_end()))
  923. def get_result(self):
  924. return self.result
  925. class IterIncrementCounter(PlainCounter):
  926. def count(self, marker, pattern_to_search):
  927. self.marker = marker
  928. self.pattern_to_search = pattern_to_search
  929. for match in pattern_to_search.finditer(self.data.get_content(),
  930. marker.get_offset_begin(),
  931. marker.get_offset_end()):
  932. self.result += self.increment(match)
  933. def increment(self, match):
  934. return 1
  935. class IterAssignCounter(PlainCounter):
  936. def count(self, marker, pattern_to_search):
  937. self.marker = marker
  938. self.pattern_to_search = pattern_to_search
  939. for match in pattern_to_search.finditer(self.data.get_content(),
  940. marker.get_offset_begin(),
  941. marker.get_offset_end()):
  942. self.result = self.assign(match)
  943. def assign(self, match):
  944. return self.result
  945. class RankedCounter(PlainCounter):
  946. def __init__(self, *args, **kwargs):
  947. super(MetricPluginMixin.RankedCounter, self).__init__(*args, **kwargs)
  948. self.result = self.region.get_data(self.namespace, self.field)
  949. if self.result == None:
  950. self.result = 1
  951. def get_result(self):
  952. sourced_metric = self.region.get_data(self.rank_source[0], self.rank_source[1])
  953. for (ind, range_pair) in enumerate(self.rank_ranges):
  954. if ((range_pair[0] == None or sourced_metric >= range_pair[0])
  955. and
  956. (range_pair[1] == None or sourced_metric <= range_pair[1])):
  957. self.result = self.result * (ind + 1)
  958. break
  959. return self.result
  960. def declare_metric(self, is_active, field,
  961. pattern_to_search_or_map_of_patterns,
  962. marker_type_mask=Marker.T.ANY,
  963. region_type_mask=Region.T.ANY,
  964. exclude_subregions=True,
  965. merge_markers=False):
  966. if hasattr(self, '_fields') == False:
  967. self._fields = {}
  968. if isinstance(pattern_to_search_or_map_of_patterns, dict):
  969. map_of_patterns = pattern_to_search_or_map_of_patterns
  970. else:
  971. map_of_patterns = {'*': pattern_to_search_or_map_of_patterns}
  972. # client may suply with pattern or pair of pattern + counter class
  973. for key in map_of_patterns.keys():
  974. if isinstance(map_of_patterns[key], tuple) == False:
  975. # if it is not a pair, create a pair using default counter class
  976. map_of_patterns[key] = (map_of_patterns[key],
  977. MetricPluginMixin.PlainCounter)
  978. if is_active == True:
  979. self._fields[field.name] = (field,
  980. marker_type_mask,
  981. exclude_subregions,
  982. merge_markers,
  983. map_of_patterns,
  984. region_type_mask)
  985. def is_active(self, metric_name = None):
  986. if metric_name == None:
  987. return (len(self._fields.keys()) > 0)
  988. return (metric_name in self._fields.keys())
  989. def get_fields(self):
  990. result = []
  991. for key in self._fields.keys():
  992. result.append(self._fields[key][0])
  993. return result
  994. def callback(self, parent, data, is_updated):
  995. # count if metric is enabled,
  996. # and (optimization for the case of iterative rescan:)
  997. # if file is updated or this plugin's settings are updated
  998. is_updated = is_updated or self.is_updated
  999. if is_updated == True:
  1000. for field in self.get_fields():
  1001. self.count_if_active(self.get_namespace(),
  1002. field.name,
  1003. data,
  1004. alias=parent.get_name())
  1005. # this mixin implements parent interface
  1006. self.notify_children(data, is_updated)
  1007. def count_if_active(self, namespace, field, data, alias='*'):
  1008. if self.is_active(field) == False:
  1009. return
  1010. field_data = self._fields[field]
  1011. if alias not in field_data[4].keys():
  1012. if '*' not in field_data[4].keys():
  1013. raise self.AliasError(alias)
  1014. else:
  1015. alias = '*'
  1016. (pattern_to_search, counter_class) = field_data[4][alias]
  1017. if field_data[0]._regions_supported == True:
  1018. for region in data.iterate_regions(filter_group=field_data[5]):
  1019. counter = counter_class(namespace, field, self, alias, data, region)
  1020. if field_data[1] != Marker.T.NONE:
  1021. for marker in data.iterate_markers(
  1022. filter_group = field_data[1],
  1023. region_id = region.get_id(),
  1024. exclude_children = field_data[2],
  1025. merge=field_data[3]):
  1026. counter.count(marker, pattern_to_search)
  1027. count = counter.get_result()
  1028. if count != 0 or field_data[0].non_zero == False:
  1029. region.set_data(namespace, field, count)
  1030. else:
  1031. counter = counter_class(namespace, field, self, alias, data, None)
  1032. if field_data[1] != Marker.T.NONE:
  1033. for marker in data.iterate_markers(
  1034. filter_group = field_data[1],
  1035. region_id = None,
  1036. exclude_children = field_data[2],
  1037. merge=field_data[3]):
  1038. counter.count(marker, pattern_to_search)
  1039. count = counter.get_result()
  1040. if count != 0 or field_data[0].non_zero == False:
  1041. data.set_data(namespace, field, count)