-
Notifications
You must be signed in to change notification settings - Fork 122
Added Cmd2AttributeWrapper class #985
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
Conversation
dca75fb
to
b418f0f
Compare
Codecov Report
@@ Coverage Diff @@
## master #985 +/- ##
=======================================
Coverage 97.83% 97.83%
=======================================
Files 22 22
Lines 4523 4533 +10
=======================================
+ Hits 4425 4435 +10
Misses 98 98
Continue to review full report at Codecov.
|
cmd2/decorators.py
Outdated
@@ -300,11 +300,16 @@ def cmd_wrapper(*args: Any, **kwargs: Dict[str, Any]) -> Optional[bool]: | |||
else: | |||
# Add statement to Namespace and a getter function for it | |||
setattr(ns, constants.NS_ATTR_STATEMENT, statement) | |||
setattr(ns, 'get_statement', lambda: statement) | |||
setattr(ns, 'get_statement', Cmd2AttributeWrapper(statement)) |
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.
Would Cmd2NamespaceWrapper
be a better name?
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.
It wraps an attribute that cmd2
adds to the Namespace
. Since it's callable, I've considered Cmd2AttributeGetter
.
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.
Yeah I like Cmd2AttributeGetter
better than I do the Wrapper
name
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.
After talking to @anselor, I'm gonna make a change so it's no longer callable. Instead the class will implement __get__()
and __set()
. This will function like a normal Namespace
attribute but will still be a Cmd2AttributeWrapper
type so you can easily filter them out.
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.
Nevermind. __get__()
and __set__()
only work on class attributes, not instance attributes. Since we aren't adding our Cmd2AttributeWrappers
to the Namespace
as class attributes, this won't work. The other approach is to add get()
and set()
methods to Cmd2AttributeWrapper
.
Then a developer would use it this way:
# Get the handler
handler = args.cmd2_handler.get()
handler(args)
# Overwrite the handler
args.cmd2_handler.set(new_handler)
b418f0f
to
484b6cb
Compare
…r()) are now Cmd2AttributeWrapper objects named cmd2_statement and cmd2_handler. This makes it easy to filter out which attributes in an argparse.Namespace were added by cmd2.
484b6cb
to
478ea83
Compare
get_statement()
andget_handler()
) are nowCmd2AttributeWrapper
objects namedcmd2_statement
andcmd2_handler
. This makes it easy to filter out which attributes in anargparse.Namespace
were added bycmd2
.Namespace.__statement__
will be removed incmd2
2.0.0. UseNamespace.get_statement()
going forward.