debug.py 5.1 KB

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