def apply(self, mh, cfg, filename, full_text, lines): if len(lines) >= 2 and lines[-1] == "": mh.style_issue(Location(filename, len(lines)), "trailing blank lines at end of file", self.autofix) elif len(full_text) and full_text[-1] != "\n": mh.style_issue(Location(filename, len(lines)), "file should end with a new line", self.autofix)
def apply(self, mh, cfg, filename, line_no, line): if line.endswith(" "): if len(line.strip()) == 0: mh.style_issue(Location(filename, line_no), "whitespace on blank line", self.autofix) else: mh.style_issue( Location(filename, line_no, len(line.rstrip()), len(line), line), "trailing whitespace", self.autofix)
def apply(self, mh, cfg, filename, line_no, line): if len(line) > cfg["line_length"]: mh.style_issue( Location(filename, line_no, cfg["line_length"], len(line), line), "line exceeds %u characters" % cfg["line_length"], self.autofix)
def apply(self, mh, cfg, filename, line_no, line): if len(line.strip()): self.is_blank = False elif self.is_blank: mh.style_issue(Location(filename, line_no), "more than one consecutive blank line", self.autofix) else: self.is_blank = True
def get_content(self): try: with open(self.filename, "r", encoding=self.encoding) as fd: return fd.read() except UnicodeDecodeError: pass if self.encoding != "utf-8": self.mh.warning( Location(self.filename), "encoding error for %s, assuming utf-8 instead" % self.encoding) self.encoding = "utf-8" try: with open(self.filename, "r", encoding=self.encoding) as fd: return fd.read() except UnicodeDecodeError: self.mh.error(Location(self.filename), "cannot read file, encoding error")
def match(self, kind, value=None): self.next() if self.ct is None: self.mh.error(Location(self.lexer.filename), "expected %s, reached EOF instead" % kind) elif self.ct.kind != kind: self.mh.error( self.ct.location, "expected %s, found %s instead" % (kind, self.ct.kind)) elif value and self.ct.value != value: self.mh.error( self.ct.location, "expected %s(%s), found %s(%s) instead" % (kind, value, self.ct.kind, self.ct.value))
def register_parent(dirname, find_roots): if dirname in CONFIG_TREE: return # Stop if we reach the root filesystem or a .git directory parent = os.path.dirname(dirname) if find_roots: is_root = parent == dirname else: is_root = False if not is_root: register_parent(parent, find_roots) CONFIG_TREE[parent]["children"].add(dirname) config_name = None if not options.ignore_config: for filename in CONFIG_FILENAMES: if os.path.isfile(os.path.join(dirname, filename)): if config_name is None: config_name = filename else: mh.register_file("directory " + os.path.relpath(dirname)) mh.error(Location("directory " + os.path.relpath(dirname)), "cannot have both a %s and %s config file" % (config_name, filename)) CONFIG_TREE[dirname] = { "children" : set(), "subtree" : False, "has_config" : config_name, "root" : is_root, "parent" : None if is_root else parent }
def loc(self): return Location(self.get_container().filename, blockname=self.local_name())
def loc(self): return Location(self.filename)
def apply(self, mh, cfg, filename, full_text, lines): if len(lines) > cfg["file_length"]: mh.style_issue(Location(filename, len(lines)), "file exceeds %u lines" % cfg["file_length"], self.autofix)
def apply(self, mh, cfg, filename, line_no, line): if "\t" in line: mh.style_issue( Location(filename, line_no, line.index("\t"), line.index("\t"), line), "tab is not allowed", self.autofix)
def collect_metrics(args): mh, filename, _, _, cfg = args assert isinstance(filename, str) metrics = {filename: {"errors": False, "metrics": {}, "functions": {}}} if not cfg["enable"]: mh.register_exclusion(filename) return False, filename, mh, metrics mh.register_file(filename) # Do some file-based sanity checking try: if not os.path.exists(filename): mh.error(Location(filename), "file does not exist") if not os.path.isfile(filename): mh.error(Location(filename), "is not a file") except Error: metrics[filename]["errors"] = True return True, filename, mh, metrics # Create lexer try: lexer = MATLAB_Lexer(mh, filename, encoding="cp1252") except UnicodeDecodeError: lexer = MATLAB_Lexer(mh, filename, encoding="utf8") if cfg["octave"]: lexer.set_octave_mode() if cfg["ignore_pragmas"]: lexer.process_pragmas = False # We're dealing with an empty file here. Lets just not do anything if len(lexer.text.strip()) == 0: return True, filename, mh, metrics # Create parse tree try: parser = MATLAB_Parser(mh, lexer, cfg) parse_tree = parser.parse_file() except Error: metrics[filename]["errors"] = True return True, filename, mh, metrics # File metrics metrics[filename]["metrics"] = { "file_length": { "measure": lexer.line_count(), "limit": None, "reason": None } } justifications = {filename: get_file_justifications(mh, parse_tree)} # Check+justify file metrics for file_metric in config.FILE_METRICS: check_metric(mh, cfg, Location(filename), file_metric, metrics[filename]["metrics"], justifications[filename]) # Collect, check, and justify function metrics metrics[filename]["functions"] = get_function_metrics(mh, cfg, parse_tree) # Complain about unused justifications warn_unused_justifications(mh, parse_tree) # Return results return True, filename, mh, metrics