diff --git a/pre-commit b/pre-commit index 48ccb8e7..c79b9ec1 100755 --- a/pre-commit +++ b/pre-commit @@ -80,17 +80,15 @@ def espurna_get_version(base, version_h="code/espurna/config/version.h"): return version - -SHIELD_TRAVIS = "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \ - "(https://travis-ci.org/{USER}/{REPO})" - -SHIELD_VERSION = "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)" - -SHIELD_BRANCH = "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \ - "(https://github.org/{USER}/{REPO}/tree/{BRANCH}/)" - -SHIELD_CODACY = "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \ - "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)" +TEMPLATES = { + "![travis]": "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \ + "(https://travis-ci.org/{USER}/{REPO})\n", + "![version]": "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)\n", + "![branch]": "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \ + "(https://github.org/{USER}/{REPO}/tree/{BRANCH}/)\n", + "![codacy]": "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \ + "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)\n" +} README = "README.md" @@ -99,31 +97,28 @@ if __name__ == "__main__": base = os.getcwd() user, repo = git_parse_remote() - template = { + fmt = { "USER": user, "REPO": repo, "BRANCH": git_branch(), "VERSION": espurna_get_version(base) } + templates = [ + (k, tmpl.format(**fmt)) + for k, tmpl in TEMPLATES.items() + ] + + def fmt_line(line): + for match, tmpl in templates: + if match in line: + return tmpl - shield_travis = SHIELD_TRAVIS.format(**template) - shield_version = SHIELD_VERSION.format(**template) - shield_branch = SHIELD_BRANCH.format(**template) - shield_codacy = SHIELD_CODACY.format(**template) + return line path = os.path.join(base, README) with FileInput(path, inplace=True) as readme: for line in readme: - if "![travis]" in line: - print(shield_travis) - elif "![version]" in line: - print(shield_version) - elif "![branch]" in line: - print(shield_branch) - elif "![codacy]" in line: - print(shield_codacy) - else: - print(line, end="") - - sys.exit(call(["git", "add", README], cwd=base)) + print(fmt_line(line), end='') + + sys.exit(call(["git", "add", README]))