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

Store parent object in attributes. #178

Open
Skadabucci opened this issue Apr 16, 2018 · 0 comments
Open

Store parent object in attributes. #178

Skadabucci opened this issue Apr 16, 2018 · 0 comments

Comments

@Skadabucci
Copy link

Hello,
I think a valuable feature would be the ability to access a parent object given any yang path object.

Example:

Given a room object

>>> room
<pyangbind.lib.yangtypes.YANGBaseClass object at 0x21eb430>
>>> room._yang_path()
u"/domains/domain[name='foo']/sites/site[name='bar']/rooms/room[name='scotty']"

I would like the ability to access the site object that has the key named bar like so:

>>> room.parent().parent().name
u'bar'

Current Work Around

We have a class that wraps our graph, within that we have the following method:

    def get_parent(self, object, generations=1):
        # Split up the yang path and remove empty strings
        path = [x for x in object._yang_path().split('/') if x]
        # Transform parts of the path from: domain[name='foo'] to: domain['foo']
        path = [re.sub(r"(.+?)\[.+='(.+)'\]", "\g<1>['\g<2>']", x) for x in path]
        # Remove the number of generations from the path
        path = path[:generations * -1]
        # create the string and evaluate it
        return eval('self.graph.{}'.format('.'.join(path)))

This works, but it would be super helpful having this baked into pyyangbind.

>>> graph_wrapper.get_parent(room, generations=2).name
u'bar'

In this workaround self holds an attribute graph that stores the value of my actual graph generated from pyyangbind code. Note that I have not tested this with objects that have multiple keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants