diff options
author | Joris Roovers <jroovers@cisco.com> | 2018-03-30 16:51:20 +0200 |
---|---|---|
committer | Joris Roovers <joris.roovers@gmail.com> | 2019-07-30 15:29:04 +0200 |
commit | 8f4d48d604763816bd311b4a764f0b94447cc926 (patch) | |
tree | 4389c3a2c8474e89a17fc6daad256da69a1e1590 | |
parent | fe91fa91aca92e9ef6731c8ad63a87f4941c93f7 (diff) |
Playing around with format stringsformat-strings
Needs a bunch more work
-rw-r--r-- | gitlint/cli.py | 17 | ||||
-rw-r--r-- | gitlint/config.py | 20 | ||||
-rw-r--r-- | gitlint/display.py | 5 | ||||
-rw-r--r-- | gitlint/lint.py | 9 |
4 files changed, 37 insertions, 14 deletions
diff --git a/gitlint/cli.py b/gitlint/cli.py index f3099b2..9c26035 100644 --- a/gitlint/cli.py +++ b/gitlint/cli.py @@ -49,7 +49,7 @@ def log_system_info(): def build_config( # pylint: disable=too-many-arguments - ctx, target, config_path, c, extra_path, ignore, contrib, ignore_stdin, verbose, silent, debug + ctx, target, config_path, c, extra_path, ignore, contrib, ignore_stdin, format, verbose, silent, debug ): """ Creates a LintConfig object based on a set of commandline parameters. """ config_builder = LintConfigBuilder() @@ -76,6 +76,8 @@ def build_config( # pylint: disable=too-many-arguments if silent: config_builder.set_option('general', 'verbosity', 0) + elif format: + config_builder.set_option('general', 'format', format) elif verbose > 0: config_builder.set_option('general', 'verbosity', verbose) @@ -165,6 +167,7 @@ def build_git_context(lint_config, msg_filename, refspec): @click.option('--contrib', default="", help="Contrib rules to enable (comma-separated by id or name).") @click.option('--msg-filename', type=click.File(), help="Path to a file containing a commit-msg.") @click.option('--ignore-stdin', is_flag=True, help="Ignore any stdin data. Useful for running in CI server.") +@click.option('--format', nargs=2, multiple=True, help="Format string to customize gitlint's output format.") @click.option('-v', '--verbose', count=True, default=0, help="Verbosity, more v's for more verbose output (e.g.: -v, -vv, -vvv). [default: -vvv]", ) @click.option('-s', '--silent', help="Silent mode (no output). Takes precedence over -v, -vv, -vvv.", is_flag=True) @@ -173,7 +176,7 @@ def build_git_context(lint_config, msg_filename, refspec): @click.pass_context def cli( # pylint: disable=too-many-arguments ctx, target, config, c, commits, extra_path, ignore, contrib, - msg_filename, ignore_stdin, verbose, silent, debug, + msg_filename, ignore_stdin, format, verbose, silent, debug, ): """ Git lint tool, checks your git commit messages for styling issues @@ -190,7 +193,7 @@ def cli( # pylint: disable=too-many-arguments # Get the lint config from the commandline parameters and # store it in the context (click allows storing an arbitrary object in ctx.obj). config, config_builder = build_config(ctx, target, config, c, extra_path, - ignore, contrib, ignore_stdin, verbose, silent, debug) + ignore, contrib, ignore_stdin, format, verbose, silent, debug) LOG.debug(u"Configuration\n%s", ustr(config)) ctx.obj = (config, config_builder, commits, msg_filename) @@ -245,10 +248,10 @@ def lint(ctx): if violations: # Display the commit hash & new lines intelligently if number_of_commits > 1 and commit.sha: - linter.display.e(u"{0}Commit {1}:".format( - "\n" if not first_violation or commit is last_commit else "", - commit.sha[:10] - )) + render_context = {"newline": "\n" if not first_violation or commit is last_commit else "", + "sha": commit.sha[:10]} + linter.display.render_template("commit", render_context) + linter.print_violations(violations) first_violation = False diff --git a/gitlint/config.py b/gitlint/config.py index 2d892bb..500e203 100644 --- a/gitlint/config.py +++ b/gitlint/config.py @@ -76,6 +76,26 @@ class LintConfig(object): self._config_path = None ignore_stdin_description = "Ignore any stdin data. Useful for running in CI server." self._ignore_stdin = options.BoolOption('ignore-stdin', False, ignore_stdin_description) + self._format = {} + + # def format() + + def templates(self, template_name): + if template_name not in self._format: + if template_name == "violation": + if self.verbosity == 1: + return u"{line_nr}: {rule_id}" + elif self.verbosity == 2: + return u"{line_nr}: {rule_id} {message}" + elif self.verbosity == 3: + # TODO: fix case if there's no content! + # self.display.eee(u"{line_nr}: {rule_id} {message}".format(**violation_context), exact=True) + return u"{line_nr}: {rule_id} {message}: \"{content}\"" + elif template_name == "commit": + # TODO: the newline probably doesn't need to be part of the template? + return u"{newline}Commit {sha}:" + + return self._format[template_name] if self._format else None @property def target(self): diff --git a/gitlint/display.py b/gitlint/display.py index dd17ac0..14b2c12 100644 --- a/gitlint/display.py +++ b/gitlint/display.py @@ -44,3 +44,8 @@ class Display(object): def eee(self, message, exact=False): # pylint: disable=invalid-name self._output(message, 3, exact, stderr) + + def render_template(self, template_name, context_dict, stream=stderr): # pylint: disable=invalid-name + message = self.config.templates(template_name).format(**context_dict) + stream.write(message + "\n") + # self._output(message, 3, exact, stderr) diff --git a/gitlint/lint.py b/gitlint/lint.py index 182a55a..228825a 100644 --- a/gitlint/lint.py +++ b/gitlint/lint.py @@ -99,10 +99,5 @@ class GitLinter(object): """ Print a given set of violations to the standard error output """ for v in violations: line_nr = v.line_nr if v.line_nr else "-" - self.display.e(u"{0}: {1}".format(line_nr, v.rule_id), exact=True) - self.display.ee(u"{0}: {1} {2}".format(line_nr, v.rule_id, v.message), exact=True) - if v.content: - self.display.eee(u"{0}: {1} {2}: \"{3}\"".format(line_nr, v.rule_id, v.message, v.content), - exact=True) - else: - self.display.eee(u"{0}: {1} {2}".format(line_nr, v.rule_id, v.message), exact=True) + violation_context = {"line_nr": line_nr, "rule_id": v.rule_id, "message": v.message, "content": v.content} + self.display.render_template("violation", violation_context) |