Skip to content

File Provider #388

@bojika

Description

@bojika

тут

If you create an object:

devs = Devices(
    devices=[
        Device(dev=DeviceStorage(fqdn="1.1.1.1", vendor="cisco")),
        Device(dev=DeviceStorage(fqdn="1.1.1.2", vendor="juniper")),
    ]
)

It won't be created, and will throw an exception in post_init:

raise Exception("unable to parse %s as Device: %s" % (dev, e))
Exception: unable to parse Device(dev=DeviceStorage(fqdn='1.1.1.1', vendor='cisco')) as Device: __main__.DeviceStorage() argument after ** must be a mapping, not Device

If you fix this and dump this object to yaml, you'll get a file like this:

devices:
- dev: 
    fqdn: 1.1.1.1 
    vendor: cisco
- dev: 
    fqdn: 1.1.1.2 
    vendor: juniper

So it seems to be the correct format for keeping device information in a file.

If you try to restore devs from this file, you'll get another exception in post_init:

raise Exception("unable to parse %s as Device: %s" % (dev, e))
Exception: unable to parse {'dev': {'fqdn': '1.1.1.1', 'vendor': 'cisco'}} as Device: DeviceStorage.__init__() got an unexpected keyword argument 'dev'

It seems that Devices should look something like this:

@dataclass
class Devices:
    devices: list[Device | dict[str, dict[str, Any]]]

    def __post_init__(self):
        if isinstance(self.devices, list):
            devices = []
            for dev in self.devices:
                if isinstance(dev, Device):
                    devices.append(dev)
                else:
                    try:
                        devices.append(Device(dev=DeviceStorage(**dev["dev"])))
                    except Exception as e:
                        raise Exception("unable to parse %s as Device: %s" % (dev, e))
            self.devices = devices

Should I create a PR?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions