From a2249274a16e70a90df1ac5a77c3fc314a5a2a12 Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Mon, 11 Jan 2021 18:21:13 -0700 Subject: [PATCH 01/11] Replace poyo with pyyaml. --- cookiecutter/config.py | 9 +++++---- setup.py | 2 +- tests/conftest.py | 6 ------ 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/cookiecutter/config.py b/cookiecutter/config.py index a07aa1a8b..560db2fcc 100644 --- a/cookiecutter/config.py +++ b/cookiecutter/config.py @@ -5,6 +5,7 @@ import os import poyo +import yaml from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration @@ -62,11 +63,11 @@ def get_config(config_path): logger.debug('config_path is %s', config_path) with open(config_path, encoding='utf-8') as file_handle: try: - yaml_dict = poyo.parse_string(file_handle.read()) - except poyo.exceptions.PoyoException as e: + yaml_dict = yaml.safe_load(file_handle) + except yaml.YAMLError as e: raise InvalidConfiguration( - 'Unable to parse YAML file {}. Error: {}'.format(config_path, e) - ) + 'Unable to parse YAML file {}.'.format(config_path) + ) from e config_dict = merge_configs(DEFAULT_CONFIG, yaml_dict) diff --git a/setup.py b/setup.py index 6ab7e9abd..e3c006e16 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ 'binaryornot>=0.4.4', 'Jinja2<3.0.0', 'click>=7.0', - 'poyo>=0.5.0', + 'pyyaml>=5.3.1', 'jinja2-time>=0.2.0', 'python-slugify>=4.0.0', 'requests>=2.23.0', diff --git a/tests/conftest.py b/tests/conftest.py index ee3eab6d7..23dc24d68 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -175,9 +175,3 @@ def user_config_file(user_dir, user_config_data): config_text = USER_CONFIG.format(**user_config_data) config_file.write(config_text) return str(config_file) - - -@pytest.fixture(autouse=True) -def disable_poyo_logging(): - """Fixture that disables poyo logging.""" - logging.getLogger('poyo').setLevel(logging.WARNING) From bd4b45aa67707264b7520fc6b95181c715366c70 Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Mon, 11 Jan 2021 19:38:47 -0700 Subject: [PATCH 02/11] remove unused imports --- cookiecutter/config.py | 1 - tests/conftest.py | 1 - 2 files changed, 2 deletions(-) diff --git a/cookiecutter/config.py b/cookiecutter/config.py index 560db2fcc..09e9adc62 100644 --- a/cookiecutter/config.py +++ b/cookiecutter/config.py @@ -4,7 +4,6 @@ import logging import os -import poyo import yaml from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration diff --git a/tests/conftest.py b/tests/conftest.py index 23dc24d68..c2e303b22 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,4 @@ """pytest fixtures which are globally available throughout the suite.""" -import logging import os import shutil From 2389fe5695ced62c4a01d127379e1416c32d44cb Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Mon, 11 Jan 2021 20:08:43 -0700 Subject: [PATCH 03/11] assert that the raised exception is directly caused by a YAML error. --- tests/test_get_config.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/test_get_config.py b/tests/test_get_config.py index 9466dc382..55aecd5b9 100644 --- a/tests/test_get_config.py +++ b/tests/test_get_config.py @@ -2,6 +2,7 @@ import os import pytest +import yaml from cookiecutter import config from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration @@ -82,13 +83,13 @@ def test_get_config_does_not_exist(): def test_invalid_config(): """An invalid config file should raise an `InvalidConfiguration` \ exception.""" - with pytest.raises(InvalidConfiguration) as exc_info: - config.get_config('tests/test-config/invalid-config.yaml') - expected_error_msg = ( - 'Unable to parse YAML file tests/test-config/invalid-config.yaml. Error: ' + 'Unable to parse YAML file tests/test-config/invalid-config.yaml.' ) - assert expected_error_msg in str(exc_info.value) + with pytest.raises(InvalidConfiguration) as exc_info: + config.get_config('tests/test-config/invalid-config.yaml') + assert expected_error_msg in str(exc_info.value) + assert isinstance(exc_info.value.__cause__, yaml.YAMLError) def test_get_config_with_defaults(): From 38733cc1ae5a643551e288cf5c8718cb3f79fd3d Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Mon, 11 Jan 2021 20:28:17 -0700 Subject: [PATCH 04/11] In YAML, use single quotes for unescaped backslashes. --- tests/test_cli.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index a2d8accee..54ddfc35f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -499,7 +499,11 @@ def test_debug_list_installed_templates(cli_runner, debug_file, user_config_path fake_template_dir = os.path.dirname(os.path.abspath('fake-project')) os.makedirs(os.path.dirname(user_config_path)) with open(user_config_path, 'w') as config_file: - config_file.write('cookiecutters_dir: "%s"' % fake_template_dir) + # In YAML, double quotes mean to use escape sequences. + # Single quotes mean we will have unescaped backslahes. + # http://blogs.perl.org/users/tinita/2018/03/ + # strings-in-yaml---to-quote-or-not-to-quote.html + config_file.write("cookiecutters_dir: '%s'" % fake_template_dir) open(os.path.join('fake-project', 'cookiecutter.json'), 'w').write('{}') result = cli_runner( From 47eebba6d1fd64230a7f0b4670c63701de6609e2 Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Mon, 11 Jan 2021 20:37:59 -0700 Subject: [PATCH 05/11] Use single quotes for unescaped backslashes. --- tests/conftest.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index c2e303b22..57964f903 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,9 +8,13 @@ USER_CONFIG = """ -cookiecutters_dir: "{cookiecutters_dir}" -replay_dir: "{replay_dir}" +cookiecutters_dir: '{cookiecutters_dir}' +replay_dir: '{replay_dir}' """ +# In YAML, double quotes mean to use escape sequences. +# Single quotes mean we will have unescaped backslahes. +# http://blogs.perl.org/users/tinita/2018/03/ +# strings-in-yaml---to-quote-or-not-to-quote.html def backup_dir(original_dir, backup_dir): From 6962ad9dde4b3cbc07d39b3850687742f297e7fe Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Tue, 2 Feb 2021 12:02:45 -0700 Subject: [PATCH 06/11] Replace pyyaml with ruyaml. --- cookiecutter/config.py | 6 +++--- setup.py | 2 +- tests/test_get_config.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cookiecutter/config.py b/cookiecutter/config.py index 09e9adc62..a36a2d834 100644 --- a/cookiecutter/config.py +++ b/cookiecutter/config.py @@ -4,7 +4,7 @@ import logging import os -import yaml +import ruyaml from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration @@ -62,8 +62,8 @@ def get_config(config_path): logger.debug('config_path is %s', config_path) with open(config_path, encoding='utf-8') as file_handle: try: - yaml_dict = yaml.safe_load(file_handle) - except yaml.YAMLError as e: + yaml_dict = ruyaml.YAML(typ='safe').load(file_handle) + except ruyaml.YAMLError as e: raise InvalidConfiguration( 'Unable to parse YAML file {}.'.format(config_path) ) from e diff --git a/setup.py b/setup.py index e3c006e16..48927d9c5 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ 'binaryornot>=0.4.4', 'Jinja2<3.0.0', 'click>=7.0', - 'pyyaml>=5.3.1', + 'ruyaml', 'jinja2-time>=0.2.0', 'python-slugify>=4.0.0', 'requests>=2.23.0', diff --git a/tests/test_get_config.py b/tests/test_get_config.py index 55aecd5b9..c9cf2049c 100644 --- a/tests/test_get_config.py +++ b/tests/test_get_config.py @@ -2,7 +2,7 @@ import os import pytest -import yaml +import ruyaml from cookiecutter import config from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration @@ -89,7 +89,7 @@ def test_invalid_config(): with pytest.raises(InvalidConfiguration) as exc_info: config.get_config('tests/test-config/invalid-config.yaml') assert expected_error_msg in str(exc_info.value) - assert isinstance(exc_info.value.__cause__, yaml.YAMLError) + assert isinstance(exc_info.value.__cause__, ruyaml.YAMLError) def test_get_config_with_defaults(): From 1f5a28ee2d3dfbaf9d2aea51fc6a2bfdc1860e95 Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Thu, 4 Feb 2021 08:00:28 -0700 Subject: [PATCH 07/11] Sounds like this conversation is going to be thorny and complicated, so the first thing I want to do is pull some syntax shenanigans to make it easy to switch back and forth between using ruyaml and using pyyaml. --- cookiecutter/config.py | 6 ++++-- tests/test_get_config.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cookiecutter/config.py b/cookiecutter/config.py index a36a2d834..61e8917b5 100644 --- a/cookiecutter/config.py +++ b/cookiecutter/config.py @@ -5,6 +5,8 @@ import os import ruyaml +from ruyaml import YAMLError +yaml = ruyaml.YAML(typ='safe') from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration @@ -62,8 +64,8 @@ def get_config(config_path): logger.debug('config_path is %s', config_path) with open(config_path, encoding='utf-8') as file_handle: try: - yaml_dict = ruyaml.YAML(typ='safe').load(file_handle) - except ruyaml.YAMLError as e: + yaml_dict = yaml.load(file_handle) + except YAMLError as e: raise InvalidConfiguration( 'Unable to parse YAML file {}.'.format(config_path) ) from e diff --git a/tests/test_get_config.py b/tests/test_get_config.py index c9cf2049c..092c5fd27 100644 --- a/tests/test_get_config.py +++ b/tests/test_get_config.py @@ -2,7 +2,7 @@ import os import pytest -import ruyaml +from ruyaml import YAMLError from cookiecutter import config from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration @@ -89,7 +89,7 @@ def test_invalid_config(): with pytest.raises(InvalidConfiguration) as exc_info: config.get_config('tests/test-config/invalid-config.yaml') assert expected_error_msg in str(exc_info.value) - assert isinstance(exc_info.value.__cause__, ruyaml.YAMLError) + assert isinstance(exc_info.value.__cause__, YAMLError) def test_get_config_with_defaults(): From 3ffa2f2b0f3854b146124793df0e527f8073a7de Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Thu, 4 Feb 2021 08:08:16 -0700 Subject: [PATCH 08/11] add a little comment explaining --- cookiecutter/config.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cookiecutter/config.py b/cookiecutter/config.py index 61e8917b5..f63bff42f 100644 --- a/cookiecutter/config.py +++ b/cookiecutter/config.py @@ -6,7 +6,10 @@ import ruyaml from ruyaml import YAMLError + yaml = ruyaml.YAML(typ='safe') +# import yaml +# To swap, all we need to do is swap these lines above. from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration From 5b3d0ac8a754794b1db1b32608aa82599d587394 Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Thu, 4 Feb 2021 08:18:09 -0700 Subject: [PATCH 09/11] flake8 --- cookiecutter/config.py | 7 ++++--- tests/test_get_config.py | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cookiecutter/config.py b/cookiecutter/config.py index f63bff42f..f231b7615 100644 --- a/cookiecutter/config.py +++ b/cookiecutter/config.py @@ -4,15 +4,16 @@ import logging import os -import ruyaml +from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration + from ruyaml import YAMLError +# from yaml import YAMLError +import ruyaml yaml = ruyaml.YAML(typ='safe') # import yaml # To swap, all we need to do is swap these lines above. -from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration - logger = logging.getLogger(__name__) USER_CONFIG_PATH = os.path.expanduser('~/.cookiecutterrc') diff --git a/tests/test_get_config.py b/tests/test_get_config.py index 092c5fd27..6848f79b3 100644 --- a/tests/test_get_config.py +++ b/tests/test_get_config.py @@ -3,6 +3,7 @@ import pytest from ruyaml import YAMLError +# from yaml import YAMLError from cookiecutter import config from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration From 54e767b27d48eb930fe6fa18c4f8c3d0cda7f765 Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Thu, 4 Feb 2021 08:22:04 -0700 Subject: [PATCH 10/11] why black wants this I have no idea --- cookiecutter/config.py | 1 + tests/test_get_config.py | 1 + 2 files changed, 2 insertions(+) diff --git a/cookiecutter/config.py b/cookiecutter/config.py index f231b7615..615e0a626 100644 --- a/cookiecutter/config.py +++ b/cookiecutter/config.py @@ -7,6 +7,7 @@ from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration from ruyaml import YAMLError + # from yaml import YAMLError import ruyaml diff --git a/tests/test_get_config.py b/tests/test_get_config.py index 6848f79b3..fa5e9923a 100644 --- a/tests/test_get_config.py +++ b/tests/test_get_config.py @@ -3,6 +3,7 @@ import pytest from ruyaml import YAMLError + # from yaml import YAMLError from cookiecutter import config From 7941fcf29a6d3621e161b1cdc93592b856e9f7f1 Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Thu, 4 Feb 2021 08:28:32 -0700 Subject: [PATCH 11/11] Failed to download action 'https://api.github.com/repos/actions/checkout/zipball/5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f'