Browse Source

Resolves Bandit 101 errors from misusing asserts

From Bandit's docs:
It was discovered that some projects used assert to enforce interface
constraints. However, assert is removed with compiling to optimised byte
coden (python -o producing *.pyo files). This caused various protections
to be removed. The use of assert is also considered as general bad
practice in OpenStack codebases.

Change-Id: Ie2b5e9cc3e1afcf9e9bd0f8675947754fa1e4b7d
Ian Pittwood 1 week ago
parent
commit
8a39a6fd17

+ 16
- 20
spyglass/data_extractor/plugins/formation.py View File

@@ -36,13 +36,11 @@ LOG = logging.getLogger(__name__)
36 36
 class FormationPlugin(BaseDataSourcePlugin):
37 37
     def __init__(self, region):
38 38
         # Save site name is valid
39
-        try:
40
-            assert region is not None
41
-            super().__init__(region)
42
-        except AssertionError:
39
+        if not region:
43 40
             LOG.error("Site: None! Spyglass exited!")
44 41
             LOG.info("Check spyglass --help for details")
45 42
             exit()
43
+        super().__init__(region)
46 44
 
47 45
         self.source_type = "rest"
48 46
         self.source_name = "formation"
@@ -80,23 +78,21 @@ class FormationPlugin(BaseDataSourcePlugin):
80 78
 
81 79
     def get_plugin_conf(self, kwargs):
82 80
         """ Validates the plugin param and return if success"""
83
-        try:
84
-            assert (
85
-                kwargs["formation_url"]
86
-            ) is not None, "formation_url is Not Specified"
87
-            url = kwargs["formation_url"]
88
-            assert (
89
-                kwargs["formation_user"]
90
-            ) is not None, "formation_user is Not Specified"
91
-            user = kwargs["formation_user"]
92
-            assert (
93
-                kwargs["formation_password"]
94
-            ) is not None, "formation_password is Not Specified"
95
-            password = kwargs["formation_password"]
96
-        except AssertionError:
97
-            LOG.error("Insufficient plugin parameter! Spyglass exited!")
98
-            raise
81
+
82
+        if not kwargs["formation_url"]:
83
+            LOG.error("formation_url not specified! Spyglass exited!")
84
+            exit()
85
+        url = kwargs["formation_url"]
86
+
87
+        if not kwargs["formation_user"]:
88
+            LOG.error("formation_user not specified! Spyglass exited!")
89
+            exit()
90
+        user = kwargs["formation_user"]
91
+
92
+        if not kwargs["formation_password"]:
93
+            LOG.error("formation_password not specified! Spyglass exited!")
99 94
             exit()
95
+        password = kwargs['formation_password']
100 96
 
101 97
         plugin_conf = {"url": url, "user": user, "password": password}
102 98
         return plugin_conf

+ 7
- 9
spyglass/data_extractor/plugins/tugboat/tugboat.py View File

@@ -68,16 +68,14 @@ class TugboatPlugin(BaseDataSourcePlugin):
68 68
         and excel specs are not specified. The below code has been
69 69
         written as an additional safeguard.
70 70
         """
71
-        try:
72
-            assert len(kwargs["excel"]), "Engineering Spec file not specified"
73
-            excel_file_info = kwargs["excel"]
74
-            assert (
75
-                kwargs["excel_spec"]
76
-            ) is not None, "Excel Spec file not specified"
77
-            excel_spec_info = kwargs["excel_spec"]
78
-        except AssertionError as e:
79
-            LOG.error("{}:Spyglass exited!".format(e))
71
+        if not kwargs["excel"]:
72
+            LOG.error("Engineering excel file not specified: Spyglass exited!")
73
+            exit()
74
+        excel_file_info = kwargs["excel"]
75
+        if not kwargs["excel_spec"]:
76
+            LOG.error("Engineering spec file not specified: Spyglass exited!")
80 77
             exit()
78
+        excel_spec_info = kwargs["excel_spec"]
81 79
         plugin_conf = {
82 80
             "excel_path": excel_file_info,
83 81
             "excel_spec": excel_spec_info,

Loading…
Cancel
Save