Return an iterable of rules loaded from rule files in ``rules_data_dir``. Optionally check for consistency if ``with_checks`` is True.
(
rules_data_dir=rules_data_dir,
with_checks=True,
is_builtin=True,
with_deprecated=False,
)
| 1216 | |
| 1217 | |
| 1218 | def load_rules( |
| 1219 | rules_data_dir=rules_data_dir, |
| 1220 | with_checks=True, |
| 1221 | is_builtin=True, |
| 1222 | with_deprecated=False, |
| 1223 | ): |
| 1224 | """ |
| 1225 | Return an iterable of rules loaded from rule files in ``rules_data_dir``. |
| 1226 | Optionally check for consistency if ``with_checks`` is True. |
| 1227 | """ |
| 1228 | # TODO: OPTIMIZE: create a graph of rules to account for containment and |
| 1229 | # similarity clusters? |
| 1230 | seen_files = set() |
| 1231 | processed_files = set() |
| 1232 | lower_case_files = set() |
| 1233 | case_problems = set() |
| 1234 | space_problems = [] |
| 1235 | model_errors = [] |
| 1236 | |
| 1237 | for rule_file in resource_iter(location=rules_data_dir, with_dirs=False): |
| 1238 | if rule_file.endswith('.RULE'): |
| 1239 | base_name = file_base_name(rule_file) |
| 1240 | |
| 1241 | if with_checks and ' ' in base_name: |
| 1242 | space_problems.append(rule_file) |
| 1243 | |
| 1244 | try: |
| 1245 | rule = Rule.from_file(rule_file=rule_file) |
| 1246 | if not with_deprecated and rule.is_deprecated: |
| 1247 | continue |
| 1248 | else: |
| 1249 | yield rule |
| 1250 | |
| 1251 | except Exception as re: |
| 1252 | if with_checks: |
| 1253 | model_errors.append(str(re)) |
| 1254 | |
| 1255 | if with_checks: |
| 1256 | # accumulate sets to ensures we do not have illegal names or extra |
| 1257 | # orphaned files |
| 1258 | rule_file_lower = rule_file.lower() |
| 1259 | if rule_file_lower in lower_case_files: |
| 1260 | case_problems.add(rule_file_lower) |
| 1261 | else: |
| 1262 | lower_case_files.add(rule_file_lower) |
| 1263 | |
| 1264 | processed_files.add(rule_file) |
| 1265 | |
| 1266 | if with_checks and not rule_file.endswith('~'): |
| 1267 | seen_files.add(rule_file) |
| 1268 | |
| 1269 | if with_checks: |
| 1270 | unknown_files = seen_files - processed_files |
| 1271 | if unknown_files or case_problems or model_errors or space_problems: |
| 1272 | msg = '' |
| 1273 | |
| 1274 | if model_errors: |
| 1275 | errors = '\n'.join(model_errors) |