def format_message(self, kind, message, startlineno, endlineno, startcolno=0, endcolno=-1): back_index = {'warning': 2, 'error': 3, 'info': 0}.get(kind.lower(), 3) r = ['%s while processing %r (mode=%r)..' % (kind, self.id, self.mode)] for i in range(max(1, startlineno - back_index), startlineno): r.append('%5d:%s' % (i, self.source_lines[i - 1])) for i in range(startlineno, min(endlineno + back_index, len(self.source_lines)) + 1): if i == 0 and not self.source_lines: break linenostr = '%5d:' % (i) if i == endlineno: sourceline = self.source_lines[i - 1] l0 = linenostr + sourceline[:startcolno] if endcolno == -1: l1 = sourceline[startcolno:] l2 = '' else: l1 = sourceline[startcolno:endcolno] l2 = sourceline[endcolno:] r.append('%s%s%s <== %s' % (l0, yellow_text(l1), l2, red_text(message))) else: r.append(linenostr + self.source_lines[i - 1]) return '\n'.join(r)
def analyze(self): if self.is_analyzed: return if self.block is None: self.reader.show_message('Nothing to analyze.') return try: self.block.analyze() except AnalyzeError: pass except Exception, msg: if str(msg) != '123454321': traceback.print_exc(file=sys.stderr) self.reader.show_message(red_text('FATAL ERROR: STOPPED ANALYSING %r CONTENT' % (self.reader.source) ), sys.stderr) sys.exit(123454321) return
def next(self, ignore_comments=False): try: if self.reader is not None: try: return self.reader.next() except StopIteration: self.reader = None item = self._next(ignore_comments) if isinstance(item, Line) and _is_include_line(item.line): reader = item.reader filename = item.line.strip()[7:].lstrip()[1:-1] include_dirs = self.include_dirs[:] path = filename for incl_dir in include_dirs: path = os.path.join(incl_dir, filename) if os.path.exists(path): break if not os.path.isfile(path): dirs = os.pathsep.join(include_dirs) message = reader.format_message(\ 'WARNING', 'include file %r not found in %r,'\ ' ignoring.' % (filename, dirs), item.span[0], item.span[1]) reader.show_message(message, sys.stdout) return self.next(ignore_comments=ignore_comments) message = reader.format_message('INFORMATION', 'found file %r' % (path), item.span[0], item.span[1]) reader.show_message(message, sys.stdout) self.reader = FortranFileReader(path, include_dirs=include_dirs) return self.reader.next(ignore_comments=ignore_comments) return item except StopIteration: raise except: message = self.format_message('FATAL ERROR', 'while processing line', self.linecount, self.linecount) self.show_message(message, sys.stdout) traceback.print_exc(file=sys.stdout) self.show_message(red_text('STOPPED READING'), sys.stdout) raise StopIteration
def parse(self): if self.block is not None: return try: block = self.block = BeginSource(self) except KeyboardInterrupt: raise except: reader = self.reader while reader is not None: message = reader.format_message('FATAL ERROR', 'while processing line', reader.linecount, reader.linecount) reader.show_message(message, sys.stderr) reader = reader.reader traceback.print_exc(file=sys.stderr) self.reader.show_message(red_text('STOPPED PARSING'), sys.stderr) return return
def next(self, ignore_comments=False): try: if self.reader is not None: try: return self.reader.next() except StopIteration: self.reader = None item = self._next(ignore_comments) if isinstance(item, Line) and _is_include_line(item.line): reader = item.reader filename = item.line.strip()[7:].lstrip()[1:-1] include_dirs = self.include_dirs[:] path = filename for incl_dir in include_dirs: path = os.path.join(incl_dir, filename) if os.path.exists(path): break if not os.path.isfile(path): dirs = os.pathsep.join(include_dirs) message = reader.format_message( "WARNING", "include file %r not found in %r," " ignoring." % (filename, dirs), item.span[0], item.span[1], ) reader.show_message(message, sys.stdout) return self.next(ignore_comments=ignore_comments) message = reader.format_message("INFORMATION", "found file %r" % (path), item.span[0], item.span[1]) reader.show_message(message, sys.stdout) self.reader = FortranFileReader(path, include_dirs=include_dirs) return self.reader.next(ignore_comments=ignore_comments) return item except StopIteration: raise except: message = self.format_message("FATAL ERROR", "while processing line", self.linecount, self.linecount) self.show_message(message, sys.stdout) traceback.print_exc(file=sys.stdout) self.show_message(red_text("STOPPED READING"), sys.stdout) raise StopIteration
def parse(self): if self.block is not None: return try: block = self.block = BeginSource(self) except KeyboardInterrupt: raise except: reader = self.reader while reader is not None: message = reader.format_message('FATAL ERROR', 'while processing line', reader.linecount, reader.linecount) logger.critical(message) # reader.show_message(message, sys.stderr) reader = reader.reader logger.debug(''.join(('Traceback\n',''.join( traceback.format_stack() )))) # traceback.print_exc(file=sys.stderr) logger.critical(red_text('STOPPED PARSING')) # self.reader.show_message(red_text('STOPPED PARSING'), sys.stderr) return return
def format_message(self, kind, message, startlineno, endlineno, startcolno=0, endcolno=-1): back_index = {"warning": 2, "error": 3, "info": 0}.get(kind.lower(), 3) r = ["%s while processing %r (mode=%r).." % (kind, self.id, self.mode)] for i in range(max(1, startlineno - back_index), startlineno): r.append("%5d:%s" % (i, self.source_lines[i - 1])) for i in range(startlineno, min(endlineno + back_index, len(self.source_lines)) + 1): if i == 0 and not self.source_lines: break linenostr = "%5d:" % (i) if i == endlineno: sourceline = self.source_lines[i - 1] l0 = linenostr + sourceline[:startcolno] if endcolno == -1: l1 = sourceline[startcolno:] l2 = "" else: l1 = sourceline[startcolno:endcolno] l2 = sourceline[endcolno:] r.append("%s%s%s <== %s" % (l0, yellow_text(l1), l2, red_text(message))) else: r.append(linenostr + self.source_lines[i - 1]) return "\n".join(r)
def format_message(self, kind, message, startlineno, endlineno, startcolno=0, endcolno=-1): back_index = {'warning':2,'error':3,'info':0}.get(kind.lower(),3) r = ['While processing %r (mode=%r)..' % (self.id, self.mode)] for i in range(max(1,startlineno-back_index),startlineno): r.append('%5d:%s' % (i,self.source_lines[i-1])) for i in range(startlineno,min(endlineno+back_index,len(self.source_lines))+1): if i==0 and not self.source_lines: break linenostr = '%5d:' % (i) if i==endlineno: sourceline = self.source_lines[i-1] l0 = linenostr+sourceline[:startcolno] if endcolno==-1: l1 = sourceline[startcolno:] l2 = '' else: l1 = sourceline[startcolno:endcolno] l2 = sourceline[endcolno:] r.append('%s%s%s <== %s' % (l0,yellow_text(l1),l2,red_text(message))) else: r.append(linenostr+ self.source_lines[i-1]) return '\n'.join(r)
def parse(self): if self.block is not None: return try: block = self.block = BeginSource(self) except KeyboardInterrupt: raise except: reader = self.reader while reader is not None: message = reader.format_message('FATAL ERROR', 'while processing line', reader.linecount, reader.linecount) logger.critical(message) # reader.show_message(message, sys.stderr) reader = reader.reader logger.debug(''.join( ('Traceback\n', ''.join(traceback.format_stack())))) # traceback.print_exc(file=sys.stderr) logger.critical(red_text('STOPPED PARSING')) # self.reader.show_message(red_text('STOPPED PARSING'), sys.stderr) return return
def error(self, message): message = self.format_message('ERROR', red_text(message)) self.show_message(message) return
def next(self, ignore_comments = False): """ Return the next Fortran code item. Include statements are realized. Parameters ---------- ignore_comments : bool When True then act as if Fortran code does not contain any comments or blank lines. See also -------- _next, get_source_item """ try: if self.reader is not None: # inside INCLUDE statement try: return self.reader.next() except StopIteration: self.reader = None item = self._next(ignore_comments) if isinstance(item, Line) and _is_include_line(item.line): # catch INCLUDE statement and create a new FortranReader # to enter to included file. reader = item.reader filename = item.line.strip()[7:].lstrip()[1:-1] include_dirs = self.include_dirs[:] path = filename for incl_dir in include_dirs: path = os.path.join(incl_dir, filename) if os.path.exists(path): break if not os.path.isfile(path): # include file does not exist dirs = os.pathsep.join(include_dirs) # According to Fortran standard, INCLUDE line is # not a Fortran statement. reader.warning('%r not found in %r. INLCUDE line treated as comment line.'\ % (filename, dirs), item) item = self.next(ignore_comments) return item # To keep the information, we are turning the # INCLUDE line to a comment: reader.warning('%r not found in %r. '\ 'The INCLUDE line will be turned to a comment.'\ % (filename, dirs), item) return self.comment_item('!'+item.get_line(apply_map=True), item.span[0], item.span[1]) reader.info('including file %r' % (path), item) self.reader = FortranFileReader(path, include_dirs=include_dirs) return self.reader.next(ignore_comments = ignore_comments) return item except StopIteration: raise except: message = self.format_message('FATAL ERROR', 'while processing line', self.linecount, self.linecount) logger.critical(message) # self.show_message(message, sys.stderr) # traceback.print_exc(file=sys.stderr) logger.debug(''.join(('Traceback\n',''.join( traceback.format_stack())))) logger.critical(red_text('STOPPED READING')) # self.show_message(red_text('STOPPED READING'), sys.stderr) raise StopIteration
def error(self, message): message = self.format_message('ERROR', red_text(message)) logger.error(message) # self.show_message(message) return
def next(self, ignore_comments=False): """ Return the next Fortran code item. Include statements are realized. Parameters ---------- ignore_comments : bool When True then act as if Fortran code does not contain any comments or blank lines. See also -------- _next, get_source_item """ try: if self.reader is not None: # inside INCLUDE statement try: return self.reader.next() except StopIteration: self.reader = None item = self._next(ignore_comments) if isinstance(item, Line) and _is_include_line(item.line): # catch INCLUDE statement and create a new FortranReader # to enter to included file. reader = item.reader filename = item.line.strip()[7:].lstrip()[1:-1] include_dirs = self.include_dirs[:] path = filename for incl_dir in include_dirs: path = os.path.join(incl_dir, filename) if os.path.exists(path): break if not os.path.isfile(path): # include file does not exist dirs = os.pathsep.join(include_dirs) # According to Fortran standard, INCLUDE line is # not a Fortran statement. reader.warning('%r not found in %r. INLCUDE line treated as comment line.'\ % (filename, dirs), item) item = self.next(ignore_comments) return item # To keep the information, we are turning the # INCLUDE line to a comment: reader.warning('%r not found in %r. '\ 'The INCLUDE line will be turned to a comment.'\ % (filename, dirs), item) return self.comment_item( '!' + item.get_line(apply_map=True), item.span[0], item.span[1]) reader.info('including file %r' % (path), item) self.reader = FortranFileReader(path, include_dirs=include_dirs) return self.reader.next(ignore_comments=ignore_comments) return item except StopIteration: raise except: message = self.format_message('FATAL ERROR', 'while processing line', self.linecount, self.linecount) logger.critical(message) # self.show_message(message, sys.stderr) # traceback.print_exc(file=sys.stderr) logger.debug(''.join( ('Traceback\n', ''.join(traceback.format_stack())))) logger.critical(red_text('STOPPED READING')) # self.show_message(red_text('STOPPED READING'), sys.stderr) raise StopIteration
def error(self, message): message = self.format_message("ERROR", red_text(message)) logger.error(message) # self.show_message(message) return