6
6
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7
7
# See the License for the specific language governing permissions and limitations under the License.
8
8
9
- import pickle
9
+ import json
10
10
import collections
11
+ from pathlib import Path
11
12
12
13
import tabulate
13
14
@@ -84,12 +85,16 @@ def render_capabilities(doc: rd.ResultDocument, ostream: StringIO):
84
85
+-------------------------------------------------------+-------------------------------------------------+
85
86
"""
86
87
87
- def load_rules_prevalence (filename : str ) -> dict :
88
- with open (filename , "rb" ) as pickle_file :
89
- return pickle .load (pickle_file )
88
+ def load_rules_prevalence (file : Path ) -> dict :
89
+ try :
90
+ return json .load (file .open ("r" ))
91
+ except FileNotFoundError :
92
+ raise FileNotFoundError (f"File '{ file } ' not found." )
93
+ except Exception as e :
94
+ raise RuntimeError (f"An error occurred while loading '{ file } ': { e } " )
90
95
91
96
subrule_matches = find_subrule_matches (doc )
92
- rules_prevalence = load_rules_prevalence ("./assets/rules_prevalence.pickle" )
97
+ rules_prevalence = load_rules_prevalence (Path ( "./assets/rules_prevalence.json" ) )
93
98
94
99
# seperate rules based on their prevalence
95
100
common = []
@@ -101,13 +106,13 @@ def load_rules_prevalence(filename: str) -> dict:
101
106
count = len (rule .matches )
102
107
matches = f"({ count } matches)" if count > 1 else ""
103
108
104
- rule_prevalence = float (rules_prevalence .get (rule .meta .name + " \n " , 0 ))
109
+ rule_prevalence = float (rules_prevalence .get (rule .meta .name , 0 ))
105
110
if rule_prevalence < 0 :
106
111
raise ValueError ("Match probability cannot be negative" )
107
112
108
113
prevalences = [rutils .bold ("rare" ), rutils .bold ("common" ), "unknown" ]
109
114
110
- if rule_prevalence >= 0.05 or rule_prevalence == 0 :
115
+ if rule_prevalence == 0 or rule_prevalence >= 0.05 :
111
116
prevalence = prevalences [2 ] if rule_prevalence == 0 else prevalences [1 ]
112
117
common .append ((rule .meta .namespace , rule .meta .name , matches , prevalence ))
113
118
else :
0 commit comments