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

hosts.add with an entries list containing the same host twice should fail #54

Open
paulbusse opened this issue Sep 9, 2024 · 2 comments

Comments

@paulbusse
Copy link

Running the following program

from python_hosts import Hosts, HostsEntry

host = HostsEntry(
    entry_type='ipv4',
    address='10.10.10.10',
    names=['samsung', 'samsung.local'],
)
entries = []
entries.append(host)
entries.append(host)

hosts = Hosts('testhosts')
hosts.add(entries, force=True)
hosts.write()

gives this content in testhosts

10.10.10.10	samsung samsung.local
10.10.10.10	samsung samsung.local

I would expect the entry to be present only once.

Thanks for the module. Saved me a lot of time.

@jonhadfield
Copy link
Owner

Thanks for the report. It would make sense to either fail or deal with it like a declaration and do nothing, as there'd be nothing to do in this case. Will investigate.

@jonhadfield
Copy link
Owner

The issue is that you're creating a list of duplicate items and adding that to hosts. If instead you instantiated hosts first and added each item, then you wouldn't get duplicates, i.e.

hosts = Hosts('testhosts')
host = HostsEntry(
    entry_type='ipv4',
    address='10.10.10.10',
    names=['samsung', 'samsung.local'],
)
hosts.add([host])
hosts.add([host])
hosts.write()

The reason this happens is that the code is looking for duplicates being applied to existing hosts, not checking if the new hosts you're adding contain duplicates. I'll think about de-duplicating incoming hosts before adding, but think this really is an edge case.

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