Skip to content

Commit

Permalink
Add config infrastructure and file-filter config
Browse files Browse the repository at this point in the history
-c/--config can be used to add configuration for the parsers. It can
 be used repeatedly and each time have a key=value pair

As first config setting, add "file-filter" that will check for a
prefix in the filename and filter that out.
  • Loading branch information
wallento committed Jun 19, 2020
1 parent 72247cd commit f3ea6fb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
10 changes: 8 additions & 2 deletions edalogparser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def __init__(self, *, severity=None, msg=None, file=None, line=None, col=None, c
self.line = line
self.col = col
self.code = code

def as_dict(self, full=False):
d = { "severity": self.severity, "msg": self.msg }
if self.file is not None or full:
Expand Down Expand Up @@ -67,10 +68,15 @@ def as_ghaction(self):
return "\n".join([l.as_ghaction() for l in self])

class LogParser(ABC):
def __init__(self):
pass
def __init__(self, config):
self.config = config if config else {}

@abstractmethod
def parse(self, log):
pass

def filter_filename(self, name):
if "file-filter" in self.config:
if name.startswith(self.config["file-filter"]):
return name[len(self.config["file-filter"]):]
return name
12 changes: 11 additions & 1 deletion edalogparser/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@
"verilator": VerilatorLogParser,
"vivado": VivadoLogParser }

class parse_dict_args(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
d = {}
for val in values:
k, v = val.split('=')
d[k] = v
setattr(namespace, self.dest, d)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--tool", required=True, choices=tools.keys())
parser.add_argument("-f", "--format", choices=["json", "azure", "ghaction"], default="json")
parser.add_argument("-c", "--config", nargs='*', action=parse_dict_args)
parser.add_argument("input", nargs='?', help="", type=argparse.FileType('r'),
default=sys.stdin)
args = parser.parse_args()

print(tools[args.tool]().parse(args.input.readlines()).get_as(args.format))
print(tools[args.tool](args.config).parse(args.input.readlines()).get_as(args.format))
6 changes: 3 additions & 3 deletions edalogparser/verible.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
class VeribleLintLogParser(LogParser):
regex = re.compile(r"^(.*?):(\d+):(\d+): (.*) \[Style: (.*?)\]")

def __init__(self):
super().__init__()
def __init__(self, config):
super().__init__(config)

def parse(self, log):
entries = Log()
Expand All @@ -14,7 +14,7 @@ def parse(self, log):
if m:
severity = "warning"
msg = m.group(4)
file = m.group(1)
file = self.filter_filename(m.group(1))
line = m.group(2)
col = m.group(3)
code = m.group(5)
Expand Down
6 changes: 3 additions & 3 deletions edalogparser/verilator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class VerilatorLogParser(LogParser):
regex_nofl = re.compile(r"%((Warning|Error)-\w+: ([^%]*))")
regex_fl = re.compile(r"(.*?):(\d+):(\d+): (.*)")

def __init__(self):
super().__init__()
def __init__(self, config):
super().__init__(config)

def parse(self, log):
log = "".join(log)
Expand All @@ -16,7 +16,7 @@ def parse(self, log):
msg = m.group(3).replace("\n", "\\n")
m = self.regex_fl.match(msg)
if m:
file = m.group(1)
file = self.filter_filename(m.group(1))
line = int(m.group(2))
col = int(m.group(3))
msg = m.group(4)
Expand Down
6 changes: 3 additions & 3 deletions edalogparser/vivado.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
class VivadoLogParser(LogParser):
regex = re.compile(r"^((INFO|WARNING|ERROR): \[(.*?)\] (.*?)( \[(.*?)\])?)\n$")

def __init__(self):
super().__init__()
def __init__(self, config):
super().__init__(config)

def parse(self, log):
entries = Log()
Expand All @@ -18,7 +18,7 @@ def parse(self, log):
line = None
if m.group(6):
colon = m.group(6).rfind(":")
file = m.group(6)[0:colon]
file = self.filter_filename(m.group(6)[0:colon])
line = m.group(6)[colon+1:]
code = m.group(3)
entries.append(LogEntry(severity=severity, msg=msg, file=file, line=line, code=code))
Expand Down

0 comments on commit f3ea6fb

Please sign in to comment.