jonhadfield / python-hosts

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

No support for adding comments #20

Closed justintime4tea closed 4 years ago

justintime4tea commented 5 years ago

No matter what permutation I try of args I cannot create a HostsEntry that is a comment nor does comment as an arg do anything significant.

I'll continue to test and if possible make a PR that solves the issue. I would have thought entry_type=comment with comment arg with value would have created a comment entry but apparently not?

justintime4tea commented 5 years ago

You can "hack" it by doing the following:

hosts_file = Hosts()
comment = HostsEntry(entry_type='comment', comment='# Some comment\n')
hosts_file.entries.append(comment)
hosts_file.write()

However you shouldn't need to append directly to the entries property of the Hosts object. The Hosts#add method does not support a "comment only" entry which I had expected given the choice of entry_type and comment args.

jonhadfield commented 5 years ago

Thanks for raising. Not sure why that use case isn't supported. Will take a look.

jonhadfield commented 5 years ago

@justintime4tea I've just pushed changes to devel branch that should now enable you to add comments:

from python_hosts.hosts import Hosts, HostsEntry
hosts = Hosts(path='hosts_test')
new_entry = HostsEntry(entry_type='comment', comment='# example comment')
hosts.add([new_entry])
hosts.write()

Please check and let me know if you have any issues.

justintime4tea commented 5 years ago

Haven't had a chance to take a look yet but will probably by end of weekend.

Would like to submit a PR for doing wildcard "remove_all_matching". We have a use case where we dynamically update the hosts file but our source doesn't contain devices which are no longer online and hence we no longer have their full name or IP anymore but we do know the first 3 octets of their IP addresses.

Would like to do "remove_by_regex" or maybe more simply "remove_all_like" and split the string on a wild card identifier like the *. Would that be well received or should I keep that as a feature on my own branch?

jonhadfield commented 5 years ago

Thanks.

First thought on remove_by_regex is that it might be better to do remove_all_matching_regex, duplicating the existing function (maybe allowing for comment matching too?) and including the regex matching. Not a big fan of longer function names or duplication, yet I think it keeps it simple and obvious. Even better may be a remove_all_matching_cidr, but that may be overkill in terms of complexity.

ghost commented 5 years ago

What I really wanted was to be able to include a comment on a address entry (valid in hosts) e.g. to get something like 192.168.0.50 OnePlus5T # here is a comment or specifically for my use case 192.168.0.50 OnePlus5T # xx:xx:xx:xx:xx:xx

I want to save the mac addr of the devices on my home network

In fact I'd love the remove_all_matching to support passing a mac addr then I can update an entry when the ip address changes (damn my tp-link router for only supporting 32 fixed ip addresses)

jonhadfield commented 5 years ago

I'll check what's involved in adding both inline comments and the ability to remove by matching (sub)string.

ghost commented 5 years ago

Cheers Jon, I should have had a go myself and put in a merge request, but was just on way to bed and saw this perfect thread.

github-actions[bot] commented 4 years ago

Stale issue message

lucasfcnunes commented 3 years ago

Should I pull request this? https://github.com/lucasfcnunes/python-hosts/commit/92f3011849c313c8a2765b3f451144f7096599ff -- EDIT opened the PR now here: https://github.com/jonhadfield/python-hosts/pull/36