Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# SPDX-License-Identifier: Apache-2.0

from runparams import RunParams
import logging

from runparams import LogCountingHandler, RunParams
from spec_parser import Model

if __name__ == "__main__":
Expand All @@ -11,3 +13,30 @@
m = Model(cfg.input_path)
if not cfg.no_output:
m.generate(cfg)

# Logger names and their labels for error reporting,
# ordered by processing sequence.
loggers = [
("spec_parser.mdparsing", "Markdown parsing"),
("spec_parser.model", "Model"),
("spec_parser.rdf", "RDF generation"),
]

total_errors = 0

for logger_name, label in loggers:
logger = logging.getLogger(logger_name)
handler = next(
(h for h in logger.handlers if isinstance(h, LogCountingHandler)), None
) # Get the first LogCountingHandler
if handler and handler.num_errors() > 0:
print(f"\n{label} errors: {handler.num_errors()}")
for idx, msg in enumerate(
sorted(handler.record[logging.ERROR]), total_errors + 1
):
print(f"{idx:3}: {msg}")
total_errors += handler.num_errors()

if total_errors > 0:
print(f"\nTotal errors: {total_errors}")
exit(1)
12 changes: 10 additions & 2 deletions runparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,19 @@ def create_output_dirs(self, force):
class LogCountingHandler(logging.StreamHandler):
def __init__(self):
super().__init__()
self.count = dict.fromkeys((logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL), 0)
__log_levels = (
logging.DEBUG,
logging.INFO,
logging.WARNING,
logging.ERROR,
logging.CRITICAL,
)
self.count = dict.fromkeys(__log_levels, 0)
self.record = {level: [] for level in __log_levels}

def emit(self, record):
self.count[record.levelno] += 1
self.record[record.levelno].append(self.format(record))
super().emit(record)

def num_critical(self):
Expand All @@ -169,4 +178,3 @@ def num_errors(self):

def num_warnings(self):
return self.count[logging.WARNING]

4 changes: 4 additions & 0 deletions spec_parser/mdparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import logging
import re

from runparams import LogCountingHandler

logger = logging.getLogger(__name__)
log_handler = LogCountingHandler()
logger.addHandler(log_handler)

class SpecFile:
RE_SPLIT_TO_SECTIONS = re.compile(r"\n(?=(?:\Z|# |## ))")
Expand Down
4 changes: 4 additions & 0 deletions spec_parser/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
SpecFile,
)

from runparams import LogCountingHandler

logger = logging.getLogger(__name__)
log_handler = LogCountingHandler()
logger.addHandler(log_handler)

class Model:
def __init__(self, inpath=None):
Expand Down
4 changes: 4 additions & 0 deletions spec_parser/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
from rdflib.namespace import DCTERMS, OWL, RDF, RDFS, SH, SKOS, XSD
from rdflib.tools.rdf2dot import rdf2dot

from runparams import LogCountingHandler

URI_BASE = "https://spdx.org/rdf/3.0.1/terms/"

logger = logging.getLogger(__name__)
log_handler = LogCountingHandler()
logger.addHandler(log_handler)

def gen_rdf(model, outpath, cfg):
p = outpath
Expand Down