jonhadfield / python-hosts

a hosts file manager library written in python
MIT License
125 stars 33 forks source link

'HostsEntry' object is not iterable #16

Closed blckjack closed 6 years ago

blckjack commented 6 years ago

Hey there, thanks for this lib! I have an issue, but not sure, about expected behavior of the code. So I have 2 lists:

addresses = ['127.0.0.1', '127.0.1.1']
names = ['google.com', 'microsoft.com']

And want to run my tests in all possible variations:

for test in tests:
    for address in addresses:
        hosts.add(HostsEntry(entry_type='ipv4', address=address, names=names))
        hosts.write()
        for name in names:
            cmd = ['ruby', test, name]
            run = Popen(cmd, stdout=PIPE)

unfortunatelly exception appears:

 File "/usr/local/lib/python2.7/dist-packages/python_hosts/hosts.py", line 354, in add for entry in entries:
TypeError: 'HostsEntry' object is not iterable

probably, I missed something but, anyway, looking forward your assist. thx, ^_^

jonhadfield commented 6 years ago

Hi @blckjack, unfortunately the API docs aren't displaying properly. I'll try and fix them soon. In the meantime, the add method takes a list of entries:

def add(self, entries=None, force=False, allow_address_duplication=False):
        """
        Add instances of HostsEntry to the instance of Hosts.
        :param entries: A list of instances of HostsEntry
        :param force: Remove matching before adding
        :param allow_address_duplication: Allow using multiple entries for same address
        :return: The counts of successes and failures
        """

To add a single HostsEntry, just enclose it in square brackets, so: hosts.add(HostsEntry(entry_type='ipv4', address=address, names=names)) becomes: hosts.add([HostsEntry(entry_type='ipv4', address=address, names=names)])

jonhadfield commented 6 years ago

Docs now fixed: http://python-hosts.readthedocs.io/en/latest/

Detail on Hosts and HostsEntry are here: http://python-hosts.readthedocs.io/en/latest/python_hosts.hosts.html