diff options
Diffstat (limited to '')
-rw-r--r-- | gitlint/options.py | 58 |
1 files changed, 24 insertions, 34 deletions
diff --git a/gitlint/options.py b/gitlint/options.py index 3ea8310..9fdac8f 100644 --- a/gitlint/options.py +++ b/gitlint/options.py @@ -2,7 +2,7 @@ from abc import abstractmethod import os import re -from gitlint.utils import ustr, sstr +from gitlint.exception import GitlintError def allow_none(func): @@ -17,11 +17,11 @@ def allow_none(func): return wrapped -class RuleOptionError(Exception): +class RuleOptionError(GitlintError): pass -class RuleOption(object): +class RuleOption: """ Base class representing a configurable part (i.e. option) of a rule (e.g. the max-length of the title-max-line rule). This class should not be used directly. Instead, use on the derived classes like StrOption, IntOption to set @@ -29,8 +29,8 @@ class RuleOption(object): """ def __init__(self, name, value, description): - self.name = ustr(name) - self.description = ustr(description) + self.name = name + self.description = description self.value = None self.set(value) @@ -40,37 +40,28 @@ class RuleOption(object): pass # pragma: no cover def __str__(self): - return sstr(self) # pragma: no cover - - def __unicode__(self): - return u"({0}: {1} ({2}))".format(self.name, self.value, self.description) # pragma: no cover - - def __repr__(self): - return self.__str__() # pragma: no cover + return f"({self.name}: {self.value} ({self.description}))" def __eq__(self, other): return self.name == other.name and self.description == other.description and self.value == other.value - def __ne__(self, other): - return not self.__eq__(other) # required for py2 - class StrOption(RuleOption): @allow_none def set(self, value): - self.value = ustr(value) + self.value = str(value) class IntOption(RuleOption): def __init__(self, name, value, description, allow_negative=False): self.allow_negative = allow_negative - super(IntOption, self).__init__(name, value, description) + super().__init__(name, value, description) def _raise_exception(self, value): if self.allow_negative: - error_msg = u"Option '{0}' must be an integer (current value: '{1}')".format(self.name, value) + error_msg = f"Option '{self.name}' must be an integer (current value: '{value}')" else: - error_msg = u"Option '{0}' must be a positive integer (current value: '{1}')".format(self.name, value) + error_msg = f"Option '{self.name}' must be a positive integer (current value: '{value}')" raise RuleOptionError(error_msg) @allow_none @@ -88,9 +79,9 @@ class BoolOption(RuleOption): # explicit choice to not annotate with @allow_none: Booleans must be False or True, they cannot be unset. def set(self, value): - value = ustr(value).strip().lower() + value = str(value).strip().lower() if value not in ['true', 'false']: - raise RuleOptionError(u"Option '{0}' must be either 'true' or 'false'".format(self.name)) + raise RuleOptionError(f"Option '{self.name}' must be either 'true' or 'false'") self.value = value == 'true' @@ -103,37 +94,36 @@ class ListOption(RuleOption): if isinstance(value, list): the_list = value else: - the_list = ustr(value).split(",") + the_list = str(value).split(",") - self.value = [ustr(item.strip()) for item in the_list if item.strip() != ""] + self.value = [str(item.strip()) for item in the_list if item.strip() != ""] class PathOption(RuleOption): """ Option that accepts either a directory or both a directory and a file. """ - def __init__(self, name, value, description, type=u"dir"): + def __init__(self, name, value, description, type="dir"): self.type = type - super(PathOption, self).__init__(name, value, description) + super().__init__(name, value, description) @allow_none def set(self, value): - value = ustr(value) + value = str(value) - error_msg = u"" + error_msg = "" if self.type == 'dir': if not os.path.isdir(value): - error_msg = u"Option {0} must be an existing directory (current value: '{1}')".format(self.name, value) + error_msg = f"Option {self.name} must be an existing directory (current value: '{value}')" elif self.type == 'file': if not os.path.isfile(value): - error_msg = u"Option {0} must be an existing file (current value: '{1}')".format(self.name, value) + error_msg = f"Option {self.name} must be an existing file (current value: '{value}')" elif self.type == 'both': if not os.path.isdir(value) and not os.path.isfile(value): - error_msg = (u"Option {0} must be either an existing directory or file " - u"(current value: '{1}')").format(self.name, value) + error_msg = (f"Option {self.name} must be either an existing directory or file " + f"(current value: '{value}')") else: - error_msg = u"Option {0} type must be one of: 'file', 'dir', 'both' (current: '{1}')".format(self.name, - self.type) + error_msg = f"Option {self.name} type must be one of: 'file', 'dir', 'both' (current: '{self.type}')" if error_msg: raise RuleOptionError(error_msg) @@ -148,7 +138,7 @@ class RegexOption(RuleOption): try: self.value = re.compile(value, re.UNICODE) except (re.error, TypeError) as exc: - raise RuleOptionError("Invalid regular expression: '{0}'".format(exc)) + raise RuleOptionError(f"Invalid regular expression: '{exc}'") from exc def __deepcopy__(self, _): # copy.deepcopy() - used in rules.py - doesn't support copying regex objects prior to Python 3.7 |