def parse(self, file): """Parses a log from the given input stream, which is supposed to deliver an XES log in XML representation. :param file: file generated by the function 'open(path)', which is supposed to deliver an XES log in XML representation. :type file: _io.TextIOWrapper :return: The parsed list of logs. :rtype: list[XLog] """ handler = XesXmlParser.XesXmlHandler() xml_parse(file, handler) return [handler.get_log()]
def parse(self, file): """Parses a set of logs from the given input stream, which is supposed to deliver an MXML serialization. :param file: file generated by the function 'open(path)', which is supposed to deliver an MXML serialization. :type file: _io.TextIOWrapper :return: The parsed list of logs. :rtype: list[XLog] """ handler = XMxmlParser.MxmlHandler() xml_parse(file, handler) return handler.get_logs()
def parse(file): """Parses an extension from a definition file. :param file: The path of the file containing the extension or url string which represents the extension definition file.. :type file: str :return: The extension object, as defined in the provided file. :rtype: XExtension """ handler = XExtensionParser.XExtensionHandler() if os.path.isfile(file): with open(file) as data: xml_parse(data, handler) elif not os.path.isdir(file): parseString(request.urlopen(file).read(), handler) return handler.get_extension()
source_dir = options.source_dir # Get the stream that we read cppcheck errors from. input_file = sys.stdin if options.file: if not os.path.exists(options.file): parser.error('cppcheck xml file: %s not found.' % options.file) input_file = io.open(options.file, 'r') else: parser.error('No cppcheck xml file specified. (--file=)') # Parse the xml file and produce a simple list of errors. print('Parsing xml report.') try: contentHandler = CppCheckHandler() xml_parse(input_file, contentHandler) except XmlParseException as msg: print('Failed to parse cppcheck xml file: %s' % msg) sys.exit(1) # We have a list of errors. But now we want to group them on # each source code file. Lets create a files dictionary that # will contain a list of all the errors in that file. For each # file we will also generate a HTML filename to use. files = {} file_no = 0 for error in contentHandler.errors: filename = error['file'] if filename not in files.keys(): files[filename] = { 'errors': [],
source_dir = os.getcwd() if options.source_dir: source_dir = options.source_dir # Get the stream that we read cppcheck errors from. input_file = sys.stdin if options.file: if not os.path.exists(options.file): parser.error('cppcheck xml file: %s not found.' % options.file) input_file = io.open(options.file, 'r') # Parse the xml file and produce a simple list of errors. print('Parsing xml report.') try: contentHandler = CppCheckHandler() xml_parse(input_file, contentHandler) except XmlParseException as msg: print('Failed to parse cppcheck xml file: %s' % msg) sys.exit(1) # We have a list of errors. But now we want to group them on # each source code file. Lets create a files dictionary that # will contain a list of all the errors in that file. For each # file we will also generate a HTML filename to use. files = {} file_no = 0 for error in contentHandler.errors: filename = error['file'] if filename not in files.keys(): files[filename] = { 'errors': [], 'htmlfile': str(file_no) + '.html'}
def unserialize(self, context): xml_parse(context.xmlfile, MoinContentHandler(self, context))
def main(): # Configure all the options this little utility is using. parser = optparse.OptionParser() parser.add_option('--title', dest='title', help='The title of the project.', default='[project name]') parser.add_option('--file', dest='file', help='The cppcheck xml output file to read defects ' 'from. Default is reading from stdin.') parser.add_option('--report-dir', dest='report_dir', help='The directory where the HTML report content is ' 'written.') parser.add_option('--source-dir', dest='source_dir', help='Base directory where source code files can be ' 'found.') parser.add_option('--source-encoding', dest='source_encoding', help='Encoding of source code.', default='utf-8') # Parse options and make sure that we have an output directory set. options, args = parser.parse_args() if not options.report_dir: parser.error('No report directory set.') # Get the directory where source code files are located. source_dir = os.getcwd() if options.source_dir: source_dir = options.source_dir # Get the stream that we read cppcheck errors from. input_file = sys.stdin if options.file: if not os.path.exists(options.file): parser.error('cppcheck xml file: %s not found.' % options.file) input_file = io.open(options.file, 'r') # Parse the xml file and produce a simple list of errors. print('Parsing xml report.') try: contentHandler = CppCheckHandler() xml_parse(input_file, contentHandler) except XmlParseException as msg: print('Failed to parse cppcheck xml file: %s' % msg) sys.exit(1) # We have a list of errors. But now we want to group them on # each source code file. Lets create a files dictionary that # will contain a list of all the errors in that file. For each # file we will also generate a HTML filename to use. files = {} file_no = 0 error_sort = ["Info","Minor","Major","Critical","Blocker"] error_count = [] error_total = 0 Infonum = 0 Minornum = 0 Majornum = 0 Criticalnum = 0 Blockernum = 0 for error in contentHandler.errors: filename = error['file'] state = "unchanged" if filename not in files.keys(): files[filename] = { 'errors': [], 'htmlfile': str(file_no) + '.html'} file_no = file_no + 1 files[filename]['errors'].append(error) severity = error['severity'] if severity=='Info': Infonum = Infonum + 1 error_total = error_total + 1 elif severity=='Major': Majornum = Majornum + 1 error_total = error_total + 1 elif severity=='Critical': Criticalnum = Criticalnum + 1 error_total = error_total + 1 if state=='new': Criticalnumnew = Criticalnumnew +1 elif severity=='Blocker': Blockernum = Blockernum + 1 error_total = error_total + 1 elif severity=='Minor': Minornum = Minornum + 1 error_total = error_total + 1 error_count.append(Infonum) error_count.append(Minornum) error_count.append(Majornum) error_count.append(Criticalnum) error_count.append(Blockernum) # Make sure that the report directory is created if it doesn't exist. print('Creating %s directory' % options.report_dir) if not os.path.exists(options.report_dir): os.mkdir(options.report_dir) # Generate a HTML file with syntax highlighted source code for each # file that contains one or more errors. print('Processing errors') for filename, data in files.items(): htmlfile = data['htmlfile'] errors = data['errors'] lines = [] for error in errors: lines.append(error['line']) if filename == '': continue source_filename = os.path.join(source_dir, filename) try: with open(source_filename, 'r') as input_file: Text = input_file.read() enc = chardet.detect(Text)['encoding'] if enc is None: enc=options.source_encoding content=Text.decode(enc,'ignore') except IOError: sys.stderr.write("ERROR: Source file '%s' not found.\n" % source_filename) continue htmlFormatter = AnnotateCodeFormatter(linenos=True, style='colorful', hl_lines=lines, lineanchors='line', encoding=options.source_encoding) htmlFormatter.errors = errors with io.open(os.path.join(options.report_dir, htmlfile), 'w') as output_file: output_file.write(HTML_HEAD % (options.title, htmlFormatter.get_style_defs('.highlight'), options.title)) lexer = guess_lexer_for_filename(source_filename, '') if options.source_encoding: lexer.encoding = options.source_encoding output_file.write( highlight(content, lexer, htmlFormatter).decode( options.source_encoding)) output_file.write(HTML_FOOTER) # print(' ' + filename) # Generate a master index.html file that will contain a list of # all the errors created. print('Creating index.html') with io.open(os.path.join(options.report_dir, 'index.html'), 'w') as output_file: output_file.write(HTML_HEAD % (options.title, '', options.title)) output_file.write('<table border="1px" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">') output_file.write('<tr><th>Total</th></tr>') output_file.write("<tr><td>%s</td></tr>" %(error_total)) output_file.write('</table>') output_file.write('</br>') output_file.write('</br>') output_file.write('<table border="1px" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">') if error_total>0: output_file.write('<tr><th>File</th><th>Line</th><th>Type</th><th>Message</th></tr>') for filename, data in files.items(): if filename.strip() != '': output_file.write( "<tr><td rowspan='%s'><a href='%s'>%s</a></td><td><a href='%s#line-%d'>%d</a></td> \ <td>%s</td><td>%s</td></tr>" % (len(data['errors']),data['htmlfile'], filename,data['htmlfile'], data['errors'][0]['line'],data['errors'][0]['line'], data['errors'][0]['id'],data['errors'][0]['msg'])) for i in range(1,len(data['errors'])): output_file.write( "<tr><td><a href='%s#line-%d'>%d</a></td><td>%s</td><td>%s</td></tr>" % (data['htmlfile'], data['errors'][i]['line'], data['errors'][i]['line'], data['errors'][i]['id'], data['errors'][i]['msg'])) output_file.write('</table>') output_file.write(HTML_FOOTER) print('Creating style.css file') with io.open(os.path.join(options.report_dir, 'style.css'), 'w') as css_file: css_file.write(STYLE_FILE)