export.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 csv
  21. import mpp.api
  22. import mpp.log
  23. import mpp.dbf
  24. import mpp.cmdparser
  25. import mpp.utils
  26. class Tool(mpp.api.ITool):
  27. def run(self, tool_args):
  28. return main(tool_args)
  29. def main(tool_args):
  30. log_plugin = mpp.log.Plugin()
  31. db_plugin = mpp.dbf.Plugin()
  32. parser = mpp.cmdparser.MultiOptionParser(usage="Usage: %prog export [options] -- [path 1] ... [path N]")
  33. log_plugin.declare_configuration(parser)
  34. db_plugin.declare_configuration(parser)
  35. parser.add_option("--format", "--ft", default='csv', choices=['csv', 'xml'], help="Format of the output data. "
  36. "Possible values are 'xml' and 'csv' [default: %default]")
  37. (options, args) = parser.parse_args(tool_args)
  38. log_plugin.configure(options)
  39. db_plugin.configure(options)
  40. out_format = options.__dict__['format']
  41. log_plugin.initialize()
  42. db_plugin.initialize()
  43. loader_prev = db_plugin.get_loader_prev()
  44. loader = db_plugin.get_loader()
  45. # Check for versions consistency
  46. for each in loader.iterate_properties():
  47. if db_plugin.dbfile_prev != None:
  48. prev = loader_prev.get_property(each.name)
  49. if prev != each.value:
  50. logging.warn("Previous data has got different metadata:")
  51. logging.warn(" - identification of change trends can be not reliable")
  52. logging.warn(" - use 'info' tool to get more details")
  53. break
  54. paths = None
  55. if len(args) == 0:
  56. paths = [""]
  57. else:
  58. paths = args
  59. exit_code = export_to_stdout(out_format, paths, loader, loader_prev)
  60. return exit_code
  61. def export_to_stdout(out_format, paths, loader, loader_prev):
  62. class StdoutWriter(object):
  63. def write(self, *args, **kwargs):
  64. print args[0],
  65. exit_code = 0
  66. columnNames = ["file", "region", ]
  67. columns = []
  68. for name in loader.iterate_namespace_names():
  69. namespace = loader.get_namespace(name)
  70. for field in namespace.iterate_field_names():
  71. columns.append((name, field, namespace.are_regions_supported()))
  72. columnNames.append(name + ":" + field)
  73. writer = StdoutWriter()
  74. csvWriter = csv.writer(writer)
  75. csvWriter.writerow(columnNames)
  76. if out_format == 'xml':
  77. print "<export>\n"
  78. elif out_format == 'csv':
  79. print "CSV"
  80. else:
  81. assert False, "Unknown output format " + out_format
  82. for path in paths:
  83. path = mpp.utils.preprocess_path(path)
  84. files = loader.iterate_file_data(path)
  85. if files != None:
  86. for file_data in files:
  87. for reg in file_data.iterate_regions():
  88. per_reg_data = []
  89. for column in columns:
  90. per_reg_data.append(reg.get_data(column[0], column[1]))
  91. csvWriter.writerow([file_data.get_path(), reg.get_name()] + per_reg_data)
  92. per_file_data = []
  93. for column in columns:
  94. per_file_data.append(file_data.get_data(column[0], column[1]))
  95. csvWriter.writerow([file_data.get_path(), None] + per_file_data)
  96. else:
  97. mpp.utils.report_bad_path(path)
  98. exit_code += 1
  99. if out_format == 'xml':
  100. print "XML"
  101. return 0