-
Notifications
You must be signed in to change notification settings - Fork 42
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
Make the --requirements param to accept file and text #608
Make the --requirements param to accept file and text #608
Conversation
471d9ba
to
9b7a26c
Compare
pulpcore/cli/common/generic.py
Outdated
@@ -328,74 +328,59 @@ def _version_callback( | |||
return value | |||
|
|||
|
|||
# TODO: would be great to have enable this to take a validator, rather than having | |||
# to build "on top of" it like I'm doing now w/ json_callback | |||
def load_file_or_string_callback( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not grocking the whole change yet, but this thing now looks deceivingly like a decorator. Do you think we can use it as one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, sir!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this one can be used as a callback or chained when used as a decorator...
Maybe that should be in the docstring.
3b9c5f3
to
febb165
Compare
pulpcore/cli/ansible/remote.py
Outdated
callback=_requirements_callback, | ||
callback=lambda c, p, x: f"{yaml.safe_load(x)}" if x else x, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess this is not what we wanted...
pulpcore/cli/ansible/repository.py
Outdated
content_json_callback = create_content_json_callback(schema=CONTENT_LIST_SCHEMA) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason not to reuse the generated callback anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand. I am using literally the same callback as before a couple of lines below.
pulpcore/cli/common/generic.py
Outdated
@load_file_or_string_callback | ||
def string_callback(ctx: click.Context, param: click.Parameter, value: str) -> str: | ||
return value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just like using the load_file_or_string_callback directly.
pulpcore/cli/common/generic.py
Outdated
value = load_json_callback(ctx, param, value) | ||
@load_file_or_string_callback | ||
def labels_callback(ctx: click.Context, param: click.Parameter, value: str) -> Any: | ||
value = json_callback(ctx, param, value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe json_callback
should follow the chaining pattern. Also this version of json_callback
is already decorated. So we are attemting to lookup a file twice here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, i'd be ok to use the load_json_callback here. We never want a version of the label callback that cannot read from file, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes perfect sense. Thanks!
pulpcore/cli/common/generic.py
Outdated
callback=load_json_callback, | ||
callback=json_callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about the following naming convention?
load_string_callback
searches for "@" and attempts to load from file else value.
json_callback
transforms value
to json.
load_json_callback
is json_callback
decorated (chained) with load_string_callback
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string_callback
would be the same as specifying nothing...
febb165
to
aa56f4c
Compare
546ffa8
to
663c4ae
Compare
pulpcore/cli/ansible/remote.py
Outdated
callback=_requirements_callback, | ||
callback=requirements_callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has this ever been correct to to use the requirements callback? Also we should rename that one as all it does is load some yaml. Let's call it the yaml_callback
.
pulpcore/cli/ansible/remote.py
Outdated
help=_("Collections only: URL to receive a session token"), | ||
allowed_with_contexts=collection_context, | ||
), | ||
pulp_option( | ||
"--token", | ||
callback=_requirements_callback, | ||
callback=requirements_callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here.
pulpcore/cli/common/generic.py
Outdated
# to build "on top of" it like I'm doing now w/ json_callback | ||
def load_file_or_string_callback( | ||
ctx: click.Context, param: click.Parameter, value: Optional[str] | ||
def load_file_or_string_wrapper( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call this the load_file_wrapper
.
pulpcore/cli/common/generic.py
Outdated
# None or empty-str are legal - shortcircuit here | ||
if not value: | ||
return value | ||
load_file_or_string_callback = load_file_or_string_wrapper() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one the load_string_callback
pulpcore/cli/common/generic.py
Outdated
# None is legal - shortcircuit here | ||
if value is None: | ||
return value | ||
load_json_callback = load_file_or_string_wrapper(json_callback) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and they'd be in line with this one.
pulpcore/cli/common/generic.py
Outdated
value = load_json_callback(ctx, param, value) | ||
@load_file_or_string_callback | ||
def labels_callback(ctx: click.Context, param: click.Parameter, value: str) -> Any: | ||
value = json_callback(ctx, param, value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, i'd be ok to use the load_json_callback here. We never want a version of the label callback that cannot read from file, right?
pulpcore/cli/common/generic.py
Outdated
callback=load_json_callback, | ||
callback=load_file_or_string_callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The help text states we want JSON...
663c4ae
to
8e5e8df
Compare
pulpcore/cli/ansible/remote.py
Outdated
callback=_requirements_callback, | ||
callback=yaml_callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was a copy and paste error in the first place. The original callback didn't do a thing for this option (look at the if-elif structure of the code you removed).
Also we really expect just a string for a url, no?
pulpcore/cli/ansible/remote.py
Outdated
callback=_requirements_callback, | ||
callback=yaml_callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here.
pulpcore/cli/common/generic.py
Outdated
def load_file_or_string_callback( | ||
ctx: click.Context, param: click.Parameter, value: Optional[str] | ||
def load_file_wrapper( | ||
handler: Callable[[click.Context, click.Parameter, str], Any] = lambda c, p, x: x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, i'd be better to not have the default value here, but pass the lambda in line 364.
load_string_callback = load_file_wrapper(lambda c, p, x: x)
pulpcore/cli/common/generic.py
Outdated
callback=labels_callback, | ||
callback=load_json_callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing labels_callback
provides valuable validation. Please keep it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought you wanted to remove it by saying this: #608 (comment). My bad. I did this now:
def load_labels_callback(ctx: click.Context, param: click.Parameter, value: str) -> Any:
value = load_json_callback(ctx, param, value)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So labels are optionally read from file and always validated? Sounds right to me.
return None | ||
@load_file_wrapper | ||
def _content_list_callback(ctx: click.Context, param: click.Parameter, value: str) -> Any: | ||
result = json_callback(ctx, param, value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we still need the
if result is None:
return None
part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we do! Thanks!
8e5e8df
to
966867e
Compare
pulpcore/cli/common/generic.py
Outdated
load_json_callback = load_file_wrapper(json_callback) | ||
|
||
|
||
def load_labels_callback(ctx: click.Context, param: click.Parameter, value: str) -> Any: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can be more specific here.
def load_labels_callback(ctx: click.Context, param: click.Parameter, value: str) -> Any: | |
def load_labels_callback(ctx: click.Context, param: click.Parameter, value: str) -> Optional[Dict[str, str]]: |
(Don't forget to run black...)
Also when you change the name, can you verify that this is not used anywhere in the known plugins?
This should be my last request. We are ready to go then...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran git grep "labels_callback"
against pulp-cli, pulp-cli-deb, and pulp-cli-ostree. Only these occurrences were found:
pulpcore/cli/common/generic.py:def load_labels_callback(
pulpcore/cli/common/generic.py: callback=load_labels_callback,
I think we are good to go.
8150c53
to
4eae390
Compare
4eae390
to
1b77253
Compare
I have made a couple of |
closes #230