convertor.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/python
  2. #-*- coding: UTF-8 -*-
  3. # ----------------------------------------------------------------------------
  4. # Convert the performance test result from json files to excel.
  5. #
  6. # Author: Bill Zhang
  7. #
  8. # License: MIT
  9. # ----------------------------------------------------------------------------
  10. '''
  11. Convert the performance test result from json files to excel.
  12. '''
  13. import xlwt
  14. import os
  15. import json
  16. from argparse import ArgumentParser
  17. DEFAULT_STYLE = 'borders: left thin, right thin, top thin, bottom thin;'
  18. CONDITION_STYLE = 'pattern: pattern solid, fore_color light_green;'
  19. RESULT_STYLE = 'pattern: pattern solid, fore_color light_yellow;'
  20. BASE_KEYS = [
  21. 'osVersion',
  22. 'fileVersion',
  23. 'timeStamp',
  24. 'engineVersion',
  25. 'device'
  26. ]
  27. KEY_CONDITION_HEADERS = "conditionHeaders"
  28. KEY_RESULT_HEADERS = "resultHeaders"
  29. KEY_RESULTS = "results"
  30. START_COL_INDEX = 0
  31. START_ROW_INDEX = 0
  32. class KnownException(Exception):
  33. pass
  34. class Convertor:
  35. def __init__(self, src_path, output_path=None):
  36. self.src_path = self.change_to_abspath(src_path)
  37. if not os.path.exists(self.src_path):
  38. raise KnownException('%s is not existed!' % self.src_path)
  39. if output_path is None:
  40. # not specified output path, default use source path
  41. if os.path.isfile(self.src_path):
  42. self.output_path = os.path.dirname(self.src_path)
  43. else:
  44. self.output_path = self.src_path
  45. else:
  46. self.output_path = self.change_to_abspath(output_path)
  47. def change_to_abspath(self, path):
  48. ret = os.path.expanduser(path)
  49. if not os.path.isabs(ret):
  50. ret = os.path.abspath(ret)
  51. ret = os.path.normpath(ret)
  52. return ret
  53. def get_col_width(self, col_str):
  54. return 256 * (len(col_str) + 1)
  55. def convert_file(self, file_path):
  56. f = open(file_path)
  57. testData = json.load(f)
  58. f.close()
  59. basename, ext = os.path.splitext(os.path.basename(file_path))
  60. dst_file_path = os.path.join(self.output_path, "%s.xls" % basename)
  61. if os.path.isfile(dst_file_path):
  62. os.remove(dst_file_path)
  63. workbook = xlwt.Workbook(encoding = 'ascii')
  64. default_style = xlwt.Style.easyxf(DEFAULT_STYLE)
  65. con_style = xlwt.Style.easyxf("%s%s" % (DEFAULT_STYLE, CONDITION_STYLE))
  66. ret_style = xlwt.Style.easyxf("%s%s" % (DEFAULT_STYLE, RESULT_STYLE))
  67. for key in testData.keys():
  68. if key in BASE_KEYS:
  69. continue
  70. # create a sheet for the test case
  71. sheetObj = workbook.add_sheet(key)
  72. # get test case data
  73. caseInfo = testData[key]
  74. # Add headers for the test case
  75. condHeaders = caseInfo[KEY_CONDITION_HEADERS]
  76. retHeaders = caseInfo[KEY_RESULT_HEADERS]
  77. curRow = START_ROW_INDEX
  78. curCol = START_COL_INDEX
  79. col_widths = {}
  80. for header in (condHeaders + retHeaders):
  81. sheetObj.write(curRow, curCol, header, default_style)
  82. col_width = self.get_col_width(header)
  83. col_widths[curCol] = col_width
  84. sheetObj.col(curCol).width = col_width
  85. curCol += 1
  86. rets = caseInfo[KEY_RESULTS]
  87. for retInfo in rets:
  88. curRow += 1
  89. curCol = START_COL_INDEX
  90. for ret in retInfo:
  91. if (curCol - START_COL_INDEX) < len(condHeaders):
  92. use_style = con_style
  93. else:
  94. use_style = ret_style
  95. sheetObj.write(curRow, curCol, ret, use_style)
  96. new_width = self.get_col_width(ret)
  97. old_width = col_widths[curCol]
  98. if new_width > old_width:
  99. sheetObj.col(curCol).width = new_width
  100. col_widths[curCol] = new_width
  101. curCol += 1
  102. workbook.save(dst_file_path)
  103. print("%s is generated." % dst_file_path)
  104. def do_convert(self):
  105. if not os.path.exists(self.output_path):
  106. os.makedirs(self.output_path)
  107. if os.path.isfile(self.src_path):
  108. self.convert_file(self.src_path)
  109. else:
  110. for f in os.listdir(self.src_path):
  111. full_path = os.path.join(self.src_path, f)
  112. ignore, ext = os.path.splitext(f)
  113. if os.path.isfile(full_path) and ext == '.json':
  114. self.convert_file(full_path)
  115. if __name__ == '__main__':
  116. parser = ArgumentParser(description="Performance test data convertor.")
  117. parser.add_argument('-s', dest='src_path', required=True, help='Specify the json file path or the folder path of json files.')
  118. parser.add_argument('-o', dest='output_path', help='Specify the output path of excel files.')
  119. (args, unknown) = parser.parse_known_args()
  120. try:
  121. convertor = Convertor(args.src_path, args.output_path)
  122. convertor.do_convert()
  123. except Exception as e:
  124. if e.__class__.__name__ == "KnownException":
  125. print(' '.join(e.args))
  126. else:
  127. raise