Skip to content

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

Merged
merged 1 commit into from
Aug 27, 2020
Merged

Added Cmd2AttributeWrapper class #985

merged 1 commit into from
Aug 27, 2020

Conversation

kmvanbrunt
Copy link
Member

@kmvanbrunt kmvanbrunt commented Aug 25, 2020

  • Breaking Changes
    • The functions cmd2 adds to Namespaces (get_statement() and get_handler()) 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.
  • Deprecations
    • Namespace.__statement__ will be removed in cmd2 2.0.0. Use Namespace.get_statement() going forward.

@kmvanbrunt kmvanbrunt requested a review from anselor August 25, 2020 20:14
@kmvanbrunt kmvanbrunt self-assigned this Aug 25, 2020
@kmvanbrunt kmvanbrunt force-pushed the dynamic_value branch 4 times, most recently from dca75fb to b418f0f Compare August 26, 2020 16:41
@kmvanbrunt kmvanbrunt changed the title get_statement() and get_handler() fixes Added Cmd2AttributeWrapper class Aug 26, 2020
@codecov
Copy link

codecov bot commented Aug 26, 2020

Codecov Report

Merging #985 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #985   +/-   ##
=======================================
  Coverage   97.83%   97.83%           
=======================================
  Files          22       22           
  Lines        4523     4533   +10     
=======================================
+ Hits         4425     4435   +10     
  Misses         98       98           
Impacted Files Coverage Δ
cmd2/__init__.py 100.00% <100.00%> (ø)
cmd2/argparse_custom.py 94.62% <100.00%> (+0.12%) ⬆️
cmd2/cmd2.py 97.21% <100.00%> (ø)
cmd2/decorators.py 97.27% <100.00%> (+0.07%) ⬆️

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 97c348c...478ea83. Read the comment docs.

tleonhardt
tleonhardt previously approved these changes Aug 26, 2020
@@ -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))
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member Author

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)

…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.
@kmvanbrunt kmvanbrunt merged commit f4943a9 into master Aug 27, 2020
@kmvanbrunt kmvanbrunt deleted the dynamic_value branch August 27, 2020 17:19
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.

3 participants