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

Implemented the CLI with Click #309

Closed
wants to merge 6 commits into from
Closed

Conversation

vishav1771
Copy link
Contributor

@vishav1771 vishav1771 commented Mar 20, 2020

Description

Implemented the CLI with Click instead of argparse

Motivation and Context

#297

Types of Changes

  • Documentation update
  • Bug fix
  • New feature
  • Breaking change1

1The change breaks (or has the potential to break) existing functionality.

Checklist:

If necessary:

  • I have updated the API documentation as part of the package doc-strings.
  • I have created a separate pull request to update the framework documentation on signac-docs and linked it here.
  • I have updated the changelog and added all related issue and pull request numbers for future reference (if applicable). See example below.

Example for a changelog entry: Fix issue with launching rockets to the moon (#101, #212).

@vishav1771
Copy link
Contributor Author

@csadorf @bdice I have implemented find command in this draft. As click does not provide multiple values (arguments) for an option so I am using the workaround. Should I move forward with this? Please review.

@codecov
Copy link

codecov bot commented Mar 20, 2020

Codecov Report

Merging #309 into master will decrease coverage by 2.58%.
The diff coverage is 94.79%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #309      +/-   ##
==========================================
- Coverage   76.13%   73.54%   -2.59%     
==========================================
  Files          43       43              
  Lines        7076     6323     -753     
==========================================
- Hits         5387     4650     -737     
+ Misses       1689     1673      -16     
Impacted Files Coverage Δ
signac/__main__.py 88.02% <94.79%> (+8.02%) ⬆️
signac/common/validate.py 39.13% <0.00%> (-39.14%) ⬇️
signac/common/crypt.py 58.13% <0.00%> (-18.61%) ⬇️
signac/common/config.py 70.37% <0.00%> (-15.75%) ⬇️
signac/common/host.py 35.00% <0.00%> (-11.43%) ⬇️
signac/contrib/utility.py 51.49% <0.00%> (-7.19%) ⬇️
signac/common/configobj/__init__.py 49.16% <0.00%> (-6.19%) ⬇️
signac/sync.py 83.82% <0.00%> (-3.93%) ⬇️
signac/syncutil.py 79.61% <0.00%> (-3.19%) ⬇️
signac/common/configobj/validate.py 49.48% <0.00%> (-1.72%) ⬇️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eb5cd29...1ecdcaf. Read the comment docs.

Copy link
Member

@bdice bdice left a comment

Choose a reason for hiding this comment

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

@vishav1771 This is a good start! My initial comments are not too specific, just wanted to start a conversation. Feel free to write comments on this PR with any questions / difficulties you have, and we can help. Once this PR is approved with just the find command, we should start a long-running feature branch called click and merge PRs into that, until we're ready to fully replace the existing CLI.

@@ -127,7 +127,7 @@ jobs:
- run:
name: run tests
command: |
python -m pytest tests/ -v
python -m pytest tests/test_shell.py -v
Copy link
Member

Choose a reason for hiding this comment

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

Why did you change the tests command here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just want to check the test_find in test_shell first. So changed it for simplicity. Forgot to revert back

return get_project().find_job_ids(index=index, filter=f, doc_filter=df)


def main_project(args):
class MultipleOptionalArgument(click.Option):
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 sure if I understand the purpose of this class. @vishav1771 Can you offer some explanation for why this exists? It seemed to me that "Multi Value Options" or "Multiple Options" would suffice, but I'm not sure if I understand the purpose. https://click.palletsprojects.com/en/7.x/options/#multi-value-options

Copy link
Contributor Author

@vishav1771 vishav1771 Mar 21, 2020

Choose a reason for hiding this comment

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

I tried to match the original command. As click does not support nargs=*, +, ?, so I need to do some monkey patching. This class customize the add_to_parser func and set the arguments according to need.
https://stackoverflow.com/questions/48391777/nargs-equivalent-for-options-in-click

The "Multiple Option" was the other option. But I found a few issues:

  • For --sp: I need to divide this command into two commands --all-sp, --sp where --all-sp will work as --sp(previous api) with no argument and --sp will work as --sp(previous api).
  • For --doc-filter: command --doc-filter a 0 will be written as --doc-filter a --doc-filter 0 which do not make sense. And if I go for tuple of two values then I was not able to create --doc-filter a {"a": {"$exists": true}}
    (Solution to this would be to change --doc-filter key:value)

Kindly provide the feedback.

Copy link
Contributor

Choose a reason for hiding this comment

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

These are the exact issues I anticipated in attempting an exact replication of the current interface and I'm glad that we are having this discussion.

The click framework intentionally does not support options with variadic arguments, because it creates syntactical ambiguities. This makes sense to me and I'd prefer to deprecated these instead of trying to force such behavior into the new framework.

However, there is development on enabling options with default arguments, which should make it much easier to replicate the existing interface.

@vishav1771 Could you make a suggestion as to how we could revise the find interface to not rely on variadic options, but assuming that flag options are possible?

Then we can identify how we can use the kind of work-arounds you introduced here to enable backwards compatible behavior with deprecation warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However, there is development on enabling options with default arguments, which should make it much easier to replicate the existing interface.

Taking this into account, I think --sp should not be changed. But we need to change --doc-filter.
We can implement --doc-filter key:value with setting multiple to True. So --doc-filter a 0 b 1 c would look like --doc-filter a:0 --doc-filter b:1 --doc-filter c. In my opinion, arguments to find should be also be changed (it can be implemented as it is) in a similar way. (find a 0 b 1 c to find a:0 b:1 c)

Copy link
Contributor

Choose a reason for hiding this comment

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

Taking this into account, I think --sp should not be changed.

Are you sure about that? Since --sp is currently accepting an arbitrary number of arguments and I don't see that supported even with the patch.

But we need to change --doc-filter.
We can implement --doc-filter key:value with setting multiple to True. So --doc-filter a 0 b 1 c would look like --doc-filter a:0 --doc-filter b:1 --doc-filter c.

Afaik, it is possible to specify the exact number arguments an option accepts, so while it's true that users would need to use multiple options to create a filter, changing the notation to the key:value syntax would not strictly be necessary, right?

In my opinion, arguments to find should be also be changed (it can be implemented as it is) in a similar way. (find a 0 b 1 c to find a:0 b:1 c)

I think we should be much more careful with considerations concerning changing the API for the arguments. Users are very much used to these now and it would present a huge break in backwards compatibility.

I propose we keep the discussion going and I will also experiment with this a little bit myself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Afaik, it is possible to specify the exact number arguments an option accepts, so while it's true that users would need to use multiple options to create a filter, changing the notation to the key:value syntax would not strictly be necessary, right?

It is not strictly necessary. But i think --doc-filter a:0 is much more intuitive than --doc-filter a --doc-filter 0

I think we should be much more careful with considerations concerning changing the API for the arguments. Users are very much used to these now and it would present a huge break in backwards compatibility.

Makes sense. (I just wanted to keep both the filters similar.)

Copy link
Contributor Author

@vishav1771 vishav1771 Mar 23, 2020

Choose a reason for hiding this comment

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

Are you sure about that? Since --sp is currently accepting an arbitrary number of arguments and I don't see that supported even with the patch.

I think that should be achievable by setting multiple to True(--sp a --sp b). I will try and let you know.

Copy link
Contributor Author

@vishav1771 vishav1771 Mar 24, 2020

Choose a reason for hiding this comment

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

I tried and was able to implement sp option as --sp, --sp=a --sp=b. Is it okay?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's fine!


try:
for job_id in find_with_filter(args):
for job_id in find_with_filter(**kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

You might find types.SimpleNamespace useful in places that would otherwise require porting between args (which is a namespace) and **kwargs (which is a dictionary). https://docs.python.org/3/library/types.html#types.SimpleNamespace

@bdice bdice added the Click Related to refactoring CLI with Click. label Mar 21, 2020
@csadorf
Copy link
Contributor

csadorf commented Mar 30, 2020

@vishav1771 What is the status on this? Were you able to make any progress? I think it would be important to at least finish the implementation of one command even we decide to put the project on hold after that.

@vyasr
Copy link
Contributor

vyasr commented Apr 25, 2020

Hi @vishav1771, I had some discussion on this PR with @csadorf and we decided that some of the decisions that are currently being discussed in this PR would be better addressed in a series of smaller, non-breaking changes to the rest of the repository. Once we've made some of those changes, we can come back to this pull request and hopefully it will be easier to make decisions at that point.

For now, the first pull request we should complete is #188, which was partially completed a while ago but went stale and was never merged in. The idea is to enable a unified querying system where statepoint and document keys can be specified through a single filter (rather than separate filter and doc_filter arguments) by simply prefixing i.e. sp.KEY or doc.KEY. That will make many things more convenient, and will make it easier for us to define a nicer API that unifies the --sp and --doc arguments you were discussing above.

I'm going to assign you to #188 for now, but feel free to comment if you think there's a better path or if you have any other questions or concerns about this.

@csadorf csadorf added the blocked Dependent on something else label Apr 27, 2020
@mikemhenry
Copy link
Collaborator

PR #332 is the PR that is actually blocking this one now, not #188

@csadorf
Copy link
Contributor

csadorf commented Mar 25, 2021

@mikemhenry @vyasr This is no longer blocked as #332 is merged, correct?

@vyasr
Copy link
Contributor

vyasr commented Mar 25, 2021

I don't think this is blocked any longer, but I also don't recall whether we made any decisions regarding this item's priority. Did we decide that it would be worth it, or if the effort required for the change would be too substantial to be worth the payoff? Since IIRC it will require at least minor breaking of the CLI, if we do want to push forward with this I think it would need to be aligned with a major release. I don't particularly want to delay 2.0 for this change without a stronger motivation.

@csadorf
Copy link
Contributor

csadorf commented Mar 26, 2021

I don't think this is blocked any longer, but I also don't recall whether we made any decisions regarding this item's priority. Did we decide that it would be worth it, or if the effort required for the change would be too substantial to be worth the payoff? Since IIRC it will require at least minor breaking of the CLI, if we do want to push forward with this I think it would need to be aligned with a major release. I don't particularly want to delay 2.0 for this change without a stronger motivation.

I think the change introduced with #332 deprecates the ambivalent command line pattern that was holding up this effort. I think it would be worth it to at least briefly evaluate how much effort it would take to complete this. It should not be a blocker for 2.0.

@csadorf csadorf removed the blocked Dependent on something else label Mar 26, 2021
@stale
Copy link

stale bot commented Jun 2, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 2, 2021
@csadorf csadorf added pinned Instructs stale bot to ignore this issue. and removed stale labels Jun 3, 2021
@tcmoore3
Copy link
Member

Hi @vishav1771, have you had a chance to look at this?

@csadorf
Copy link
Contributor

csadorf commented Nov 2, 2021

@vishav1771 I assume that we need to assign somebody else for this.

Any of the @glotzerlab/signac-committers open to taking this on?

@bdice
Copy link
Member

bdice commented Nov 2, 2021

@csadorf I would vote to close this PR. It is stalled and I don’t know of any champions within the developer pool. It can always be done later if we have someone interested!

@csadorf csadorf removed the pinned Instructs stale bot to ignore this issue. label Nov 2, 2021
@csadorf
Copy link
Contributor

csadorf commented Nov 2, 2021

This effort is put on hold for now.

@csadorf csadorf closed this Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Click Related to refactoring CLI with Click.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants