debug.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. # TODO fix highlightning of markers
  54. # result += '<table><tr><td><pre>'
  55. # last_pos = 0
  56. # for marker in data.iterate_markers(filter_group= mpp.api.Marker.T.COMMENT |
  57. # mpp.api.Marker.T.STRING |
  58. # mpp.api.Marker.T.PREPROCESSOR):
  59. # result += (cgi.escape(text[last_pos:marker.begin]))
  60. # if marker.get_type() == mpp.api.Marker.T.STRING:
  61. # result += ('<span style="color:#0000FF">')
  62. # elif marker.get_type() == mpp.api.Marker.T.COMMENT:
  63. # result += ('<span style="color:#009900">')
  64. # elif marker.get_type() == mpp.api.Marker.T.PREPROCESSOR:
  65. # result += ('<span style="color:#990000">')
  66. # else:
  67. # assert False, "Uknown marker type"
  68. # result += (cgi.escape(text[marker.begin:marker.end]))
  69. # result += ('</span>')
  70. # last_pos = marker.end
  71. # result += (cgi.escape(text[last_pos:]))
  72. # result += ('</pre></td><td><pre>')
  73. result += '<table><tr><td><pre>'
  74. styles = [('<span style="background-color:#F0F010">',
  75. '<span style="background-color:#F010F0">'),
  76. ('<span style="background-color:#F0F030">',
  77. '<span style="background-color:#F030F0">'),
  78. ('<span style="background-color:#F0F050">',
  79. '<span style="background-color:#F050F0">'),
  80. ('<span style="background-color:#F0F070">',
  81. '<span style="background-color:#F070F0">'),
  82. ('<span style="background-color:#F0F090">',
  83. '<span style="background-color:#F090F0">'),
  84. ('<span style="background-color:#F0F0B0">',
  85. '<span style="background-color:#F0B0F0">'),
  86. ('<span style="background-color:#F0F0D0">',
  87. '<span style="background-color:#F0D0F0">'),
  88. ('<span style="background-color:#F0F0E0">',
  89. '<span style="background-color:#F0E0F0">')]
  90. def proc_rec(region_id, file_data, styles, indent, pos):
  91. result = (styles[indent % len(styles)][pos % 2])
  92. region = file_data.get_region(region_id)
  93. result += ('<a href="#line' + str(region.get_cursor()) + '" id=line"' + str(region.get_cursor()) + '"></a>')
  94. last_pos = region.get_offset_begin()
  95. for (ind, sub_id) in enumerate(file_data.get_region(region_id).iterate_subregion_ids()):
  96. subregion = file_data.get_region(sub_id)
  97. result += (cgi.escape(text[last_pos:subregion.get_offset_begin()]))
  98. result += proc_rec(sub_id, file_data, styles, indent + 3, ind)
  99. last_pos = subregion.get_offset_end()
  100. result += (cgi.escape(text[last_pos:region.get_offset_end()]))
  101. result += ('</span>')
  102. return result
  103. result += proc_rec(1, data, styles, 0, 0)
  104. result += ('</pre></td></tr></table>')
  105. result += ('</body></html>')
  106. print result
  107. return exit_code