diff --git a/CHANGELOG.md b/CHANGELOG.md index 575cc1f..952ecf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## [unreleased] - XXXX-XX-XX * Added --version option +* Improved quickstart: + * respective default answers will be written to config if user provided no + answer + * added tests for quickstart ## [1.3.1] - 2022-06-10 diff --git a/blag/blag.py b/blag/blag.py index 32c6338..3c0a908 100644 --- a/blag/blag.py +++ b/blag/blag.py @@ -17,6 +17,7 @@ import feedgenerator from blag.markdown import markdown_factory, convert_markdown from blag.devserver import serve from blag.version import __VERSION__ +from blag.quickstart import quickstart logger = logging.getLogger(__name__) logging.basicConfig( @@ -425,33 +426,5 @@ def generate_tags(articles, tags_template, tag_template, output_dir): fh.write(result) -def quickstart(args): - """Quickstart. - - This method asks the user some questions and generates a - configuration file that is needed in order to run blag. - - Parameters - ---------- - args : argparse.Namespace - - """ - base_url = input("Hostname (and path) to the root? " - "[https://example.com/]: ") - title = input("Title of your website? ") - description = input("Description of your website [John Doe's Blog]? ") - author = input("Author of your website [John Doe]? ") - - config = configparser.ConfigParser() - config['main'] = { - 'base_url': base_url, - 'title': title, - 'description': description, - 'author': author, - } - with open('config.ini', 'w') as fh: - config.write(fh) - - if __name__ == '__main__': main() diff --git a/blag/quickstart.py b/blag/quickstart.py new file mode 100644 index 0000000..00b4725 --- /dev/null +++ b/blag/quickstart.py @@ -0,0 +1,68 @@ +"""Helper methods for blag's quickstart command. + +""" + +import configparser + + +def get_input(question, default): + """Prompt for user input. + + This is a wrapper around the input-builtin. It will show the default answer + in the prompt and -- if no answer was given -- use the default. + + Parameters + ---------- + question : str + the question the user is presented + default : str + the default value that will be used if no answer was given + + Returns + ------- + str + + """ + reply = input(f"{question} [{default}]: ") + if not reply: + reply = default + return reply + + +def quickstart(args): + """Quickstart. + + This method asks the user some questions and generates a + configuration file that is needed in order to run blag. + + Parameters + ---------- + args : argparse.Namespace + + """ + base_url = get_input( + "Hostname (and path) to the root?", + "https://example.com/", + ) + title = get_input( + "Title of your website?", + "My little blog", + ) + description = get_input( + "Description of your website?", + "John Doe's Blog", + ) + author = get_input( + "Author of your website", + "John Doe", + ) + + config = configparser.ConfigParser() + config['main'] = { + 'base_url': base_url, + 'title': title, + 'description': description, + 'author': author, + } + with open('config.ini', 'w') as fh: + config.write(fh) diff --git a/docs/api.rst b/docs/api.rst index 6a6e941..f856c26 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -9,3 +9,4 @@ API blag.blag blag.markdown blag.devserver + blag.quickstart diff --git a/tests/test_quickstart.py b/tests/test_quickstart.py new file mode 100644 index 0000000..4594497 --- /dev/null +++ b/tests/test_quickstart.py @@ -0,0 +1,24 @@ +from blag.quickstart import get_input, quickstart + + +def test_get_input_default_answer(monkeypatch): + monkeypatch.setattr('builtins.input', lambda x: '') + answer = get_input("foo", "bar") + assert answer == 'bar' + + +def test_get_input(monkeypatch): + monkeypatch.setattr('builtins.input', lambda x: 'baz') + answer = get_input("foo", "bar") + assert answer == 'baz' + + +def test_quickstart(cleandir, monkeypatch): + monkeypatch.setattr('builtins.input', lambda x: 'foo') + quickstart(None) + with open('config.ini', 'r') as fh: + data = fh.read() + assert 'base_url = foo' in data + assert 'title = foo' in data + assert 'description = foo' in data + assert 'author = foo' in data