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

Make the --requirements param to accept file and text #608

Merged
merged 1 commit into from
Jan 18, 2023

Conversation

lubosmj
Copy link
Member

@lubosmj lubosmj commented Nov 30, 2022

closes #230

@lubosmj lubosmj force-pushed the requirements-option-accept-str-file-230 branch 3 times, most recently from 471d9ba to 9b7a26c Compare December 1, 2022 10:30
@@ -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(
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, sir!

Copy link
Member

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.

@lubosmj lubosmj force-pushed the requirements-option-accept-str-file-230 branch 6 times, most recently from 3b9c5f3 to febb165 Compare December 2, 2022 19:15
@lubosmj lubosmj marked this pull request as ready for review December 2, 2022 19:49
Comment on lines 72 to 71
callback=_requirements_callback,
callback=lambda c, p, x: f"{yaml.safe_load(x)}" if x else x,
Copy link
Member

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...

Comment on lines 149 to 148
content_json_callback = create_content_json_callback(schema=CONTENT_LIST_SCHEMA)

Copy link
Member

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?

Copy link
Member Author

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.

Comment on lines 359 to 361
@load_file_or_string_callback
def string_callback(ctx: click.Context, param: click.Parameter, value: str) -> str:
return value
Copy link
Member

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.

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)
Copy link
Member

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.

Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes perfect sense. Thanks!

Comment on lines 727 to 721
callback=load_json_callback,
callback=json_callback,
Copy link
Member

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.

Copy link
Member

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...

@lubosmj lubosmj marked this pull request as draft December 5, 2022 20:28
@lubosmj lubosmj force-pushed the requirements-option-accept-str-file-230 branch from febb165 to aa56f4c Compare December 12, 2022 17:54
@lubosmj lubosmj marked this pull request as ready for review December 13, 2022 19:59
@lubosmj lubosmj marked this pull request as draft December 15, 2022 13:00
@lubosmj lubosmj force-pushed the requirements-option-accept-str-file-230 branch 3 times, most recently from 546ffa8 to 663c4ae Compare December 15, 2022 13:19
@lubosmj lubosmj marked this pull request as ready for review December 15, 2022 13:39
pulpcore/cli/ansible/remote.py Show resolved Hide resolved
Comment on lines 87 to 88
callback=_requirements_callback,
callback=requirements_callback,
Copy link
Member

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.

help=_("Collections only: URL to receive a session token"),
allowed_with_contexts=collection_context,
),
pulp_option(
"--token",
callback=_requirements_callback,
callback=requirements_callback,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

# 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(
Copy link
Member

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.

# None or empty-str are legal - shortcircuit here
if not value:
return value
load_file_or_string_callback = load_file_or_string_wrapper()
Copy link
Member

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

# None is legal - shortcircuit here
if value is None:
return value
load_json_callback = load_file_or_string_wrapper(json_callback)
Copy link
Member

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.

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)
Copy link
Member

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?

Comment on lines 794 to 792
callback=load_json_callback,
callback=load_file_or_string_callback,
Copy link
Member

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...

@lubosmj lubosmj marked this pull request as draft December 15, 2022 17:37
@lubosmj lubosmj force-pushed the requirements-option-accept-str-file-230 branch from 663c4ae to 8e5e8df Compare January 8, 2023 17:23
@lubosmj lubosmj marked this pull request as ready for review January 8, 2023 18:14
@lubosmj lubosmj requested a review from mdellweg January 8, 2023 18:14
Comment on lines 87 to 91
callback=_requirements_callback,
callback=yaml_callback,
Copy link
Member

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?

Comment on lines 93 to 97
callback=_requirements_callback,
callback=yaml_callback,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

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
Copy link
Member

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)

Comment on lines 845 to 831
callback=labels_callback,
callback=load_json_callback,
Copy link
Member

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.

Copy link
Member Author

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)

Copy link
Member

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)
Copy link
Member

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?

Copy link
Member Author

@lubosmj lubosmj Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we do! Thanks!

@lubosmj lubosmj marked this pull request as draft January 12, 2023 17:26
@lubosmj lubosmj force-pushed the requirements-option-accept-str-file-230 branch from 8e5e8df to 966867e Compare January 16, 2023 21:43
@lubosmj lubosmj marked this pull request as ready for review January 16, 2023 22:42
@lubosmj lubosmj requested a review from mdellweg January 16, 2023 22:42
load_json_callback = load_file_wrapper(json_callback)


def load_labels_callback(ctx: click.Context, param: click.Parameter, value: str) -> Any:
Copy link
Member

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.

Suggested change
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...

Copy link
Member Author

@lubosmj lubosmj Jan 18, 2023

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.

@lubosmj lubosmj force-pushed the requirements-option-accept-str-file-230 branch 2 times, most recently from 8150c53 to 4eae390 Compare January 18, 2023 14:10
@lubosmj lubosmj force-pushed the requirements-option-accept-str-file-230 branch from 4eae390 to 1b77253 Compare January 18, 2023 14:12
@lubosmj
Copy link
Member Author

lubosmj commented Jan 18, 2023

I have made a couple of value parameters to receive Optional[str] values. I hope it is alright.

@mdellweg mdellweg enabled auto-merge (rebase) January 18, 2023 14:15
@mdellweg mdellweg merged commit 7fd4fa3 into pulp:main Jan 18, 2023
@mdellweg mdellweg added this to the 0.17.0 milestone Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change the --requirements option on ansible remote commands to accept a string or file
3 participants