From 465bbe9c15a0ea48827b4493220198acdea72c70 Mon Sep 17 00:00:00 2001 From: Ruben Dahl Date: Sat, 8 Apr 2023 23:56:30 +0200 Subject: [PATCH] Changed the constant file with os.environ Also doing some more testing and mucking around with the parser --- constants.py | 1 - datatypes.py | 2 +- decorators.py | 8 ++++---- pyne.py | 15 ++++++++++----- 4 files changed, 15 insertions(+), 11 deletions(-) delete mode 100644 constants.py diff --git a/constants.py b/constants.py deleted file mode 100644 index 51c64dd..0000000 --- a/constants.py +++ /dev/null @@ -1 +0,0 @@ -DEBUG = True diff --git a/datatypes.py b/datatypes.py index 9898d78..4e42086 100644 --- a/datatypes.py +++ b/datatypes.py @@ -25,5 +25,5 @@ VAR_MATCH = { r"\".+\"": Datatype.STRING, r"\d+\.\d{0,}": Datatype.FLOAT, r"\d+": Datatype.INTEGER, - r".+": Datatype.NONE, + # r".+": Datatype.NONE, } diff --git a/decorators.py b/decorators.py index b93be83..08286ab 100644 --- a/decorators.py +++ b/decorators.py @@ -1,10 +1,10 @@ -from constants import DEBUG +import os def todo(msg: str = "Not implemented"): def inner(func): def inner2(*args, **kwargs): - if DEBUG: + if "PYNE_DEBUG" in os.environ: print(f"[{func.__name__}] TODO: {msg}") return func(*args, **kwargs) @@ -15,7 +15,7 @@ def todo(msg: str = "Not implemented"): def wip(func): def inner(*args, **kwargs): - if DEBUG: + if "PYNE_DEBUG" in os.environ: print( f"Called work in progress function {func.__name__}. Functionality may change in the future." ) @@ -27,7 +27,7 @@ def wip(func): def deprecated(func): @functools.wraps(func) def inner(*args, **kwargs): - if DEBUG: + if "PYNE_DEBUG" in os.environ: print( f"\x1b[31mWarning:\x1b[0m The function {inner.__name__} is deprecated." ) diff --git a/pyne.py b/pyne.py index 9c5dec5..2b27b1b 100644 --- a/pyne.py +++ b/pyne.py @@ -24,7 +24,7 @@ class Pyne: def parse(self) -> list[PyneObject]: objects: list[PyneObject] = [] with open(self.path) as f: - for line in f.readlines(): + for row, line in enumerate(f.readlines(), 1): for var in datatypes.VAR_MATCH: regex = re.compile(var) res = regex.match(line) @@ -35,6 +35,9 @@ class Pyne: ) ) break + continue + else: + raise ParserError(f"[{self.path}:{row}] Token {line} not found.") if len(objects) == 0: objects.append(datatypes.NONE_TYPE) return objects @@ -55,10 +58,12 @@ def main() -> int: args = parser.parse_args() print(args) pyne = Pyne(args.file[0], None) - - for obj in pyne.parse(): - print(obj) - return 0 + try: + for obj in pyne.parse(): + print(obj) + return 0 + except ParserError as e: + print(f"Found invalid token: {e}") if __name__ == "__main__":