Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter resource enum class name for argument parsing error messages #170

Merged
merged 4 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Version 0.13.2
-------------

**Features**
- Improve error messages for CLI invocations in case invalid value was provided for argument with `Enum` type. [pull request #170](https://github.com/codemagic-ci-cd/cli-tools/pull/170)

**Fixes**

- Use correct package type for `altool` commands when publishing tvOS apps using `app-store-connect publish`. [PR #173](https://github.com/codemagic-ci-cd/cli-tools/pull/173)
Expand Down
29 changes: 29 additions & 0 deletions src/codemagic/apple/resources/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import enum
import re
from typing import Optional
from typing import Tuple

Expand All @@ -20,12 +21,14 @@ class ResourceEnumMeta(enum.EnumMeta):
"""

graceful_fallback = True
enable_name_transformation = False

def __call__(cls, value, *args, **kwargs): # noqa: N805
try:
return super().__call__(value, *args, **kwargs)
except ValueError as ve:
if not cls.graceful_fallback:
cls._transform_class_name()
raise
logger = log.get_logger(cls, log_to_stream=False)
logger.warning('Undefined Resource enumeration: %s', ve)
Expand All @@ -35,6 +38,32 @@ def __call__(cls, value, *args, **kwargs): # noqa: N805
except TypeError:
raise ve

def _transform_class_name(cls): # noqa: N805
"""
If enabled, transform CamelCase class name 'ClassName' to more
readable 'class name', which appears prettier in argparse error messages.
"""
if not cls.enable_name_transformation:
return
formatted_name = re.sub(r'([A-Z])', lambda m: f' {m.group(1).lower()}', cls.__name__)
cls.__name__ = formatted_name.strip()

@staticmethod
@contextlib.contextmanager
def cli_arguments_parsing_mode():
original_graceful_fallback = ResourceEnumMeta.graceful_fallback
original_enable_name_transformation = ResourceEnumMeta.enable_name_transformation

# Turn off graceful enumeration fallback to get proper error messages
ResourceEnumMeta.graceful_fallback = False
# Enable name transformation to obtain pretty argparse error messages
ResourceEnumMeta.enable_name_transformation = True
try:
yield
finally:
ResourceEnumMeta.graceful_fallback = original_graceful_fallback
ResourceEnumMeta.enable_name_transformation = original_enable_name_transformation

@staticmethod
@contextlib.contextmanager
def without_graceful_fallback():
Expand Down
6 changes: 1 addition & 5 deletions src/codemagic/cli/cli_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,8 @@ def _resolve_cli_invocation_arg(cls):
from codemagic.apple.resources.enums import ResourceEnumMeta

parser = cls._setup_cli_options()
# Turn off graceful enumeration fallback to get proper error messages
ResourceEnumMeta.graceful_fallback = False
try:
with ResourceEnumMeta.cli_arguments_parsing_mode():
args = parser.parse_args()
finally:
ResourceEnumMeta.graceful_fallback = True

return parser, args

Expand Down