loader.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 logging
  20. import os.path
  21. import core.api
  22. import core.db.sqlite
  23. ####################################
  24. # Data Interface
  25. ####################################
  26. class Data(object):
  27. def __init__(self):
  28. self.data = {}
  29. def get_data(self, namespace, field):
  30. if namespace not in self.data.keys():
  31. return None
  32. if field not in self.data[namespace].keys():
  33. return None
  34. return self.data[namespace][field]
  35. def set_data(self, namespace, field, value):
  36. if namespace not in self.data:
  37. self.data[namespace] = {}
  38. self.data[namespace][field] = value
  39. def iterate_namespaces(self):
  40. for namespace in self.data.keys():
  41. yield namespace
  42. def iterate_fields(self, namespace):
  43. for field in self.data[namespace].keys():
  44. yield (field, self.data[namespace][field])
  45. def get_data_tree(self, namespaces=None):
  46. return self.data
  47. def __repr__(self):
  48. return object.__repr__(self) + " with data " + self.data.__repr__()
  49. class LoadableData(Data):
  50. def __init__(self, loader, file_id, region_id):
  51. Data.__init__(self)
  52. self.loader = loader
  53. self.file_id = file_id
  54. self.region_id = region_id
  55. self.loaded_namespaces = []
  56. self.changed_namespaces = []
  57. def load_namespace(self, namespace):
  58. try:
  59. row = self.loader.db.get_row(namespace, self.file_id, self.region_id)
  60. except Exception:
  61. logging.debug("No data in the database for namespace: " + namespace)
  62. return
  63. if row == None:
  64. return
  65. for column_name in row.keys():
  66. packager = self.loader.get_namespace(namespace).get_field_packager(column_name)
  67. if packager == None:
  68. continue
  69. Data.set_data(self, namespace, column_name, packager.unpack(row[column_name]))
  70. def set_data(self, namespace, field, value):
  71. if namespace not in self.changed_namespaces:
  72. self.changed_namespaces.append(namespace)
  73. return Data.set_data(self, namespace, field, value)
  74. def get_data(self, namespace, field):
  75. if namespace not in self.loaded_namespaces:
  76. self.loaded_namespaces.append(namespace)
  77. self.load_namespace(namespace)
  78. return Data.get_data(self, namespace, field)
  79. def is_namespace_updated(self, namespace):
  80. return namespace in self.changed_namespaces
  81. def is_namespace_loaded(self, namespace):
  82. return namespace in self.loaded_namespaces
  83. def get_data_tree(self, namespaces=None):
  84. if namespaces == None:
  85. namespaces = self.loader.iterate_namespace_names()
  86. for each in namespaces:
  87. self.load_namespace(each)
  88. return Data.get_data_tree(self)
  89. class FileRegionData(LoadableData):
  90. class T(object):
  91. NONE = 0x00
  92. GLOBAL = 0x01
  93. CLASS = 0x02
  94. STRUCT = 0x04
  95. NAMESPACE = 0x08
  96. FUNCTION = 0x10
  97. ANY = 0xFFFFFFFF
  98. def to_str(self, group):
  99. if group == self.NONE:
  100. return "none"
  101. elif group == self.GLOBAL:
  102. return "global"
  103. elif group == self.CLASS:
  104. return "class"
  105. elif group == self.STRUCT:
  106. return "struct"
  107. elif group == self.NAMESPACE:
  108. return "namespace"
  109. elif group == self.FUNCTION:
  110. return "function"
  111. else:
  112. assert(False)
  113. def __init__(self, loader, file_id, region_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum):
  114. LoadableData.__init__(self, loader, file_id, region_id)
  115. self.name = region_name
  116. self.begin = offset_begin
  117. self.end = offset_end
  118. self.line_begin = line_begin
  119. self.line_end = line_end
  120. self.cursor = cursor_line
  121. self.group = group
  122. self.checksum = checksum
  123. self.children = []
  124. def get_id(self):
  125. return self.region_id
  126. def get_name(self):
  127. return self.name
  128. def get_offset_begin(self):
  129. return self.begin
  130. def get_offset_end(self):
  131. return self.end
  132. def get_line_begin(self):
  133. return self.line_begin
  134. def get_line_end(self):
  135. return self.line_end
  136. def get_cursor(self):
  137. return self.cursor
  138. def get_type(self):
  139. return self.group
  140. def get_checksum(self):
  141. return self.checksum
  142. def register_subregion_id(self, child_id):
  143. self.children.append(child_id)
  144. def iterate_subregion_ids(self):
  145. return self.children
  146. class Marker(object):
  147. class T(object):
  148. NONE = 0x00
  149. COMMENT = 0x01
  150. STRING = 0x02
  151. PREPROCESSOR = 0x04
  152. ALL_EXCEPT_CODE = 0x07
  153. def __init__(self, offset_begin, offset_end, group):
  154. self.begin = offset_begin
  155. self.end = offset_end
  156. self.group = group
  157. def get_offset_begin(self):
  158. return self.begin
  159. def get_offset_end(self):
  160. return self.end
  161. def get_type(self):
  162. return self.group
  163. class FileData(LoadableData):
  164. def __init__(self, loader, path, file_id, checksum, content):
  165. LoadableData.__init__(self, loader, file_id, None)
  166. self.path = path
  167. self.checksum = checksum
  168. self.content = content
  169. self.regions = None
  170. self.markers = None
  171. self.loader = loader
  172. self.loading_tmp = []
  173. def get_id(self):
  174. return self.file_id
  175. def get_path(self):
  176. return self.path
  177. def get_checksum(self):
  178. return self.checksum
  179. def get_content(self, exclude = Marker.T.NONE):
  180. if exclude == Marker.T.NONE:
  181. return self.content
  182. if exclude == (Marker.T.COMMENT | Marker.T.STRING | Marker.T.PREPROCESSOR):
  183. # optimise frequent queries of this type
  184. if hasattr(self, 'content_cache'):
  185. return self.content_cache
  186. last_pos = 0
  187. content = ""
  188. for marker in self.iterate_markers(exclude):
  189. content += self.content[last_pos:marker.begin]
  190. content += " " * (marker.end - marker.begin)
  191. last_pos = marker.end
  192. content += self.content[last_pos:]
  193. if exclude == (Marker.T.COMMENT | Marker.T.STRING | Marker.T.PREPROCESSOR):
  194. self.content_cache = content
  195. assert(len(content) == len(self.content))
  196. return content
  197. def internal_append_region(self, region):
  198. # here we apply some magic - we rely on special ordering of coming regions,
  199. # which is supported by code parsers
  200. prev_id = None
  201. while True:
  202. if len(self.loading_tmp) == 0:
  203. break
  204. prev_id = self.loading_tmp.pop()
  205. if self.get_region(prev_id).get_offset_end() > region.get_offset_begin():
  206. self.loading_tmp.append(prev_id) # return back
  207. break
  208. self.loading_tmp.append(region.get_id())
  209. if prev_id != None:
  210. self.get_region(prev_id).register_subregion_id(region.get_id())
  211. self.regions.append(region)
  212. def load_regions(self):
  213. if self.regions == None:
  214. self.regions = []
  215. for each in self.loader.db.iterate_regions(self.get_id()):
  216. self.internal_append_region(FileRegionData(self.loader,
  217. self.get_id(),
  218. each.region_id,
  219. each.name,
  220. each.begin,
  221. each.end,
  222. each.line_begin,
  223. each.line_end,
  224. each.cursor,
  225. each.group,
  226. each.checksum))
  227. assert(len(self.regions) == each.region_id)
  228. def add_region(self, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum):
  229. if self.regions == None:
  230. self.regions = [] # do not load in time of collection
  231. new_id = len(self.regions) + 1
  232. self.internal_append_region(FileRegionData(self.loader, self.get_id(), new_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum))
  233. self.loader.db.create_region(self.file_id, new_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum)
  234. return new_id
  235. def get_region(self, region_id):
  236. self.load_regions()
  237. return self.regions[region_id - 1]
  238. def iterate_regions(self, filter_group = FileRegionData.T.ANY):
  239. self.load_regions()
  240. for each in self.regions:
  241. if each.group & filter_group:
  242. yield each
  243. def are_regions_loaded(self):
  244. return self.regions != None
  245. def load_markers(self):
  246. if self.markers == None:
  247. self.markers = []
  248. for each in self.loader.db.iterate_markers(self.get_id()):
  249. self.markers.append(Marker(each.begin, each.end, each.group))
  250. def add_marker(self, offset_begin, offset_end, group):
  251. if self.markers == None:
  252. self.markers = [] # do not load in time of collection
  253. self.markers.append(Marker(offset_begin, offset_end, group))
  254. self.loader.db.create_marker(self.file_id, offset_begin, offset_end, group)
  255. def iterate_markers(self, filter_group = Marker.T.COMMENT |
  256. Marker.T.STRING | Marker.T.PREPROCESSOR):
  257. self.load_markers()
  258. for each in self.markers:
  259. if each.group & filter_group:
  260. yield each
  261. def get_marker_types(self):
  262. return Marker.T
  263. def get_region_types(self):
  264. return FileRegionData.T
  265. def are_markers_loaded(self):
  266. return self.markers != None
  267. def __repr__(self):
  268. return Data.__repr__(self) + " and regions " + self.regions.__repr__()
  269. class AggregatedData(Data):
  270. def __init__(self, loader, path):
  271. Data.__init__(self)
  272. self.path = path
  273. self.loader = loader
  274. self.subdirs = None
  275. self.subfiles = None
  276. def get_subdirs(self):
  277. if self.subdirs != None:
  278. return self.subdirs
  279. self.subdirs = []
  280. if self.path != None:
  281. for subdir in self.loader.db.iterate_dircontent(self.path, include_subdirs = True, include_subfiles = False):
  282. self.subdirs.append(subdir)
  283. return self.subdirs
  284. def get_subfiles(self):
  285. if self.subfiles != None:
  286. return self.subfiles
  287. self.subfiles = []
  288. if self.path != None:
  289. for subfile in self.loader.db.iterate_dircontent(self.path, include_subdirs = False, include_subfiles = True):
  290. self.subfiles.append(subfile)
  291. return self.subfiles
  292. class SelectData(Data):
  293. def __init__(self, loader, path, file_id, region_id):
  294. Data.__init__(self)
  295. self.loader = loader
  296. self.path = path
  297. self.file_id = file_id
  298. self.region_id = region_id
  299. self.region = None
  300. def get_path(self):
  301. return self.path
  302. def get_region(self):
  303. if self.region == None and self.region_id != None:
  304. row = self.loader.db.get_region(self.file_id, self.region_id)
  305. if row != None:
  306. self.region = FileRegionData(self.loader,
  307. self.file_id,
  308. self.region_id,
  309. row.name,
  310. row.begin,
  311. row.end,
  312. row.line_begin,
  313. row.line_end,
  314. row.cursor,
  315. row.group,
  316. row.checksum)
  317. return self.region
  318. class DiffData(Data):
  319. def __init__(self, new_data, old_data):
  320. Data.__init__(self)
  321. self.new_data = new_data
  322. self.old_data = old_data
  323. def get_data(self, namespace, field):
  324. new_data = self.new_data.get_data(namespace, field)
  325. old_data = self.old_data.get_data(namespace, field)
  326. if new_data == None:
  327. return None
  328. if old_data == None:
  329. # non_zero fields has got zero value by default if missed
  330. # the data can be also unavailable,
  331. # because previous collection does not include that
  332. # but external tools (like limit.py) should warn about this,
  333. # using list of registered database properties
  334. old_data = 0
  335. return new_data - old_data
  336. ####################################
  337. # Packager Interface
  338. ####################################
  339. class PackagerError(Exception):
  340. def __init__(self):
  341. Exception.__init__(self, "Failed to pack or unpack.")
  342. class PackagerFactory(object):
  343. def create(self, python_type, non_zero):
  344. if python_type == None:
  345. return PackagerFactory.SkipPackager()
  346. if python_type == int:
  347. if non_zero == False:
  348. return PackagerFactory.IntPackager()
  349. else:
  350. return PackagerFactory.IntNonZeroPackager()
  351. if python_type == float and non_zero == False:
  352. return PackagerFactory.FloatPackager()
  353. if python_type == str:
  354. return PackagerFactory.StringPackager()
  355. class PackagerFactoryError(Exception):
  356. def __init__(self, python_type):
  357. Exception.__init__(self, "Python type '" + str(python_type) + "' is not supported by the factory.")
  358. raise PackagerFactoryError(python_type)
  359. def get_python_type(self, sql_type):
  360. if sql_type == "integer":
  361. return int
  362. if sql_type == "real":
  363. return float
  364. if sql_type == "text":
  365. return str
  366. class PackagerFactoryError(Exception):
  367. def __init__(self, sql_type):
  368. Exception.__init__(self, "SQL type '" + str(sql_type) + "' is not supported by the factory.")
  369. raise PackagerFactoryError(sql_type)
  370. class IPackager(object):
  371. def pack(self, unpacked_data):
  372. raise core.api.InterfaceNotImplemented(self)
  373. def unpack(self, packed_data):
  374. raise core.api.InterfaceNotImplemented(self)
  375. def get_sql_type(self):
  376. raise core.api.InterfaceNotImplemented(self)
  377. def get_python_type(self):
  378. raise core.api.InterfaceNotImplemented(self)
  379. def is_non_zero(self):
  380. return False
  381. class IntPackager(IPackager):
  382. def pack(self, unpacked_data):
  383. if not isinstance(unpacked_data, int):
  384. raise PackagerError()
  385. return str(unpacked_data)
  386. def unpack(self, packed_data):
  387. try:
  388. return int(packed_data)
  389. except ValueError:
  390. raise PackagerError()
  391. def get_sql_type(self):
  392. return "integer"
  393. def get_python_type(self):
  394. return int
  395. class IntNonZeroPackager(IntPackager):
  396. def pack(self, unpacked_data):
  397. if unpacked_data == 0:
  398. raise PackagerError()
  399. return PackagerFactory.IntPackager.pack(self, unpacked_data)
  400. def is_non_zero(self):
  401. return True
  402. class FloatPackager(IPackager):
  403. def pack(self, unpacked_data):
  404. if not isinstance(unpacked_data, float):
  405. raise PackagerError()
  406. return str(unpacked_data)
  407. def unpack(self, packed_data):
  408. try:
  409. return float(packed_data)
  410. except ValueError:
  411. raise PackagerError()
  412. def get_sql_type(self):
  413. return "real"
  414. def get_python_type(self):
  415. return float
  416. class FloatNonZeroPackager(FloatPackager):
  417. def pack(self, unpacked_data):
  418. if unpacked_data == 0:
  419. raise PackagerError()
  420. return PackagerFactory.FloatPackager.pack(self, unpacked_data)
  421. def is_non_zero(self):
  422. return True
  423. class StringPackager(IPackager):
  424. def pack(self, unpacked_data):
  425. if not isinstance(unpacked_data, str):
  426. raise PackagerError()
  427. return str(unpacked_data)
  428. def unpack(self, packed_data):
  429. try:
  430. return str(packed_data)
  431. except ValueError:
  432. raise PackagerError()
  433. def get_sql_type(self):
  434. return "text"
  435. def get_python_type(self):
  436. return str
  437. class SkipPackager(IPackager):
  438. def pack(self, unpacked_data):
  439. return None
  440. def unpack(self, packed_data):
  441. return None
  442. def get_sql_type(self):
  443. return None
  444. def get_python_type(self):
  445. return None
  446. ####################################
  447. # Loader
  448. ####################################
  449. class NamespaceError(Exception):
  450. def __init__(self, namespace, reason):
  451. Exception.__init__(self, "Namespace '"
  452. + namespace
  453. + "': '"
  454. + reason
  455. + "'")
  456. class FieldError(Exception):
  457. def __init__(self, field, reason):
  458. Exception.__init__(self, "Field '"
  459. + field
  460. + "': '"
  461. + reason
  462. + "'")
  463. class Namespace(object):
  464. def __init__(self, db_handle, name, support_regions = False):
  465. if not isinstance(name, str):
  466. raise NamespaceError(name, "name not a string")
  467. self.name = name
  468. self.support_regions = support_regions
  469. self.fields = {}
  470. self.db = db_handle
  471. if self.db.check_table(name) == False:
  472. self.db.create_table(name, support_regions)
  473. else:
  474. for column in self.db.iterate_columns(name):
  475. self.add_field(column.name, PackagerFactory().get_python_type(column.sql_type), non_zero=column.non_zero)
  476. def get_name(self):
  477. return self.name
  478. def are_regions_supported(self):
  479. return self.support_regions
  480. def add_field(self, field_name, python_type, non_zero=False):
  481. if not isinstance(field_name, str):
  482. raise FieldError(field_name, "field_name not a string")
  483. packager = PackagerFactory().create(python_type, non_zero)
  484. if field_name in self.fields.keys():
  485. raise FieldError(field_name, "double used")
  486. self.fields[field_name] = packager
  487. if self.db.check_column(self.get_name(), field_name) == False:
  488. self.db.create_column(self.name, field_name, packager.get_sql_type(), non_zero=non_zero)
  489. def iterate_field_names(self):
  490. for name in self.fields.keys():
  491. yield name
  492. def get_field_packager(self, field_name):
  493. if field_name in self.fields.keys():
  494. return self.fields[field_name]
  495. else:
  496. return None
  497. def get_field_sql_type(self, field_name):
  498. return self.get_field_packager(field_name).get_sql_type()
  499. def get_field_python_type(self, field_name):
  500. return self.get_field_packager(field_name).get_python_type()
  501. class DataNotPackable(Exception):
  502. def __init__(self, namespace, field, value, packager, extra_message):
  503. Exception.__init__(self, "Data '"
  504. + str(value)
  505. + "' of type "
  506. + str(value.__class__)
  507. + " referred by '"
  508. + namespace
  509. + "=>"
  510. + field
  511. + "' is not packable by registered packager '"
  512. + str(packager.__class__)
  513. + "': " + extra_message)
  514. class Loader(object):
  515. def __init__(self):
  516. self.namespaces = {}
  517. self.db = None
  518. self.last_file_data = None # for performance boost reasons
  519. def create_database(self, dbfile, previous_db = None):
  520. self.db = core.db.sqlite.Database()
  521. if os.path.exists(dbfile):
  522. logging.warn("Removing existing file: " + dbfile)
  523. os.unlink(dbfile)
  524. if previous_db != None and os.path.exists(previous_db) == False:
  525. raise core.api.ExitError(None, "Database file '" + previous_db + "' does not exist")
  526. self.db.create(dbfile, clone_from=previous_db)
  527. def open_database(self, dbfile, read_only = True):
  528. self.db = core.db.sqlite.Database()
  529. if os.path.exists(dbfile) == False:
  530. raise core.api.ExitError(None, "Database file '" + dbfile + "' does not exist")
  531. self.db.connect(dbfile, read_only=read_only)
  532. for table in self.db.iterate_tables():
  533. self.create_namespace(table.name, table.support_regions)
  534. def set_property(self, property_name, value):
  535. if self.db == None:
  536. return None
  537. return self.db.set_property(property_name, value)
  538. def get_property(self, property_name):
  539. if self.db == None:
  540. return None
  541. return self.db.get_property(property_name)
  542. def iterate_properties(self):
  543. if self.db == None:
  544. return None
  545. return self.db.iterate_properties()
  546. def create_namespace(self, name, support_regions = False):
  547. if self.db == None:
  548. return None
  549. if name in self.namespaces.keys():
  550. raise NamespaceError(name, "double used")
  551. new_namespace = Namespace(self.db, name, support_regions)
  552. self.namespaces[name] = new_namespace
  553. return new_namespace
  554. def iterate_namespace_names(self):
  555. for name in self.namespaces.keys():
  556. yield name
  557. def get_namespace(self, name):
  558. if name in self.namespaces.keys():
  559. return self.namespaces[name]
  560. else:
  561. return None
  562. def create_file_data(self, path, checksum, content):
  563. if self.db == None:
  564. return None
  565. (new_id, is_updated) = self.db.create_file(path, checksum)
  566. result = FileData(self, path, new_id, checksum, content)
  567. self.last_file_data = result
  568. return (result, is_updated)
  569. def load_file_data(self, path):
  570. if self.db == None:
  571. return None
  572. if self.last_file_data != None and self.last_file_data.get_path() == path:
  573. return self.last_file_data
  574. data = self.db.get_file(path)
  575. if data == None:
  576. return None
  577. result = FileData(self, data.path, data.id, data.checksum, None)
  578. self.last_file_data = result
  579. return result
  580. def save_file_data(self, file_data):
  581. if self.db == None:
  582. return None
  583. class DataIterator(object):
  584. def iterate_packed_values(self, data, namespace, support_regions = False):
  585. for each in data.iterate_fields(namespace):
  586. space = self.loader.get_namespace(namespace)
  587. if space == None:
  588. raise DataNotPackable(namespace, each[0], each[1], None, "The namespace has not been found")
  589. packager = space.get_field_packager(each[0])
  590. if packager == None:
  591. raise DataNotPackable(namespace, each[0], each[1], None, "The field has not been found")
  592. if space.support_regions != support_regions:
  593. raise DataNotPackable(namespace, each[0], each[1], packager, "Incompatible support for regions")
  594. try:
  595. packed_data = packager.pack(each[1])
  596. if packed_data == None:
  597. continue
  598. except PackagerError:
  599. raise DataNotPackable(namespace, each[0], each[1], packager, "Packager raised exception")
  600. yield (each[0], packed_data)
  601. def __init__(self, loader, data, namespace, support_regions = False):
  602. self.loader = loader
  603. self.iterator = self.iterate_packed_values(data, namespace, support_regions)
  604. def __iter__(self):
  605. return self.iterator
  606. for namespace in file_data.iterate_namespaces():
  607. if file_data.is_namespace_updated(namespace) == False:
  608. continue
  609. self.db.add_row(namespace,
  610. file_data.get_id(),
  611. None,
  612. DataIterator(self, file_data, namespace))
  613. if file_data.are_regions_loaded():
  614. for region in file_data.iterate_regions():
  615. for namespace in region.iterate_namespaces():
  616. if region.is_namespace_updated(namespace) == False:
  617. continue
  618. self.db.add_row(namespace,
  619. file_data.get_id(),
  620. region.get_id(),
  621. DataIterator(self, region, namespace, support_regions = True))
  622. def iterate_file_data(self):
  623. if self.db != None:
  624. for data in self.db.iterate_files():
  625. yield FileData(self, data.path, data.id, data.checksum, None)
  626. def load_aggregated_data(self, path = None, path_like_filter = "%", namespaces = None):
  627. if self.db == None:
  628. return None
  629. final_path_like = path_like_filter
  630. if path != None:
  631. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  632. return None
  633. final_path_like = path + path_like_filter
  634. if namespaces == None:
  635. namespaces = self.namespaces.keys()
  636. result = AggregatedData(self, path)
  637. for name in namespaces:
  638. namespace = self.get_namespace(name)
  639. data = self.db.aggregate_rows(name, path_like = final_path_like)
  640. for field in data.keys():
  641. if namespace.get_field_packager(field).is_non_zero() == True:
  642. data[field]['min'] = None
  643. data[field]['avg'] = None
  644. result.set_data(name, field, data[field])
  645. return result
  646. def load_selected_data(self, namespace, fields = None, path = None, path_like_filter = "%", filters = []):
  647. if self.db == None:
  648. return None
  649. # TODO implement restriction for non_zero fields
  650. final_path_like = path_like_filter
  651. if path != None:
  652. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  653. return None
  654. final_path_like = path + path_like_filter
  655. namespace_obj = self.get_namespace(namespace)
  656. if namespace_obj == None:
  657. return None
  658. class SelectDataIterator(object):
  659. def iterate_selected_values(self, loader, namespace_obj, final_path_like, fields, filters):
  660. for row in loader.db.select_rows(namespace_obj.get_name(), path_like=final_path_like, filters=filters):
  661. region_id = None
  662. if namespace_obj.are_regions_supported() == True:
  663. region_id = row['region_id']
  664. data = SelectData(loader, row['path'], row['id'], region_id)
  665. field_names = fields
  666. if fields == None:
  667. field_names = namespace_obj.iterate_field_names()
  668. for field in field_names:
  669. data.set_data(namespace, field, row[field])
  670. yield data
  671. def __init__(self, loader, namespace_obj, final_path_like, fields, filters):
  672. self.iterator = self.iterate_selected_values(loader, namespace_obj, final_path_like, fields, filters)
  673. def __iter__(self):
  674. return self.iterator
  675. return SelectDataIterator(self, namespace_obj, final_path_like, fields, filters)