# # Metrix++, Copyright 2009-2013, Metrix++ Project # Link: http://metrixplusplus.sourceforge.net # # This file is a part of Metrix++ Tool. # # Metrix++ is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3 of the License. # # Metrix++ is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Metrix++. If not, see . # import logging import cgi import core.log import core.cmdparser import core.db.post import core.db.loader import core.api class Tool(core.api.ITool): def run(self, tool_args): return main(tool_args) def main(tool_args): log_plugin = core.log.Plugin() db_plugin = core.db.post.Plugin() parser = core.cmdparser.MultiOptionParser(usage="Usage: %prog debug [options] -- [path 1] ... [path N]") log_plugin.declare_configuration(parser) db_plugin.declare_configuration(parser) parser.add_option("-m", "--general.mode", default='dumphtml', choices=['dumphtml'], help="'dumphtml' - prints html code with code highlights for each given path [default: %default]") (options, args) = parser.parse_args(tool_args) log_plugin.configure(options) db_plugin.configure(options) loader = core.db.loader.Loader() loader.open_database(db_plugin.dbfile) if options.__dict__['general.mode'] == 'dumphtml': return dumphtml(args, loader) assert(False) def dumphtml(args, loader): exit_code = 0 result = "" result += '' for path in args: data = loader.load_file_data(path) if data == None: logging.error("Specified path '" + path + "' is invalid (not found in the database records)") exit_code += 1 continue file_name = data.get_path() fh = open(file_name, 'r') if fh == None: logging.error("can not open file '" + path + "' for reading") exit_code += 1 continue text = fh.read() fh.close() result += '
'
        last_pos = 0
        for marker in data.iterate_markers():
            result += (cgi.escape(text[last_pos:marker.begin]))
            if marker.get_type() == data.get_marker_types().STRING:
                result += ('')
            elif marker.get_type() == data.get_marker_types().COMMENT:
                result += ('')
            elif marker.get_type() == data.get_marker_types().PREPROCESSOR:
                result += ('')
            result += (cgi.escape(text[marker.begin:marker.end]))
            result += ('')
            last_pos = marker.end
        result += (cgi.escape(text[last_pos:]))
        last_pos = 0
        result += ('
')
        styles = [('',
                  ''),
                  ('',
                  ''),
                  ('',
                  ''),
                  ('',
                  ''),
                  ('',
                  ''),
                  ('',
                  ''),
                  ('',
                  ''),
                  ('',
                  '')]
        
        def proc_rec(region_id, file_data, styles, indent, pos):
            result = (styles[indent % len(styles)][pos % 2])
            region = file_data.get_region(region_id)
            result += ('')
            last_pos = region.get_offset_begin() 
            for (ind, sub_id) in enumerate(file_data.get_region(region_id).iterate_subregion_ids()):
                subregion = file_data.get_region(sub_id)
                result += (cgi.escape(text[last_pos:subregion.get_offset_begin()]))
                result += proc_rec(sub_id, file_data, styles, indent + 3, ind)
                last_pos = subregion.get_offset_end()
            result += (cgi.escape(text[last_pos:region.get_offset_end()]))
            result += ('')
            return result
        result += proc_rec(1, data, styles, 0, 0)
        result += ('
') result += ('') print result return exit_code