Skip to content

Commit d3820c6

Browse files
committed
Improve documentation
1 parent 49c0e2e commit d3820c6

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ python-hosts
44

55

66
This is a python library for managing a hosts file.
7-
It enables you to add and remove entries, or import them from a file or URL.
8-
Utility functions have been streamlined for easier maintenance.
7+
It enables you to add and remove entries, import them from a file or URL and
8+
query existing entries. Utility functions have been streamlined for easier
9+
maintenance.
910
It remains compatible with Python 2.7 as well as modern Python 3 releases.
1011

1112
Documentation
@@ -21,21 +22,30 @@ pip install python-hosts
2122

2223
Example usage
2324
------------
24-
Adding an entry to a hosts file
25+
Create a ``Hosts`` instance and add an entry::
2526

2627
from python_hosts import Hosts, HostsEntry
2728
hosts = Hosts(path='hosts_test')
2829
new_entry = HostsEntry(entry_type='ipv4', address='1.2.3.4', names=['www.example.com', 'example'])
2930
hosts.add([new_entry])
3031
hosts.write()
3132

32-
Importing a list of host entries by URL
33+
Import entries from a URL or file::
3334

34-
from python_hosts import Hosts
35-
hosts = Hosts(path='hosts_test')
36-
hosts.import_url(url='https://gist.githubusercontent.com/jonhadfield/5b6cdf853ef629f9b187345d89157280/raw/ddfa4a069fb12bf3c1f285249d44922aeb75db3f/hosts')
35+
hosts.import_url('https://example.com/hosts')
36+
hosts.import_file('extra_hosts')
3737
hosts.write()
3838

39+
Remove or query entries::
40+
41+
hosts.remove_all_matching(name='example')
42+
hosts.exists(address='1.2.3.4')
43+
44+
Entries can also be merged with existing ones::
45+
46+
new_entry = HostsEntry(entry_type='ipv4', address='1.2.3.4', names=['alias'])
47+
hosts.add([new_entry], merge_names=True)
48+
3949
CLI
4050
---
4151
A command line client using python-hosts can be found here: https://github.com/jonhadfield/hostman

docs/usage.rst

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
Usage
22
=====
3-
**Create an instance of a hosts file**::
43

5-
from python_hosts import Hosts, HostsEntry
6-
my_hosts = Hosts()
4+
Basic operations
5+
----------------
76

8-
**Add an entry**::
7+
Create an instance of a hosts file (the default path for the current platform
8+
is used when ``path`` is not supplied)::
99

10-
new_entry = HostsEntry(entry_type='ipv4', address='1.2.3.4', names=['example.com', 'example'])
11-
my_hosts.add([new_entry])
10+
from python_hosts import Hosts, HostsEntry
11+
my_hosts = Hosts()
1212

13-
**Remove an entry/entries matching an address**::
13+
Add an entry::
1414

15-
my_hosts.remove_all_matching(address='1.2.3.4')
15+
new_entry = HostsEntry(entry_type='ipv4', address='1.2.3.4', names=['example.com', 'example'])
16+
my_hosts.add([new_entry])
1617

17-
**Remove an entry/entries matching an address**::
18+
Remove entries by address or name::
1819

19-
my_hosts.remove_all_matching(name='example.com')
20+
my_hosts.remove_all_matching(address='1.2.3.4')
21+
my_hosts.remove_all_matching(name='example.com')
2022

21-
**Write entries**::
23+
Write changes back to disk::
2224

23-
my_hosts.write()
25+
my_hosts.write()
26+
27+
Additional features
28+
-------------------
29+
30+
Import entries from a file or URL::
31+
32+
my_hosts.import_file('extra_hosts')
33+
my_hosts.import_url('https://example.com/hosts')
34+
35+
Check if a host entry exists::
36+
37+
my_hosts.exists(address='1.2.3.4')
38+
39+
Merge names with an existing entry while keeping the same address::
40+
41+
new_entry = HostsEntry(entry_type='ipv4', address='1.2.3.4', names=['alias'])
42+
my_hosts.add([new_entry], merge_names=True)

0 commit comments

Comments
 (0)