From 8e2e4979b08e638e9860c7c72869f120bd47feee Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 6 Feb 2024 13:42:21 -0500 Subject: [PATCH] Misc cleanup --- netbox/extras/scripts.py | 28 ++++++++++++++++++++-------- netbox/extras/utils.py | 7 ++----- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index 2696a75b8..afb68d37d 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -394,9 +394,18 @@ class BaseScript: return ordered_vars def run(self, data, commit): - raise NotImplementedError("The script must define a run() method.") + """ + Override this method with custom script logic. + """ + # Backward compatibility for legacy Reports + self.pre_run() + self.run_tests() + self.post_run() + + # # Form rendering + # def get_fieldsets(self): fieldsets = [] @@ -435,7 +444,9 @@ class BaseScript: return form + # # Logging + # def _log(self, message, obj=None, level=LogLevelChoices.LOG_DEFAULT): """ @@ -484,7 +495,9 @@ class BaseScript: self._log(message, obj, level=LogLevelChoices.LOG_FAILURE) self._failed = True + # # Convenience functions + # def load_yaml(self, filename): """ @@ -511,6 +524,10 @@ class BaseScript: return data + # + # Legacy Report functionality + # + def run_tests(self): """ Run the report and save its results. Each test method will be executed in order. @@ -529,20 +546,15 @@ class BaseScript: self._current_method = '' - def run(self, data, commit): - self.pre_run() - self.run_tests() - self.post_run() - def pre_run(self): """ - Extend this method to include any tasks which should execute *before* the report is run. + Legacy method for operations performed immediately prior to running a Report. """ pass def post_run(self): """ - Extend this method to include any tasks which should execute *after* the report is run. + Legacy method for operations performed immediately after running a Report. """ pass diff --git a/netbox/extras/utils.py b/netbox/extras/utils.py index 2a99405d0..a3a6670cf 100644 --- a/netbox/extras/utils.py +++ b/netbox/extras/utils.py @@ -49,15 +49,12 @@ def register_features(model, features): def is_script(obj): """ - Returns True if the object is a Script. - """ - """ - Returns True if the given object is a Report. + Returns True if the object is a Script or Report. """ from .reports import Report from .scripts import Script try: - return ((issubclass(obj, Report) and obj != Report) or (issubclass(obj, Script) and obj != Script)) + return (issubclass(obj, Report) and obj != Report) or (issubclass(obj, Script) and obj != Script) except TypeError: return False