Skip to content

Commit

Permalink
Merge pull request #96 from michaeljoseph/config
Browse files Browse the repository at this point in the history
Project configuration
  • Loading branch information
michaeljoseph committed Nov 8, 2014
2 parents 938bfa4 + 71d1fbc commit d306e93
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
4 changes: 2 additions & 2 deletions changes/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def write_new_changelog(repo_url, filename, content_lines, dry_run=True):
heading_and_newline = '# [Changelog](%s/releases)\n' % repo_url

with open(filename, 'r+') as f:
with click.open_file(filename, 'r+') as f:
existing = f.readlines()

output = existing[2:]
Expand All @@ -27,7 +27,7 @@ def write_new_changelog(repo_url, filename, content_lines, dry_run=True):
output = ''.join(output)

if not dry_run:
with open(filename, 'w+') as f:
with click.open_file(filename, 'w+') as f:
f.write(output)
else:
log.info('New changelog:\n%s', ''.join(content_lines))
Expand Down
4 changes: 3 additions & 1 deletion changes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def main(context, module_name, dry_run, debug, no_input, requirements, patch, mi

current_version = version.current_version(module_name)
repo_url = attributes.extract_attribute(module_name, '__url__')
context.obj = config.Changes(module_name, dry_run, debug, no_input, requirements, new_version, current_version, repo_url, version_prefix)
context.obj = config.CLI(module_name, dry_run, debug, no_input,
requirements, new_version, current_version,
repo_url, version_prefix)

probe.probe_project(context.obj)

Expand Down
37 changes: 34 additions & 3 deletions changes/config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
from os.path import exists, join

class Changes(object):
import click
import yaml

CONFIG_FILE = '.changes'
DEFAULTS = {
'changelog': 'CHANGELOG.md',
'readme': 'README.md',
}


class CLI(object):
test_command = None
pypi = None
skip_changelog = None

def __init__(self, module_name, dry_run, debug, no_input, requirements, new_version, current_version, repo_url, version_prefix):
def __init__(self, module_name, dry_run, debug, no_input, requirements,
new_version, current_version, repo_url, version_prefix):
self.module_name = module_name
self.dry_run = dry_run
self.debug = debug
self.no_input = no_input
self.requirements = requirements
self.new_version = version_prefix + new_version if version_prefix else new_version
self.new_version = (
version_prefix + new_version
if version_prefix
else new_version
)
self.current_version = current_version
self.repo_url = repo_url


def project_config(context):
config = {}
config_path = join(context.module_name, CONFIG_FILE)

# initialise config with defaults
if not exists(config_path):
config = DEFAULTS.copy()

with click.open_file(config_path, 'w') as f:
config_yaml = yaml.dump(config, default_flow_style=False)
f.write(config_yaml)

config = yaml.safe_load(click.open_file(config_path))
return config or {}
3 changes: 2 additions & 1 deletion requirements/runtime.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PyYAML < 4.0.0
plumbum < 1.5.0
click < 2.6.0
click < 4.0.0
path.py < 5.0.0
semantic_version < 3.0.0
twine < 1.4.0
Expand Down
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import io
import shutil

from changes.config import Changes
from changes.config import CLI


module_name = 'test_app'
context = Changes(module_name, True, True, True, '%s/requirements.txt' % module_name, '0.0.2', '0.0.1', 'https://github.com/someuser/test_app', None)
context = CLI(module_name, True, True, True, '%s/requirements.txt' % module_name, '0.0.2', '0.0.1', 'https://github.com/someuser/test_app', None)
context.requirements = '%s/requirements.txt' % module_name
context.tmp_file = '%s/__init__.py' % module_name
context.initial_init_content = [
Expand Down
25 changes: 25 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from os.path import exists

import click

from changes import config
from . import context, setup, teardown # noqa


def test_no_config():
assert not exists('test_app/.changes')
assert config.project_config(context) == config.DEFAULTS
assert exists('test_app/.changes')


def test_existing_config():
existing_config = 'foo: bar\nbaz: buzz\n'
with click.open_file('test_app/.changes', 'w') as f:
f.write(existing_config)
assert config.project_config(context) == {'foo': 'bar', 'baz': 'buzz'}


def test_malformed_config_returns_none():
with click.open_file('test_app/.changes', 'w') as f:
f.write('something\n\n-another thing\n')
assert config.project_config(context) == {}

0 comments on commit d306e93

Please sign in to comment.