Skip to content

Commit

Permalink
refactor exceptions (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-andreas committed May 4, 2020
1 parent 24c5977 commit 937770d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 3 additions & 3 deletions latticejson/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict, List
from warnings import warn

from .exceptions import UnknownAttributeWarning, UnknownElementWarning
from .exceptions import UnknownAttributeWarning, UnknownElementTypeWarning
from .parse import parse_elegant, parse_madx

NAME_MAP = json.loads((Path(__file__).parent / "map.json").read_text())["map"]
Expand Down Expand Up @@ -45,7 +45,7 @@ def _map_names(lattice_data: dict, name_map: dict):
latticejson_type = name_map.get(other_type)
if latticejson_type is None:
elements[name] = ["Drift", {"length": other_attributes.get("L", 0)}]
warn(UnknownElementWarning(name, other_type), stacklevel=2)
warn(UnknownElementTypeWarning(name, other_type))
continue

attributes = {}
Expand All @@ -55,7 +55,7 @@ def _map_names(lattice_data: dict, name_map: dict):
if latticejson_key is not None:
attributes[latticejson_key] = value
else:
warn(UnknownAttributeWarning(other_key, name), stacklevel=2)
warn(UnknownAttributeWarning(other_key, name))

lattices = lattice_data["lattices"]
lattice_name, main_lattice = lattices.popitem() # use last lattice as main_lattice
Expand Down
18 changes: 14 additions & 4 deletions latticejson/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import warnings


def warning_on_one_line(message, category, filename, lineno, file=None, line=None):
return f"{filename}:{lineno}: {category.__name__}: {message}\n"


warnings.formatwarning = warning_on_one_line


class UndefinedObjectError(Exception):
"""Raised if an object is referenced within a lattice but no definition is found.
Expand All @@ -7,24 +17,24 @@ class UndefinedObjectError(Exception):

def __init__(self, object_name, lattice_name):
super().__init__(
f"The Object {object_name} is referenced in {lattice_name} "
f"The object '{object_name}' is referenced in '{lattice_name}' "
"but no definition was found!"
)


class UnknownElementWarning(UserWarning):
class UnknownElementTypeWarning(UserWarning):
"""Raised if there is no equivalent LatticeJSON element."""

def __init__(self, name, type_, *args, **kwargs):
message = f"Replacing element {name} ({type_}) with Drift."
message = f"Replacing element '{name}' ({type_}) with Drift."
super().__init__(message, *args, **kwargs)


class UnknownAttributeWarning(UserWarning):
"""Raised if there is no equivalent LatticeJSON attribute."""

def __init__(self, attribute, element, *args, **kwargs):
message = f"Ignoring attribute {attribute} of {element}."
message = f"Ignoring attribute '{attribute}' of '{element}'."
super().__init__(message, *args, **kwargs)


Expand Down

0 comments on commit 937770d

Please sign in to comment.