Skip to content

Commit

Permalink
move Errors class from file.py to errors.py
Browse files Browse the repository at this point in the history
  • Loading branch information
NiumXp committed Jan 23, 2024
1 parent c2cb3ed commit e99839b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
36 changes: 36 additions & 0 deletions norminette/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from functools import cmp_to_key
from typing import Union, Literal

from norminette.norm_error import NormError, NormWarning


def sort_errs(a, b):
if a.col == b.col and a.line == b.line:
return 1 if a.errno > b.errno else -1
return a.col - b.col if a.line == b.line else a.line - b.line


class Errors:
__slots__ = "_inner"

def __init__(self) -> None:
self._inner = []

def __len__(self) -> int:
return len(self._inner)

def __iter__(self):
self._inner.sort(key=cmp_to_key(sort_errs))
return iter(self._inner)

@property
def status(self) -> Literal["OK", "Error"]:
if not self:
return "OK"
if all(isinstance(it, NormWarning) for it in self):
return "OK"
return "Error"

def append(self, value: Union[NormError, NormWarning]) -> None:
assert isinstance(value, (NormError, NormWarning))
self._inner.append(value)
37 changes: 2 additions & 35 deletions norminette/file.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
import os
from functools import cmp_to_key
from typing import Optional, Union, Literal
from typing import Optional

from norminette.norm_error import NormError, NormWarning


def sort_errs(a, b):
if a.col == b.col and a.line == b.line:
return 1 if a.errno > b.errno else -1
return a.col - b.col if a.line == b.line else a.line - b.line


class Errors:
__slots__ = "_inner"

def __init__(self) -> None:
self._inner = []

def __len__(self) -> int:
return len(self._inner)

def __iter__(self):
self._inner.sort(key=cmp_to_key(sort_errs))
return iter(self._inner)

@property
def status(self) -> Literal["OK", "Error"]:
if not self:
return "OK"
if all(isinstance(it, NormWarning) for it in self):
return "OK"
return "Error"

def append(self, value: Union[NormError, NormWarning]) -> None:
assert isinstance(value, (NormError, NormWarning))
self._inner.append(value)
from norminette.errors import Errors


class File:
Expand Down

0 comments on commit e99839b

Please sign in to comment.