Skip to content

Commit

Permalink
Set exception var_type
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Nov 15, 2022
1 parent dd3bc54 commit a04bfb0
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions sys_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ def __init__(self, *args: object, **kwargs: Dict[str, str]) -> None:

# Provide some extra information about the exception
self.var_key: str = kwargs.pop("key", "")
self.var_type: str = ""
self.var_path: str = (
fspath(globals()["__SYS_VARS_PATH"])
if "__SYS_VARS_PATH" in globals()
else ""
)
self.var_type: str = kwargs.pop("type", "")
self.var_path: str = fspath(globals()["__SYS_VARS_PATH"])


# Get the defined sys vars path from the environment
Expand Down Expand Up @@ -72,7 +68,7 @@ def __from_env_file(key: str, /) -> Optional[str]:
return __DOT_ENV_CONTENT.get(key)


def get(key: str, /, *, default: Optional[Any] = None) -> str:
def get(key: str, /, *, default: Optional[Any] = None, **kwargs: Dict[str, str]) -> str:
"""Get a system variable value as a str type.
Check the value of SYS_VARS_PATH and os.environ for the key,
Expand Down Expand Up @@ -102,7 +98,9 @@ def get(key: str, /, *, default: Optional[Any] = None) -> str:

# No default value was given, so raise an exception
raise SysVarNotFoundError(
f'Could not get value for system variable "{key}"', key=key
f'Could not get value for system variable "{key}"',
key=key,
type=kwargs.pop("var_type", "str"),
)


Expand All @@ -111,7 +109,7 @@ def get_bool(key: str, /, **kwargs: Dict[str, Any]) -> bool:
See signature of get() for parameter details."""
# Start by getting the system value
sys_val = get(key, **kwargs)
sys_val = get(key, var_type="bool", **kwargs)

# We have an actual boolean data type
# (most likely a specified default value).
Expand Down Expand Up @@ -141,7 +139,7 @@ def get_datetime(key: str, /, **kwargs: Dict[str, Any]) -> datetime:
Raises ValueError if the data cannot be cast.
See signature of get() for parameter details."""
sys_val = get(key, **kwargs)
sys_val = get(key, var_type="datetime", **kwargs)

# We have an actual datetime obj (most likely a default val)
# There's nothing more to do
Expand All @@ -156,7 +154,7 @@ def get_float(key: str, /, **kwargs: Dict[str, Any]) -> float:
Raises ValueError if the data cannot be cast.
See signature of get() for parameter details."""
return float(get(key, **kwargs))
return float(get(key, var_type="float", **kwargs))


def get_int(key: str, /, **kwargs: Dict[str, Any]) -> int:
Expand All @@ -165,7 +163,7 @@ def get_int(key: str, /, **kwargs: Dict[str, Any]) -> int:
Raises ValueError if the data cannot be cast.
See signature of get() for parameter details."""
return int(get(key, **kwargs))
return int(get(key, var_type="int", **kwargs))


def get_json(key: str, /, **kwargs: Dict[str, Any]) -> Union[Dict[str, Any], List[Any]]:
Expand All @@ -181,7 +179,7 @@ def get_json(key: str, /, **kwargs: Dict[str, Any]) -> Union[Dict[str, Any], Lis
Raises json.JSONDecodeError if the JSON data cannot be decoded.
See signature of get() for parameter details."""
sys_val = get(key, **kwargs)
sys_val = get(key, var_type="json", **kwargs)

# We have a dictionary or list (most likely a default val)
# There's nothing more to do
Expand All @@ -195,4 +193,4 @@ def get_path(key: str, /, **kwargs: Dict[str, Any]) -> Path:
"""Get a file path string system variable as a pathlib.Path instance.
See signature of get() for parameter details."""
return Path(get(key, **kwargs))
return Path(get(key, var_type="path", **kwargs))

0 comments on commit a04bfb0

Please sign in to comment.