-
Notifications
You must be signed in to change notification settings - Fork 44
[WIP] tapify
with subparsers
#140
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
Draft
kddubey
wants to merge
2
commits into
swansonk14:main
Choose a base branch
from
kddubey:tapify-with-subparsers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
from argparse import ArgumentError, ArgumentTypeError | ||
from tap._version import __version__ | ||
from tap.tap import Tap | ||
from tap.tapify import tapify, to_tap_class | ||
from tap.tapify import tapify, tapify_with_subparsers, to_tap_class | ||
|
||
__all__ = [ | ||
"ArgumentError", | ||
"ArgumentTypeError", | ||
"Tap", | ||
"tapify", | ||
"tapify_with_subparsers", | ||
"to_tap_class", | ||
"__version__", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
""" | ||
|
||
import dataclasses | ||
from functools import partial | ||
import inspect | ||
from typing import Any, Callable, Dict, List, Optional, Sequence, Type, TypeVar, Union | ||
|
||
|
@@ -355,3 +356,54 @@ def tapify( | |
|
||
# Initialize the class or run the function with the parsed arguments | ||
return class_or_function(*class_or_function_args, **class_or_function_kwargs) | ||
|
||
|
||
def tapify_with_subparsers(class_: Type): | ||
# Create a Tap class with subparsers defined by the class_'s methods | ||
docstring = _docstring(class_) | ||
param_to_description = {param.arg_name: param.description for param in docstring.params} | ||
args_data = _tap_data(class_, param_to_description, func_kwargs={}).args_data | ||
|
||
subparser_dest = "_tap_subparser_dest" | ||
|
||
class TapWithSubparsers(_tap_class(args_data)): | ||
def configure(self): # TODO: understand why overriding _configure is wrong | ||
self.add_subparsers( | ||
help="sub-command help", # TODO: prolly should be user-inputted instead | ||
required=True, # If not required just use tapify | ||
dest=subparser_dest, # Need to know which subparser (i.e., which method) is being hit by the CLI | ||
) | ||
for method_name in dir(class_): | ||
method = getattr(class_, method_name) | ||
if method_name.startswith("_") or not callable(method): | ||
# TODO: maybe the user can input their own function (method_name: str -> bool) for deciding whether | ||
# or not a method_name should be included as a subparser or not. | ||
continue | ||
subparser_tap = to_tap_class(partial(method, None)) | ||
# TODO: the partial part is a stupid fix for getting rid of self. Need to also handle static and class | ||
# methods | ||
self.add_subparser( | ||
method_name, | ||
subparser_tap, | ||
help=f"{method_name} help", # TODO: think about how to set | ||
description=f"{method_name} description", # TODO: think about how to set | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think that the docstring for that method should work well |
||
) | ||
|
||
# Parse the user's command | ||
cli_args = TapWithSubparsers().parse_args() | ||
|
||
# TODO: think about how to avoid name collisions b/t the init and method args / avoid loading everything into as_dict | ||
|
||
# Create the class_ object | ||
# TODO: maybe figure out how to not do this step so that the input class_ can be a module or any collection of things | ||
# where calling dir on it gives a bunch of functions | ||
args_for_init = {arg_data.name for arg_data in args_data} | ||
# TODO: handle args and kwargs like we did for tapify | ||
init_kwargs = {name: value for name, value in cli_args.as_dict().items() if name in args_for_init} | ||
object_ = class_(**init_kwargs) | ||
|
||
# Call the method | ||
method = getattr(object_, getattr(cli_args, subparser_dest)) | ||
# TODO: handle args and kwargs like we did for tapify | ||
method_kwargs = {name: value for name, value in cli_args.as_dict().items() if name not in args_for_init} | ||
return method(**method_kwargs) # TODO: also return the object? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 add a decorator to mark methods as subparsers then this can all be integrated into the existing
tapify
implementation.--JK
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.
Neat. That sounds like a better interface