Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
aogier committed Feb 25, 2020
1 parent ee0d434 commit b558109
Show file tree
Hide file tree
Showing 10 changed files with 815 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dist: xenial
language: python

cache: pip

python:
- "3.6"
- "3.7"
- "3.8"

install:
- pip install -U -r requirements-dev.txt

script:
- scripts/test

after_script:
- pip install codecov
- codecov
1 change: 1 addition & 0 deletions chachacha/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.0"
Empty file added chachacha/drivers/__init__.py
Empty file.
128 changes: 128 additions & 0 deletions chachacha/drivers/kac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""
Created on 25 feb 2020
@author: Alessandro Ogier <[email protected]>
"""

import os.path
import typing
from datetime import datetime

import keepachangelog
import semver
from jinja2 import Template

DEFAULT_HEADER = """
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
""".strip()

TEMPLATE = Template(
"""
{{ header }}
{%- for version, changes in current.items() %}
## [{{ version }}]{% if changes.release_date %} - {{ changes.release_date }}{% endif %}
{%- for section in ['added', 'changed', 'deprecated', 'removed', 'fixed', 'security'] %}
{%- if changes[section] %}
### {{ section | title }}
{% for entry in changes[section] %}
{{ entry }}
{%- endfor %}
{%- endif %}
{%- endfor %}
{%- endfor %}
""".strip()
)


class ChangelogFormat:
def __init__(self, filename):
self.filename = filename

def init(self, overwrite: bool = False):
if os.path.exists(self.filename) and not overwrite:
print("file exists")
import sys

sys.exit(1)

with open(self.filename, "w") as outfile:
outfile.write(TEMPLATE.render(header=DEFAULT_HEADER, current={}) + "\n")

print("changelog created")

def _write(self, current: dict) -> None:
with open(self.filename, "w") as outfile:

outfile.write(
TEMPLATE.render(header=DEFAULT_HEADER, current=current) + "\n"
)

def add_entry(
self, section_name: str, changelog_line: typing.Union[str, tuple]
) -> None:

_changelog_line = "- " + " ".join(changelog_line)
current = keepachangelog.to_dict(self.filename, show_unreleased=True)

if "Unreleased" not in current:
unreleased = {"Unreleased": {"version": "Unreleased", "release_date": None}}

new = {}
new.update(unreleased)
new.update(current)

current = new

else:
unreleased = current["Unreleased"]

unreleased = current.setdefault(
"Unreleased", {"version": "Unreleased", "release_date": None}
)

section = unreleased.setdefault(section_name, [])

section.append(_changelog_line)

self._write(current)

def release(self, mode: str) -> None:

current = keepachangelog.to_dict(self.filename, show_unreleased=True)

if "Unreleased" not in current:
print("nothing to bump!")
import sys

sys.exit(1)

try:
last = [version for version in current if version != "Unreleased"][0]
except IndexError:
last = "0.0.0"

version = semver.parse_version_info(last)

if mode == "major":
last = str(version.bump_major())
elif mode == "minor":
last = str(version.bump_minor())
else:
last = str(version.bump_patch())

entries = current.pop("Unreleased")

changelog = {last: entries}
changelog[last]["release_date"] = datetime.now().isoformat().split("T")[0]
changelog.update(current)

self._write(changelog)
94 changes: 94 additions & 0 deletions chachacha/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
Created on 25 feb 2020
@author: Alessandro Ogier <[email protected]>
"""

import typing

import click
from click.core import Context

from chachacha import drivers
from chachacha.drivers.kac import ChangelogFormat


@click.group()
@click.option("--filename", default="CHANGELOG.md", help="changelog filename")
@click.option("--driver", default="kac", help="changelog format driver")
@click.pass_context
def main(ctx: Context, filename: str, driver: str) -> None:

driver = drivers.kac.ChangelogFormat(filename)

ctx.obj = driver


@main.command(help="initialize a new file")
@click.option("--overwrite", default=False, help="overwrite", is_flag=True)
@click.pass_obj
def init(driver: ChangelogFormat, overwrite: bool) -> None:

driver.init(overwrite)


@main.command(help='add an "added" entry')
@click.pass_obj
@click.argument("changes", nargs=-1)
def added(driver: ChangelogFormat, changes: typing.Union[str, tuple]) -> None:

driver.add_entry("added", changes)


@main.command(help='add a "changed" entry')
@click.pass_obj
@click.argument("changes", nargs=-1)
def changed(driver: ChangelogFormat, changes: typing.Union[str, tuple]) -> None:

driver.add_entry("changed", changes)


@main.command(help='add a "deprecated" entry')
@click.pass_obj
@click.argument("changes", nargs=-1)
def deprecated(driver: ChangelogFormat, changes: typing.Union[str, tuple]) -> None:

driver.add_entry("deprecated", changes)


@main.command(help='add a "removed" entry')
@click.pass_obj
@click.argument("changes", nargs=-1)
def removed(driver: ChangelogFormat, changes: typing.Union[str, tuple]) -> None:

driver.add_entry("removed", changes)


@main.command(help='add a "fixed" entry')
@click.pass_obj
@click.argument("changes", nargs=-1)
def fixed(driver: ChangelogFormat, changes: typing.Union[str, tuple]) -> None:

driver.add_entry("fixed", changes)


@main.command(help='add a "security" entry')
@click.pass_obj
@click.argument("changes", nargs=-1)
def security(driver: ChangelogFormat, changes: typing.Union[str, tuple]) -> None:

driver.add_entry("security", changes)


@main.command(help="release a version")
@click.option("--major", "mode", flag_value="major", help="overwrite")
@click.option("--minor", "mode", flag_value="minor", help="overwrite")
@click.option("--patch", "mode", flag_value="patch", help="overwrite", default=True)
@click.pass_obj
def release(driver: ChangelogFormat, mode: str) -> None:

driver.release(mode)


if __name__ == "__main__": # pragma: no cover
main() # pylint: disable=no-value-for-parameter
Loading

0 comments on commit b558109

Please sign in to comment.