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

make it easy to populate NetDevices from arbitrary sources #55

Merged
merged 8 commits into from Jan 30, 2013

Conversation

Projects
None yet
1 participant
@jathanism
Member

jathanism commented Jan 29, 2013

I talked to @jathanism in IRC. I think it would be awesome to allow someone to write their own class, example:

class MagicNetDevices(object):
    def populate(self):
        # parse data from source
        # create list of NetDevice objects
        return netdevices

This could then be imported and the populate method ran to create the list of net devices used by NetDevices. My current use case is to hit a HTTP resource that returns JSON, map the data to what netdevice expects, then return the list of netdevices.

@jathanism

This comment has been minimized.

Member

jathanism commented Dec 3, 2012

This would be a simple change to the NetDevices class to have populate() defined within, then it becomes easy to override the way population occurs and I like the idea. Currently _populate() is abstracted from within NetDevices to prevent "accidental" repopulation of NetDevice objects.

I don't think that decision makes sense anymore really, especially in this context. The only people that would really be calling populate in the first place are developers.

@jathanism

This comment has been minimized.

Member

jathanism commented Jan 11, 2013

Hmm... Something came to mind in that the NetDevices class is imported and instantiated through the trigger code. If you're working with a subclass it's not going to behave how you'd expect.

The change to populate() is simple enough, but it seems like we might need to utilize a metaclass so that NetDevices will know about its subclasses. Furthermore it might make since to limit it to a single subclass so that things don't start getting confusing. The end goal would be that whenever NetDevices is imported, the correct class object is returned from the module.

The only other thing I can think of short of that is to monkey patch trigger.netdevices.NetDevices with your subclassed class object after it has been defined, and that makes me feel icky. Something like:

from trigger import netdevices
class MyND(netdevices.NetDevices):
    def populate(self, *args, **kwargs):
        # foo

netdevices.NetDevices = MyND
nd = netdevices.NetDevices()
@jathanism

This comment has been minimized.

Member

jathanism commented Jan 23, 2013

On second thought, going to implement the chain of responsibility pattern kind of like Django's template loaders.

jathanism added some commits Jan 24, 2013

First pass at NetDevices metadata loader
- Not bolted into NetDevices class yet, so don't even try
- trigger.netdevices is now a package
- trigger.netdevices.loader provides BaseLoade
- default loaders are in trigger.netdevices.loaders (currently only
  filesystem loaders provided by default for JSON, XML, Sqlite,
  Rancid, and *new*: CSV!)
- New setting: NETDEVICES_LOADERS to define a list of custom loader
  classes to try in turn. The first one to return data wins.
- Still not sure how to specify the path to a metadata "source" within
  the settings file. Will perhaps try to default to ``/etc/trigger`` and
  load using filename ''netdevices'' with a variable suffix based on the
  Loader type? Or maybe just whatever is defined within NETDEVICES_FILE
  or NETDEVICES_SOURCE?
- Update global settings and sample trigger_settings.py to use NETDEV…
…ICES_SOURCE, remove NETDEVICES_FORMAT, assign NETDEVICES_FILE to NETDEVICES_SOURCE
- Remove prints from Rancid and CSV loaders
- Don't pre-build loader objects because they are only called once at startup
+ Cleanup in netdevices
  - Remove SUPPORTED_FORMATS
  - Remove deprecated _parse_* functions
  - Remove parser imports
  - Update _munge_source_data with new arg signature
  - Update _populate and NetDevices._actual constructor w/ new args
  - Replace NETDEVICES_FILE with NETDEVICES_SOURCE

jathanism added a commit that referenced this pull request Jan 30, 2013

Merge pull request #55 from jathanism/issue-55
make it easy to populate NetDevices from arbitrary sources

@jathanism jathanism merged commit c234043 into trigger:develop Jan 30, 2013

@jathanism jathanism deleted the jathanism:issue-55 branch Jan 30, 2013

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