dumper.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://swi.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 core.api
  20. import re
  21. class Plugin(core.api.Plugin, core.api.Child, core.api.IConfigurable):
  22. POST_NAME = '.ss.std.code.dumper.html'
  23. def declare_configuration(self, parser):
  24. parser.add_option("--std.code.dumper.on", action="store_true", default=False,
  25. help="If the option is set (True), HTML files are generated for every parsed file containing code (for troubleshooting purposes only) [default: %default]")
  26. def configure(self, options):
  27. self.dump_html = options.__dict__['std.code.dumper.on']
  28. def initialize(self):
  29. if self.dump_html == True:
  30. core.api.subscribe_by_parents_interface(core.api.ICode, self)
  31. # do not process files dumped by previous run of this module
  32. self.get_plugin_loader().get_plugin('core.dir').add_exclude_rule(re.compile(r'.*' + Plugin.POST_NAME + r'$'))
  33. def callback(self, parent, data):
  34. file_name = data.get_path()
  35. text = data.get_content()
  36. import cgi
  37. f = open(file_name + Plugin.POST_NAME, 'w')
  38. f.write('<html><body><table><tr><td><pre>')
  39. last_pos = 0
  40. for marker in data.iterate_markers():
  41. f.write(cgi.escape(text[last_pos:marker.begin]))
  42. if marker.get_type() == data.get_marker_types().STRING:
  43. f.write('<span style="color:#0000FF">')
  44. elif marker.get_type() == data.get_marker_types().COMMENT:
  45. f.write('<span style="color:#009900">')
  46. elif marker.get_type() == data.get_marker_types().PREPROCESSOR:
  47. f.write('<span style="color:#990000">')
  48. f.write(cgi.escape(text[marker.begin:marker.end]))
  49. f.write('</span>')
  50. last_pos = marker.end
  51. f.write(cgi.escape(text[last_pos:]))
  52. last_pos = 0
  53. f.write('</pre></td><td><pre>')
  54. styles = ['<span style="background-color:#ffff80">', '<span style="background-color:#ff80ff">']
  55. for item in enumerate(data.iterate_regions(filter_group=data.get_region_types().FUNCTION)):
  56. reg = item[1]
  57. f.write(cgi.escape(text[last_pos:reg.get_offset_begin()]))
  58. f.write(styles[item[0] % 2])
  59. f.write('<a href="#line' + str(reg.get_cursor()) + '" id=line"' + str(reg.get_cursor()) + '"></a>')
  60. f.write(cgi.escape(text[reg.get_offset_begin():reg.get_offset_end()]))
  61. f.write('</span>')
  62. last_pos = reg.get_offset_end()
  63. f.write(cgi.escape(text[last_pos:]))
  64. f.write('</pre></td></tr></table></body></html>')
  65. f.close()