debug.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. import logging
  8. import cgi
  9. import mpp.api
  10. import mpp.utils
  11. class Plugin(mpp.api.Plugin, mpp.api.IConfigurable, mpp.api.IRunable):
  12. def declare_configuration(self, parser):
  13. parser.add_option("-m", "--mode", default='dumphtml', choices=['dumphtml'],
  14. help="'dumphtml' - prints html code with code highlights for each given path [default: %default]")
  15. def configure(self, options):
  16. self.mode = options.__dict__['mode']
  17. def run(self, args):
  18. loader = self.get_plugin('mpp.dbf').get_loader()
  19. if self.mode == 'dumphtml':
  20. return dumphtml(args, loader)
  21. assert(False)
  22. def dumphtml(args, loader):
  23. exit_code = 0
  24. result = ""
  25. result += '<html><body>'
  26. for path in args:
  27. path = mpp.utils.preprocess_path(path)
  28. data = loader.load_file_data(path)
  29. if data == None:
  30. mpp.utils.report_bad_path(path)
  31. exit_code += 1
  32. continue
  33. file_name = data.get_path()
  34. fh = open(file_name, 'r')
  35. if fh == None:
  36. logging.error("can not open file '" + path + "' for reading")
  37. exit_code += 1
  38. continue
  39. text = fh.read()
  40. fh.close()
  41. # TODO fix highlightning of markers
  42. # result += '<table><tr><td><pre>'
  43. # last_pos = 0
  44. # for marker in data.iterate_markers(filter_group= mpp.api.Marker.T.COMMENT |
  45. # mpp.api.Marker.T.STRING |
  46. # mpp.api.Marker.T.PREPROCESSOR):
  47. # result += (cgi.escape(text[last_pos:marker.begin]))
  48. # if marker.get_type() == mpp.api.Marker.T.STRING:
  49. # result += ('<span style="color:#0000FF">')
  50. # elif marker.get_type() == mpp.api.Marker.T.COMMENT:
  51. # result += ('<span style="color:#009900">')
  52. # elif marker.get_type() == mpp.api.Marker.T.PREPROCESSOR:
  53. # result += ('<span style="color:#990000">')
  54. # else:
  55. # assert False, "Uknown marker type"
  56. # result += (cgi.escape(text[marker.begin:marker.end]))
  57. # result += ('</span>')
  58. # last_pos = marker.end
  59. # result += (cgi.escape(text[last_pos:]))
  60. # result += ('</pre></td><td><pre>')
  61. result += '<table><tr><td><pre>'
  62. styles = [('<span style="background-color:#F0F010">',
  63. '<span style="background-color:#F010F0">'),
  64. ('<span style="background-color:#F0F030">',
  65. '<span style="background-color:#F030F0">'),
  66. ('<span style="background-color:#F0F050">',
  67. '<span style="background-color:#F050F0">'),
  68. ('<span style="background-color:#F0F070">',
  69. '<span style="background-color:#F070F0">'),
  70. ('<span style="background-color:#F0F090">',
  71. '<span style="background-color:#F090F0">'),
  72. ('<span style="background-color:#F0F0B0">',
  73. '<span style="background-color:#F0B0F0">'),
  74. ('<span style="background-color:#F0F0D0">',
  75. '<span style="background-color:#F0D0F0">'),
  76. ('<span style="background-color:#F0F0E0">',
  77. '<span style="background-color:#F0E0F0">')]
  78. def proc_rec(region_id, file_data, styles, indent, pos):
  79. result = (styles[indent % len(styles)][pos % 2])
  80. region = file_data.get_region(region_id)
  81. result += ('<a href="#line' + str(region.get_cursor()) + '" id=line"' + str(region.get_cursor()) + '"></a>')
  82. last_pos = region.get_offset_begin()
  83. for (ind, sub_id) in enumerate(file_data.get_region(region_id).iterate_subregion_ids()):
  84. subregion = file_data.get_region(sub_id)
  85. result += (cgi.escape(text[last_pos:subregion.get_offset_begin()]))
  86. result += proc_rec(sub_id, file_data, styles, indent + 3, ind)
  87. last_pos = subregion.get_offset_end()
  88. result += (cgi.escape(text[last_pos:region.get_offset_end()]))
  89. result += ('</span>')
  90. return result
  91. result += proc_rec(1, data, styles, 0, 0)
  92. result += ('</pre></td></tr></table>')
  93. result += ('</body></html>')
  94. print result
  95. return exit_code