diff options
Diffstat (limited to 'gitlint-core/gitlint/rule_finder.py')
-rw-r--r-- | gitlint-core/gitlint/rule_finder.py | 71 |
1 files changed, 44 insertions, 27 deletions
diff --git a/gitlint-core/gitlint/rule_finder.py b/gitlint-core/gitlint/rule_finder.py index e1c5e77..11665cf 100644 --- a/gitlint-core/gitlint/rule_finder.py +++ b/gitlint-core/gitlint/rule_finder.py @@ -31,7 +31,7 @@ def find_rule_classes(extra_path): # Filter out files that are not python modules for filename in files: - if fnmatch.fnmatch(filename, '*.py'): + if fnmatch.fnmatch(filename, "*.py"): # We have to treat __init__ files a bit special: add the parent dir instead of the filename, and also # add their parent dir to the sys.path (this fixes import issues with pypy2). if filename == "__init__.py": @@ -61,13 +61,19 @@ def find_rule_classes(extra_path): # 1) is it a class, if not, skip # 2) is the parent path the current module. If not, we are dealing with an imported class, skip # 3) is it a subclass of rule - rule_classes.extend([clazz for _, clazz in inspect.getmembers(sys.modules[module]) - if - inspect.isclass(clazz) and # check isclass to ensure clazz.__module__ exists - clazz.__module__ == module and # ignore imported classes - (issubclass(clazz, rules.LineRule) or - issubclass(clazz, rules.CommitRule) or - issubclass(clazz, rules.ConfigurationRule))]) + rule_classes.extend( + [ + clazz + for _, clazz in inspect.getmembers(sys.modules[module]) + if inspect.isclass(clazz) # check isclass to ensure clazz.__module__ exists + and clazz.__module__ == module # ignore imported classes + and ( + issubclass(clazz, rules.LineRule) + or issubclass(clazz, rules.CommitRule) + or issubclass(clazz, rules.ConfigurationRule) + ) + ] + ) # validate that the rule classes are valid user-defined rules for rule_class in rule_classes: @@ -91,55 +97,66 @@ def assert_valid_rule_class(clazz, rule_type="User-defined"): # pylint: disable """ # Rules must extend from LineRule, CommitRule or ConfigurationRule - if not (issubclass(clazz, rules.LineRule) or issubclass(clazz, rules.CommitRule) - or issubclass(clazz, rules.ConfigurationRule)): - msg = f"{rule_type} rule class '{clazz.__name__}' " + \ - f"must extend from {rules.CommitRule.__module__}.{rules.LineRule.__name__}, " + \ - f"{rules.CommitRule.__module__}.{rules.CommitRule.__name__} or " + \ - f"{rules.CommitRule.__module__}.{rules.ConfigurationRule.__name__}" + if not ( + issubclass(clazz, rules.LineRule) + or issubclass(clazz, rules.CommitRule) + or issubclass(clazz, rules.ConfigurationRule) + ): + msg = ( + f"{rule_type} rule class '{clazz.__name__}' " + f"must extend from {rules.CommitRule.__module__}.{rules.LineRule.__name__}, " + f"{rules.CommitRule.__module__}.{rules.CommitRule.__name__} or " + f"{rules.CommitRule.__module__}.{rules.ConfigurationRule.__name__}" + ) raise rules.UserRuleError(msg) # Rules must have an id attribute - if not hasattr(clazz, 'id') or clazz.id is None or not clazz.id: + if not hasattr(clazz, "id") or clazz.id is None or not clazz.id: raise rules.UserRuleError(f"{rule_type} rule class '{clazz.__name__}' must have an 'id' attribute") # Rule id's cannot start with gitlint reserved letters - if clazz.id[0].upper() in ['R', 'T', 'B', 'M', 'I']: + if clazz.id[0].upper() in ["R", "T", "B", "M", "I"]: msg = f"The id '{clazz.id[0]}' of '{clazz.__name__}' is invalid. Gitlint reserves ids starting with R,T,B,M,I" raise rules.UserRuleError(msg) # Rules must have a name attribute - if not hasattr(clazz, 'name') or clazz.name is None or not clazz.name: + if not hasattr(clazz, "name") or clazz.name is None or not clazz.name: raise rules.UserRuleError(f"{rule_type} rule class '{clazz.__name__}' must have a 'name' attribute") # if set, options_spec must be a list of RuleOption if not isinstance(clazz.options_spec, list): - msg = f"The options_spec attribute of {rule_type.lower()} rule class '{clazz.__name__}' " + \ - f"must be a list of {options.RuleOption.__module__}.{options.RuleOption.__name__}" + msg = ( + f"The options_spec attribute of {rule_type.lower()} rule class '{clazz.__name__}' " + f"must be a list of {options.RuleOption.__module__}.{options.RuleOption.__name__}" + ) raise rules.UserRuleError(msg) # check that all items in options_spec are actual gitlint options for option in clazz.options_spec: if not isinstance(option, options.RuleOption): - msg = f"The options_spec attribute of {rule_type.lower()} rule class '{clazz.__name__}' " + \ - f"must be a list of {options.RuleOption.__module__}.{options.RuleOption.__name__}" + msg = ( + f"The options_spec attribute of {rule_type.lower()} rule class '{clazz.__name__}' " + f"must be a list of {options.RuleOption.__module__}.{options.RuleOption.__name__}" + ) raise rules.UserRuleError(msg) # Line/Commit rules must have a `validate` method # We use isroutine() as it's both python 2 and 3 compatible. Details: http://stackoverflow.com/a/17019998/381010 - if (issubclass(clazz, rules.LineRule) or issubclass(clazz, rules.CommitRule)): - if not hasattr(clazz, 'validate') or not inspect.isroutine(clazz.validate): + if issubclass(clazz, rules.LineRule) or issubclass(clazz, rules.CommitRule): + if not hasattr(clazz, "validate") or not inspect.isroutine(clazz.validate): raise rules.UserRuleError(f"{rule_type} rule class '{clazz.__name__}' must have a 'validate' method") # Configuration rules must have an `apply` method elif issubclass(clazz, rules.ConfigurationRule): - if not hasattr(clazz, 'apply') or not inspect.isroutine(clazz.apply): + if not hasattr(clazz, "apply") or not inspect.isroutine(clazz.apply): msg = f"{rule_type} Configuration rule class '{clazz.__name__}' must have an 'apply' method" raise rules.UserRuleError(msg) # LineRules must have a valid target: rules.CommitMessageTitle or rules.CommitMessageBody if issubclass(clazz, rules.LineRule): if clazz.target not in [rules.CommitMessageTitle, rules.CommitMessageBody]: - msg = f"The target attribute of the {rule_type.lower()} LineRule class '{clazz.__name__}' " + \ - f"must be either {rules.CommitMessageTitle.__module__}.{rules.CommitMessageTitle.__name__} " + \ - f"or {rules.CommitMessageTitle.__module__}.{rules.CommitMessageBody.__name__}" + msg = ( + f"The target attribute of the {rule_type.lower()} LineRule class '{clazz.__name__}' " + f"must be either {rules.CommitMessageTitle.__module__}.{rules.CommitMessageTitle.__name__} " + f"or {rules.CommitMessageTitle.__module__}.{rules.CommitMessageBody.__name__}" + ) raise rules.UserRuleError(msg) |