debug.py 5.0 KB

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