Skip to content

Commit f6058b1

Browse files
Update default.py
Delete try.py, rules_prevalence.pickle
1 parent 8a0e61b commit f6058b1

File tree

3 files changed

+12
-61
lines changed

3 files changed

+12
-61
lines changed

assets/rules_prevalence.pickle

-18.9 KB
Binary file not shown.

capa/render/default.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77
# See the License for the specific language governing permissions and limitations under the License.
88

9-
import pickle
9+
import json
1010
import collections
11+
from pathlib import Path
1112

1213
import tabulate
1314

@@ -84,12 +85,16 @@ def render_capabilities(doc: rd.ResultDocument, ostream: StringIO):
8485
+-------------------------------------------------------+-------------------------------------------------+
8586
"""
8687

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}")
9095

9196
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"))
9398

9499
# seperate rules based on their prevalence
95100
common = []
@@ -101,13 +106,13 @@ def load_rules_prevalence(filename: str) -> dict:
101106
count = len(rule.matches)
102107
matches = f"({count} matches)" if count > 1 else ""
103108

104-
rule_prevalence = float(rules_prevalence.get(rule.meta.name + "\n", 0))
109+
rule_prevalence = float(rules_prevalence.get(rule.meta.name, 0))
105110
if rule_prevalence < 0:
106111
raise ValueError("Match probability cannot be negative")
107112

108113
prevalences = [rutils.bold("rare"), rutils.bold("common"), "unknown"]
109114

110-
if rule_prevalence >= 0.05 or rule_prevalence == 0:
115+
if rule_prevalence == 0 or rule_prevalence >= 0.05:
111116
prevalence = prevalences[2] if rule_prevalence == 0 else prevalences[1]
112117
common.append((rule.meta.namespace, rule.meta.name, matches, prevalence))
113118
else:

try.py

-54
This file was deleted.

0 commit comments

Comments
 (0)