export.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 mpp.api
  20. import mpp.utils
  21. import csv
  22. class Plugin(mpp.api.Plugin, mpp.api.IConfigurable, mpp.api.IRunable):
  23. def declare_configuration(self, parser):
  24. parser.add_option("--format", "--ft", default='csv', choices=['csv', 'xml'], help="Format of the output data. "
  25. "Possible values are 'xml' and 'csv' [default: %default]")
  26. def configure(self, options):
  27. self.out_format = options.__dict__['format']
  28. def run(self, args):
  29. loader_prev = self.get_plugin_loader().get_plugin('mpp.dbf').get_loader_prev()
  30. loader = self.get_plugin_loader().get_plugin('mpp.dbf').get_loader()
  31. paths = None
  32. if len(args) == 0:
  33. paths = [""]
  34. else:
  35. paths = args
  36. exit_code = export_to_stdout(self.out_format, paths, loader, loader_prev)
  37. return exit_code
  38. def export_to_stdout(out_format, paths, loader, loader_prev):
  39. class StdoutWriter(object):
  40. def write(self, *args, **kwargs):
  41. print args[0],
  42. exit_code = 0
  43. columnNames = ["file", "region", ]
  44. columns = []
  45. for name in loader.iterate_namespace_names():
  46. namespace = loader.get_namespace(name)
  47. for field in namespace.iterate_field_names():
  48. columns.append((name, field, namespace.are_regions_supported()))
  49. columnNames.append(name + ":" + field)
  50. writer = StdoutWriter()
  51. csvWriter = csv.writer(writer)
  52. csvWriter.writerow(columnNames)
  53. if out_format == 'xml':
  54. print "<export>\n"
  55. elif out_format == 'csv':
  56. print "CSV"
  57. else:
  58. assert False, "Unknown output format " + out_format
  59. for path in paths:
  60. path = mpp.utils.preprocess_path(path)
  61. files = loader.iterate_file_data(path)
  62. if files != None:
  63. for file_data in files:
  64. for reg in file_data.iterate_regions():
  65. per_reg_data = []
  66. for column in columns:
  67. per_reg_data.append(reg.get_data(column[0], column[1]))
  68. csvWriter.writerow([file_data.get_path(), reg.get_name()] + per_reg_data)
  69. per_file_data = []
  70. for column in columns:
  71. per_file_data.append(file_data.get_data(column[0], column[1]))
  72. csvWriter.writerow([file_data.get_path(), None] + per_file_data)
  73. else:
  74. mpp.utils.report_bad_path(path)
  75. exit_code += 1
  76. if out_format == 'xml':
  77. print "XML"
  78. return 0