Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Add args and kwargs support to method execution #69

Merged
merged 2 commits into from
Jul 3, 2017

Conversation

bewing
Copy link
Member

@bewing bewing commented Jun 20, 2017

Noticed that there wasn't a way to run a method that took no arguments. Updated parse.py to support *args, **kwargs format.

Further testing probably required.

@dbarrosop
Copy link
Member

Looks sane and useful. Will run tomorrow against some devices to make sure nothing broke.

@bewing
Copy link
Member Author

bewing commented Jun 20, 2017

Instead of ignoring malformed yaml, should we be raising parser errors if one tries to pass in a list to kwargs, or a dictionary to args?

for p in m["method"].split("."):
attr = getattr(attr, p)
r = attr(**m["args"])
if isinstance(m.get("args", None), list):
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we default to an empty list/dict?

Copy link
Member Author

Choose a reason for hiding this comment

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

defaults are set in lines 59/60 -- we just check if m has a valid dict/list to overwrite the default

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I see... I suggest doing this instead:

kwargs = m.get("kwargs", {})
if not isinstance(kwargs, dict):
    raise TypeError("kwargs has to be a dict")

Otherwise it seems like you are just ignoring invalid arguments without providing any feedback to the user.

@@ -183,13 +183,15 @@ the device. For example::
parser: XMLParser
execute:
- method: _rpc
args:
args: []
Copy link
Member

@dbarrosop dbarrosop Jun 21, 2017

Choose a reason for hiding this comment

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

I don't think we need args. kwargs is probably enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

Both don't need to be specified in the YAML, but I think it might be wise to have the support in the future for args in the backend.

@dbarrosop
Copy link
Member

Sorry, got sidetracked, will try to test later this week.

if not isinstance(args, list):
raise TypeError("args must be type list, not type {}".format(type(args)))
kwargs = m.get("kwargs", {})
if not isinstance(args, dict):
Copy link
Member

Choose a reason for hiding this comment

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

args should be kwargs but don't worry, I will fix it and merge via #70

Copy link
Member Author

Choose a reason for hiding this comment

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

This is what I get for copy/paste

@dbarrosop dbarrosop merged commit e1b5e8c into napalm-automation:develop Jul 3, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants