export.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 re
  21. import core.log
  22. import core.db.loader
  23. import core.db.post
  24. import core.db.utils
  25. import core.cmdparser
  26. import core.export.convert
  27. import core.api
  28. class Tool(core.api.ITool):
  29. def run(self, tool_args):
  30. return main(tool_args)
  31. def main(tool_args):
  32. log_plugin = core.log.Plugin()
  33. db_plugin = core.db.post.Plugin()
  34. parser = core.cmdparser.MultiOptionParser(usage="Usage: %prog export [options] -- [path 1] ... [path N]")
  35. log_plugin.declare_configuration(parser)
  36. db_plugin.declare_configuration(parser)
  37. parser.add_option("--format", "--ft", default='csv', choices=['csv', 'xml'], help="Format of the output data. "
  38. "Possible values are 'xml' and 'csv' [default: %default]")
  39. (options, args) = parser.parse_args(tool_args)
  40. log_plugin.configure(options)
  41. db_plugin.configure(options)
  42. out_format = options.__dict__['format']
  43. loader_prev = core.db.loader.Loader()
  44. if db_plugin.dbfile_prev != None:
  45. loader_prev.open_database(db_plugin.dbfile_prev)
  46. loader = core.db.loader.Loader()
  47. loader.open_database(db_plugin.dbfile)
  48. # Check for versions consistency
  49. for each in loader.iterate_properties():
  50. if db_plugin.dbfile_prev != None:
  51. prev = loader_prev.get_property(each.name)
  52. if prev != each.value:
  53. logging.warn("Previous data has got different metadata:")
  54. logging.warn(" - identification of change trends can be not reliable")
  55. logging.warn(" - use 'info' tool to get more details")
  56. break
  57. paths = None
  58. if len(args) == 0:
  59. paths = [""]
  60. else:
  61. paths = args
  62. exit_code = export_to_stdout(out_format, paths, loader, loader_prev)
  63. return exit_code
  64. def export_to_stdout(out_format, paths, loader, loader_prev):
  65. exit_code = 0
  66. columns = []
  67. for name in loader.iterate_namespace_names():
  68. namespace = loader.get_namespace(name)
  69. for field in namespace.iterate_field_names():
  70. columns.append((name, field, namespace.are_regions_supported()))
  71. if out_format == 'xml':
  72. print "<export>\n"
  73. elif out_format == 'csv':
  74. print "CSV"
  75. else:
  76. assert False, "Unknown output format " + out_format
  77. for (ind, path) in enumerate(paths):
  78. logging.info("Processing: " + re.sub(r'''[\\]''', "/", path))
  79. files = loader.iterate_file_data(path)
  80. if files != None:
  81. for file_data in files:
  82. for column in columns:
  83. print column[0], column[1], file_data.get_data(column[0], column[1])
  84. else:
  85. logging.error("Specified path '" + path + "' is invalid (not found in the database records)")
  86. exit_code += 1
  87. if out_format == 'xml':
  88. print "XML"
  89. return 0