Skip to content

Commit

Permalink
Implement limit on output errors
Browse files Browse the repository at this point in the history
Ref. #748
  • Loading branch information
Alexander Senier committed Aug 24, 2021
1 parent b35483c commit f7a56e6
Show file tree
Hide file tree
Showing 4 changed files with 1,560 additions and 3 deletions.
15 changes: 12 additions & 3 deletions rflx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pkg_resources import get_distribution

from rflx import __version__
from rflx.error import RecordFluxError, Severity, Subsystem, fail
from rflx.error import RecordFluxError, Severity, Subsystem, fail, set_fail_after
from rflx.generator import Generator
from rflx.graph import Graph
from rflx.model import Message, Model, Session
Expand All @@ -28,6 +28,13 @@ def main(argv: List[str]) -> Union[int, str]:
"-q", "--quiet", action="store_true", help="disable logging to standard output"
)
parser.add_argument("--version", action="store_true")
parser.add_argument(
"--max-errors",
type=int,
default=0,
metavar=("NUM"),
help="Maximum number of errors to report",
)

subparsers = parser.add_subparsers(dest="subcommand")

Expand All @@ -42,8 +49,8 @@ def main(argv: List[str]) -> Union[int, str]:
"-p",
"--prefix",
type=str,
default="RFLX",
help=("add prefix to generated packages " f"(default: {DEFAULT_PREFIX})"),
default=DEFAULT_PREFIX,
help="add prefix to generated packages (default: %(default)s)",
)
parser_generate.add_argument(
"-n", "--no-library", help="omit generating library files", action="store_true"
Expand Down Expand Up @@ -99,6 +106,8 @@ def main(argv: List[str]) -> Union[int, str]:
if args.quiet:
logging.disable(logging.CRITICAL)

set_fail_after(args.max_errors)

try:
args.func(args)
except RecordFluxError as e:
Expand Down
26 changes: 26 additions & 0 deletions rflx/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@

from rflx.common import Base, verbose_repr

FAIL_AFTER_VALUE: int = 0


def set_fail_after(count: int) -> None:
# pylint: disable = global-statement
global FAIL_AFTER_VALUE
FAIL_AFTER_VALUE = count


def get_fail_after() -> int:
# pylint: disable = global-statement
global FAIL_AFTER_VALUE
return FAIL_AFTER_VALUE


class Location(Base):
def __init__(
Expand Down Expand Up @@ -138,11 +152,15 @@ def append(
self, message: str, subsystem: Subsystem, severity: Severity, location: Location = None
) -> None:
self.__errors.append(BaseError.Entry(message, subsystem, severity, location))
if get_fail_after() > 0 and len(self.__errors) >= get_fail_after():
raise self

def appendleft(
self, message: str, subsystem: Subsystem, severity: Severity, location: Location = None
) -> None:
self.__errors.appendleft(BaseError.Entry(message, subsystem, severity, location))
if get_fail_after() > 0 and len(self.__errors) >= get_fail_after():
raise self

def extend(
self,
Expand All @@ -153,6 +171,14 @@ def extend(
else:
for message, subsystem, severity, location in entries:
self.__errors.append(BaseError.Entry(message, subsystem, severity, location))
if (
get_fail_after() > 0
and len(
list(e for e in self.__errors if e.severity in (Severity.WARNING, Severity.ERROR))
)
>= get_fail_after()
):
raise self

def check(self) -> bool:
return len(self.__errors) > 0
Expand Down
Loading

0 comments on commit f7a56e6

Please sign in to comment.