loader.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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 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(self.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 or old_data == None:
  327. return None
  328. return new_data - old_data
  329. ####################################
  330. # Packager Interface
  331. ####################################
  332. class PackagerError(Exception):
  333. def __init__(self):
  334. Exception.__init__(self, "Failed to pack or unpack.")
  335. class PackagerFactory(object):
  336. def create(self, python_type):
  337. if python_type == None:
  338. return PackagerFactory.SkipPackager()
  339. if python_type == int:
  340. return PackagerFactory.IntPackager()
  341. if python_type == float:
  342. return PackagerFactory.FloatPackager()
  343. if python_type == str:
  344. return PackagerFactory.StringPackager()
  345. class PackagerFactoryError(Exception):
  346. def __init__(self, python_type):
  347. Exception.__init__(self, "Python type '" + str(python_type) + "' is not supported by the factory.")
  348. raise PackagerFactoryError(python_type)
  349. def get_python_type(self, sql_type):
  350. if sql_type == "integer":
  351. return int
  352. if sql_type == "real":
  353. return float
  354. if sql_type == "text":
  355. return str
  356. class PackagerFactoryError(Exception):
  357. def __init__(self, sql_type):
  358. Exception.__init__(self, "SQL type '" + str(sql_type) + "' is not supported by the factory.")
  359. raise PackagerFactoryError(sql_type)
  360. class IPackager(object):
  361. def pack(self, unpacked_data):
  362. raise core.api.InterfaceNotImplemented(self)
  363. def unpack(self, packed_data):
  364. raise core.api.InterfaceNotImplemented(self)
  365. def get_sql_type(self):
  366. raise core.api.InterfaceNotImplemented(self)
  367. def get_python_type(self):
  368. raise core.api.InterfaceNotImplemented(self)
  369. class IntPackager(IPackager):
  370. def pack(self, unpacked_data):
  371. if not isinstance(unpacked_data, int):
  372. raise PackagerError()
  373. return str(unpacked_data)
  374. def unpack(self, packed_data):
  375. try:
  376. return int(packed_data)
  377. except ValueError:
  378. raise PackagerError()
  379. def get_sql_type(self):
  380. return "integer"
  381. def get_python_type(self):
  382. return int
  383. class FloatPackager(IPackager):
  384. def pack(self, unpacked_data):
  385. if not isinstance(unpacked_data, float):
  386. raise PackagerError()
  387. return str(unpacked_data)
  388. def unpack(self, packed_data):
  389. try:
  390. return float(packed_data)
  391. except ValueError:
  392. raise PackagerError()
  393. def get_sql_type(self):
  394. return "real"
  395. def get_python_type(self):
  396. return float
  397. class StringPackager(IPackager):
  398. def pack(self, unpacked_data):
  399. if not isinstance(unpacked_data, str):
  400. raise PackagerError()
  401. return str(unpacked_data)
  402. def unpack(self, packed_data):
  403. try:
  404. return str(packed_data)
  405. except ValueError:
  406. raise PackagerError()
  407. def get_sql_type(self):
  408. return "text"
  409. def get_python_type(self):
  410. return str
  411. class SkipPackager(IPackager):
  412. def pack(self, unpacked_data):
  413. return None
  414. def unpack(self, packed_data):
  415. return None
  416. def get_sql_type(self):
  417. return None
  418. def get_python_type(self):
  419. return None
  420. ####################################
  421. # Loader
  422. ####################################
  423. class NamespaceError(Exception):
  424. def __init__(self, namespace, reason):
  425. Exception.__init__(self, "Namespace '"
  426. + namespace
  427. + "': '"
  428. + reason
  429. + "'")
  430. class FieldError(Exception):
  431. def __init__(self, field, reason):
  432. Exception.__init__(self, "Field '"
  433. + field
  434. + "': '"
  435. + reason
  436. + "'")
  437. class Namespace(object):
  438. def __init__(self, db_handle, name, support_regions = False):
  439. if not isinstance(name, str):
  440. raise NamespaceError(name, "name not a string")
  441. self.name = name
  442. self.support_regions = support_regions
  443. self.fields = {}
  444. self.db = db_handle
  445. if self.db.check_table(name) == False:
  446. self.db.create_table(name, support_regions)
  447. else:
  448. for column in self.db.iterate_columns(name):
  449. self.add_field(column.name, PackagerFactory().get_python_type(column.sql_type))
  450. def get_name(self):
  451. return self.name
  452. def are_regions_supported(self):
  453. return self.support_regions
  454. def add_field(self, field_name, python_type):
  455. if not isinstance(field_name, str):
  456. raise FieldError(field_name, "field_name not a string")
  457. packager = PackagerFactory().create(python_type)
  458. if field_name in self.fields.keys():
  459. raise FieldError(field_name, "double used")
  460. self.fields[field_name] = packager
  461. if self.db.check_column(self.get_name(), field_name) == False:
  462. self.db.create_column(self.name, field_name, packager.get_sql_type())
  463. def iterate_field_names(self):
  464. for name in self.fields.keys():
  465. yield name
  466. def get_field_packager(self, field_name):
  467. if field_name in self.fields.keys():
  468. return self.fields[field_name]
  469. else:
  470. return None
  471. def get_field_sql_type(self, field_name):
  472. return self.get_field_packager(field_name).get_sql_type()
  473. def get_field_python_type(self, field_name):
  474. return self.get_field_packager(field_name).get_python_type()
  475. class DataNotPackable(Exception):
  476. def __init__(self, namespace, field, value, packager, extra_message):
  477. Exception.__init__(self, "Data '"
  478. + str(value)
  479. + "' of type "
  480. + str(value.__class__)
  481. + " referred by '"
  482. + namespace
  483. + "=>"
  484. + field
  485. + "' is not packable by registered packager '"
  486. + str(packager.__class__)
  487. + "': " + extra_message)
  488. class Loader(object):
  489. def __init__(self):
  490. self.namespaces = {}
  491. self.db = None
  492. self.last_file_data = None # for performance boost reasons
  493. def create_database(self, dbfile, previous_db = None):
  494. self.db = core.db.sqlite.Database()
  495. if os.path.exists(dbfile):
  496. logging.warn("Removing existing file: " + dbfile)
  497. os.unlink(dbfile)
  498. if previous_db != None and os.path.exists(previous_db) == False:
  499. raise core.api.ExitError(None, "Database file '" + previous_db + "' does not exist")
  500. self.db.create(dbfile, clone_from=previous_db)
  501. def open_database(self, dbfile, read_only = True):
  502. self.db = core.db.sqlite.Database()
  503. if os.path.exists(dbfile) == False:
  504. raise core.api.ExitError(None, "Database file '" + dbfile + "' does not exist")
  505. self.db.connect(dbfile, read_only=read_only)
  506. for table in self.db.iterate_tables():
  507. self.create_namespace(table.name, table.support_regions)
  508. def create_namespace(self, name, support_regions = False):
  509. if self.db == None:
  510. return None
  511. if name in self.namespaces.keys():
  512. raise NamespaceError(name, "double used")
  513. new_namespace = Namespace(self.db, name, support_regions)
  514. self.namespaces[name] = new_namespace
  515. return new_namespace
  516. def iterate_namespace_names(self):
  517. for name in self.namespaces.keys():
  518. yield name
  519. def get_namespace(self, name):
  520. if name in self.namespaces.keys():
  521. return self.namespaces[name]
  522. else:
  523. return None
  524. def create_file_data(self, path, checksum, content):
  525. if self.db == None:
  526. return None
  527. new_id = self.db.create_file(path, checksum)
  528. result = FileData(self, path, new_id, checksum, content)
  529. self.last_file_data = result
  530. return result
  531. def load_file_data(self, path):
  532. if self.db == None:
  533. return None
  534. if self.last_file_data != None and self.last_file_data.get_path() == path:
  535. return self.last_file_data
  536. data = self.db.get_file(path)
  537. if data == None:
  538. return None
  539. result = FileData(self, data.path, data.id, data.checksum, None)
  540. self.last_file_data = result
  541. return result
  542. def save_file_data(self, file_data):
  543. if self.db == None:
  544. return None
  545. class DataIterator(object):
  546. def iterate_packed_values(self, data, namespace, support_regions = False):
  547. for each in data.iterate_fields(namespace):
  548. space = self.loader.get_namespace(namespace)
  549. if space == None:
  550. raise DataNotPackable(namespace, each[0], each[1], None, "The namespace has not been found")
  551. packager = space.get_field_packager(each[0])
  552. if packager == None:
  553. raise DataNotPackable(namespace, each[0], each[1], None, "The field has not been found")
  554. if space.support_regions != support_regions:
  555. raise DataNotPackable(namespace, each[0], each[1], packager, "Incompatible support for regions")
  556. try:
  557. packed_data = packager.pack(each[1])
  558. if packed_data == None:
  559. continue
  560. except PackagerError:
  561. raise DataNotPackable(namespace, each[0], each[1], packager, "Packager raised exception")
  562. yield (each[0], packed_data)
  563. def __init__(self, loader, data, namespace, support_regions = False):
  564. self.loader = loader
  565. self.iterator = self.iterate_packed_values(data, namespace, support_regions)
  566. def __iter__(self):
  567. return self.iterator
  568. for namespace in file_data.iterate_namespaces():
  569. if file_data.is_namespace_updated(namespace) == False:
  570. continue
  571. self.db.add_row(namespace,
  572. file_data.get_id(),
  573. None,
  574. DataIterator(self, file_data, namespace))
  575. if file_data.are_regions_loaded():
  576. for region in file_data.iterate_regions():
  577. for namespace in region.iterate_namespaces():
  578. if region.is_namespace_updated(namespace) == False:
  579. continue
  580. self.db.add_row(namespace,
  581. file_data.get_id(),
  582. region.get_id(),
  583. DataIterator(self, region, namespace, support_regions = True))
  584. def iterate_file_data(self):
  585. if self.db != None:
  586. for data in self.db.iterate_files():
  587. yield FileData(self, data.path, data.id, data.checksum, None)
  588. def load_aggregated_data(self, path = None, path_like_filter = "%", namespaces = None):
  589. if self.db == None:
  590. return None
  591. final_path_like = path_like_filter
  592. if path != None:
  593. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  594. return None
  595. final_path_like = path + path_like_filter
  596. if namespaces == None:
  597. namespaces = self.namespaces.keys()
  598. result = AggregatedData(self, path)
  599. for name in namespaces:
  600. data = self.db.aggregate_rows(name, path_like = final_path_like)
  601. for field in data.keys():
  602. result.set_data(name, field, data[field])
  603. return result
  604. def load_selected_data(self, namespace, fields = None, path = None, path_like_filter = "%", filters = []):
  605. if self.db == None:
  606. return None
  607. final_path_like = path_like_filter
  608. if path != None:
  609. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  610. return None
  611. final_path_like = path + path_like_filter
  612. namespace_obj = self.get_namespace(namespace)
  613. if namespace_obj == None:
  614. return None
  615. class SelectDataIterator(object):
  616. def iterate_selected_values(self, loader, namespace_obj, final_path_like, fields, filters):
  617. for row in loader.db.select_rows(namespace_obj.get_name(), path_like=final_path_like, filters=filters):
  618. region_id = None
  619. if namespace_obj.are_regions_supported() == True:
  620. region_id = row['region_id']
  621. data = SelectData(loader, row['path'], row['id'], region_id)
  622. field_names = fields
  623. if fields == None:
  624. field_names = namespace_obj.iterate_field_names()
  625. for field in field_names:
  626. data.set_data(namespace, field, row[field])
  627. yield data
  628. def __init__(self, loader, namespace_obj, final_path_like, fields, filters):
  629. self.iterator = self.iterate_selected_values(loader, namespace_obj, final_path_like, fields, filters)
  630. def __iter__(self):
  631. return self.iterator
  632. return SelectDataIterator(self, namespace_obj, final_path_like, fields, filters)