From a5bb782d3ee6f9da6f363cfe776b82f98cadfeed Mon Sep 17 00:00:00 2001 From: checktheroads Date: Wed, 4 Sep 2019 01:28:55 -0700 Subject: [PATCH] refactor --- hyperglass/configuration/models.py | 8 ++-- hyperglass/constants.py | 36 --------------- hyperglass/exceptions.py | 53 +++++++--------------- hyperglass/render/templates/credit.html.j2 | 5 -- 4 files changed, 20 insertions(+), 82 deletions(-) delete mode 100644 hyperglass/render/templates/credit.html.j2 diff --git a/hyperglass/configuration/models.py b/hyperglass/configuration/models.py index 533d665..d752a13 100644 --- a/hyperglass/configuration/models.py +++ b/hyperglass/configuration/models.py @@ -251,7 +251,7 @@ class Branding(BaseSettings): primary: Color = "#40798c" secondary: Color = "#330036" - danger: Color = "#ff5e5b" + danger: Color = "#a21024" warning: Color = "#eec643" light: Color = "#fbfffe" dark: Color = "#383541" @@ -296,9 +296,9 @@ class Branding(BaseSettings): title_mode: str = "logo_only" title: str = "hyperglass" subtitle: str = "AS{primary_asn}" - location: str = "Location" + query_location: str = "Location" query_type: str = "Query" - query_placeholder: str = "Target" + query_target: str = "Target" terms: str = "Terms" info: str = "Help" peeringdb = "PeeringDB" @@ -354,7 +354,7 @@ class Messages(BaseSettings): no_query_type: str = "A query type must be specified." no_location: str = "A location must be selected." - no_input: str = "A target must be specified" + no_input: str = "{query_type} must be specified." blacklist: str = "{target} a member of {blacklisted_net}, which is not allowed." max_prefix: str = ( "Prefix length must be shorter than /{max_length}. {target} is too specific." diff --git a/hyperglass/constants.py b/hyperglass/constants.py index 652ae07..0dc6ac3 100644 --- a/hyperglass/constants.py +++ b/hyperglass/constants.py @@ -5,42 +5,6 @@ Global Constants for hyperglass protocol_map = {80: "http", 8080: "http", 443: "https", 8443: "https"} -class Status: - """ - Defines codes, messages, and method names for status codes used by - hyperglass. - """ - - codes_dict = { - 200: ("valid", "Valid Query"), - 405: ("not_allowed", "Query Not Allowed"), - 415: ("invalid", "Invalid Query"), - 504: ("target_error", "Unable to Reach Target"), - } - - def __init__(self): - """ - Dynamically generates class attributes for codes in codes_dict. - """ - for (_code, text) in Status.codes_dict.items(): - setattr(self, text[0], _code) - - @staticmethod - def get_reason(search_code): - """ - Maps and returns input code integer to associated reason text. - Mainly used for populating Prometheus fields. - """ - reason = None - for (_code, text) in Status.codes_dict.items(): - if _code == search_code: - reason = text[1] - return reason - - -code = Status() - - class Supported: """ Defines items supported by hyperglass. diff --git a/hyperglass/exceptions.py b/hyperglass/exceptions.py index 6c6fb65..e6d9897 100644 --- a/hyperglass/exceptions.py +++ b/hyperglass/exceptions.py @@ -2,26 +2,20 @@ Custom exceptions for hyperglass """ -from hyperglass.constants import code - class HyperglassError(Exception): """hyperglass base exception""" - def __init__(self, message="", status=500, keywords={}): + def __init__(self, message="", alert="warning", keywords={}): self.message = message - self.status = status + self.alert = alert self.keywords = keywords def __str__(self): return self.message def __dict__(self): - return { - "message": self.message, - "status": self.status, - "keywords": self.keywords, - } + return {"message": self.message, "alert": self.alert, "keywords": self.keywords} class ConfigError(HyperglassError): @@ -63,11 +57,9 @@ class ScrapeError(HyperglassError): def __init__(self, msg, **kwargs): self.message = msg.format(**kwargs) - self.status = code.target_error + self.alert = "danger" self.keywords = [value for value in kwargs.values()] - super().__init__( - message=self.message, status=self.status, keywords=self.keywords - ) + super().__init__(message=self.message, alert=self.alert, keywords=self.keywords) class AuthError(HyperglassError): @@ -75,11 +67,9 @@ class AuthError(HyperglassError): def __init__(self, msg, **kwargs): self.message = msg.format(**kwargs) - self.status = code.target_error + self.alert = "danger" self.keywords = [value for value in kwargs.values()] - super().__init__( - message=self.message, status=self.status, keywords=self.keywords - ) + super().__init__(message=self.message, alert=self.alert, keywords=self.keywords) class RestError(HyperglassError): @@ -87,11 +77,9 @@ class RestError(HyperglassError): def __init__(self, msg, **kwargs): self.message = msg.format(**kwargs) - self.status = code.target_error + self.alert = "danger" self.keywords = [value for value in kwargs.values()] - super().__init__( - message=self.message, status=self.status, keywords=self.keywords - ) + super().__init__(message=self.message, alert=self.alert, keywords=self.keywords) class InputInvalid(HyperglassError): @@ -99,11 +87,9 @@ class InputInvalid(HyperglassError): def __init__(self, unformatted_msg, **kwargs): self.message = unformatted_msg.format(**kwargs) - self.status = code.invalid + self.alert = "warning" self.keywords = [value for value in kwargs.values()] - super().__init__( - message=self.message, status=self.status, keywords=self.keywords - ) + super().__init__(message=self.message, alert=self.alert, keywords=self.keywords) class InputNotAllowed(HyperglassError): @@ -114,11 +100,9 @@ class InputNotAllowed(HyperglassError): def __init__(self, unformatted_msg, **kwargs): self.message = unformatted_msg.format(**kwargs) - self.status = code.invalid + self.alert = "warning" self.keywords = [value for value in kwargs.values()] - super().__init__( - message=self.message, status=self.status, keywords=self.keywords - ) + super().__init__(message=self.message, alert=self.alert, keywords=self.keywords) class UnsupportedDevice(HyperglassError): @@ -126,11 +110,8 @@ class UnsupportedDevice(HyperglassError): def __init__(self, **kwargs): self.message = "".format(**kwargs) - self.status = code.target_error self.keywords = [value for value in kwargs.values()] - super().__init__( - message=self.message, status=self.status, keywords=self.keywords - ) + super().__init__(message=self.message, keywords=self.keywords) class DeviceTimeout(HyperglassError): @@ -138,8 +119,6 @@ class DeviceTimeout(HyperglassError): def __init__(self, msg, **kwargs): self.message = msg.format(**kwargs) - self.status = code.target_error + self.alert = "danger" self.keywords = [value for value in kwargs.values()] - super().__init__( - message=self.message, status=self.status, keywords=self.keywords - ) + super().__init__(message=self.message, alert=self.alert, keywords=self.keywords) diff --git a/hyperglass/render/templates/credit.html.j2 b/hyperglass/render/templates/credit.html.j2 deleted file mode 100644 index 81601f9..0000000 --- a/hyperglass/render/templates/credit.html.j2 +++ /dev/null @@ -1,5 +0,0 @@ -

Powered by hyperglass. - Source code - licensed BSD - 3-Clause - Clear.

\ No newline at end of file