Skip to content

Commit d306e93

Browse files
committed
Merge pull request #96 from michaeljoseph/config
Project configuration
2 parents 938bfa4 + 71d1fbc commit d306e93

6 files changed

Lines changed: 68 additions & 9 deletions

File tree

changes/changelog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
def write_new_changelog(repo_url, filename, content_lines, dry_run=True):
1414
heading_and_newline = '# [Changelog](%s/releases)\n' % repo_url
1515

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

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

2929
if not dry_run:
30-
with open(filename, 'w+') as f:
30+
with click.open_file(filename, 'w+') as f:
3131
f.write(output)
3232
else:
3333
log.info('New changelog:\n%s', ''.join(content_lines))

changes/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def main(context, module_name, dry_run, debug, no_input, requirements, patch, mi
4646

4747
current_version = version.current_version(module_name)
4848
repo_url = attributes.extract_attribute(module_name, '__url__')
49-
context.obj = config.Changes(module_name, dry_run, debug, no_input, requirements, new_version, current_version, repo_url, version_prefix)
49+
context.obj = config.CLI(module_name, dry_run, debug, no_input,
50+
requirements, new_version, current_version,
51+
repo_url, version_prefix)
5052

5153
probe.probe_project(context.obj)
5254

changes/config.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1+
from os.path import exists, join
12

2-
class Changes(object):
3+
import click
4+
import yaml
5+
6+
CONFIG_FILE = '.changes'
7+
DEFAULTS = {
8+
'changelog': 'CHANGELOG.md',
9+
'readme': 'README.md',
10+
}
11+
12+
13+
class CLI(object):
314
test_command = None
415
pypi = None
516
skip_changelog = None
617

7-
def __init__(self, module_name, dry_run, debug, no_input, requirements, new_version, current_version, repo_url, version_prefix):
18+
def __init__(self, module_name, dry_run, debug, no_input, requirements,
19+
new_version, current_version, repo_url, version_prefix):
820
self.module_name = module_name
921
self.dry_run = dry_run
1022
self.debug = debug
1123
self.no_input = no_input
1224
self.requirements = requirements
13-
self.new_version = version_prefix + new_version if version_prefix else new_version
25+
self.new_version = (
26+
version_prefix + new_version
27+
if version_prefix
28+
else new_version
29+
)
1430
self.current_version = current_version
1531
self.repo_url = repo_url
1632

33+
34+
def project_config(context):
35+
config = {}
36+
config_path = join(context.module_name, CONFIG_FILE)
37+
38+
# initialise config with defaults
39+
if not exists(config_path):
40+
config = DEFAULTS.copy()
41+
42+
with click.open_file(config_path, 'w') as f:
43+
config_yaml = yaml.dump(config, default_flow_style=False)
44+
f.write(config_yaml)
45+
46+
config = yaml.safe_load(click.open_file(config_path))
47+
return config or {}

requirements/runtime.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
PyYAML < 4.0.0
12
plumbum < 1.5.0
2-
click < 2.6.0
3+
click < 4.0.0
34
path.py < 5.0.0
45
semantic_version < 3.0.0
56
twine < 1.4.0

tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import io
33
import shutil
44

5-
from changes.config import Changes
5+
from changes.config import CLI
66

77

88
module_name = 'test_app'
9-
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)
9+
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)
1010
context.requirements = '%s/requirements.txt' % module_name
1111
context.tmp_file = '%s/__init__.py' % module_name
1212
context.initial_init_content = [

tests/test_config.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from os.path import exists
2+
3+
import click
4+
5+
from changes import config
6+
from . import context, setup, teardown # noqa
7+
8+
9+
def test_no_config():
10+
assert not exists('test_app/.changes')
11+
assert config.project_config(context) == config.DEFAULTS
12+
assert exists('test_app/.changes')
13+
14+
15+
def test_existing_config():
16+
existing_config = 'foo: bar\nbaz: buzz\n'
17+
with click.open_file('test_app/.changes', 'w') as f:
18+
f.write(existing_config)
19+
assert config.project_config(context) == {'foo': 'bar', 'baz': 'buzz'}
20+
21+
22+
def test_malformed_config_returns_none():
23+
with click.open_file('test_app/.changes', 'w') as f:
24+
f.write('something\n\n-another thing\n')
25+
assert config.project_config(context) == {}

0 commit comments

Comments
 (0)