Skip to content

Commit a4f87da

Browse files
committed
Improve documentation
Signed-off-by: Bernát Gábor <[email protected]>
1 parent 20f37d8 commit a4f87da

18 files changed

+217
-185
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ repos:
2828
rev: v1.11.0
2929
hooks:
3030
- id: blacken-docs
31-
additional_dependencies: [ black==20.8b1 ]
31+
additional_dependencies: [ black==21.9b0 ]
3232
- repo: https://github.com/pre-commit/pygrep-hooks
3333
rev: v1.9.0
3434
hooks:

README.md

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,124 +4,10 @@
44
[![Supported Python
55
versions](https://img.shields.io/pypi/pyversions/filelock.svg)](https://pypi.org/project/filelock/)
66
[![Documentation
7-
status](https://readthedocs.org/projects/filelock/badge/?version=latest&style=flat-square)](https://filelock.readthedocs.io/en/latest/?badge=latest)
7+
status](https://readthedocs.org/projects/py-filelock/badge/?version=latest&style=flat-square)](https://py-filelock.readthedocs.io/en/latest/?badge=latest)
88
[![Code style:
99
black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
1010
[![Downloads](https://pepy.tech/badge/filelock/month)](https://pepy.tech/project/filelock/month)
1111
[![check](https://github.com/tox-dev/py-filelock/actions/workflows/check.yml/badge.svg)](https://github.com/tox-dev/py-filelock/actions/workflows/check.yml)
1212

13-
This package contains a single module, which implements a platform independent file lock in Python, which provides a
14-
simple way of inter-process communication:
15-
16-
```Python
17-
from filelock import Timeout, FileLock
18-
19-
lock = FileLock("high_ground.txt.lock")
20-
with lock:
21-
open("high_ground.txt", "a").write("You were the chosen one.")
22-
```
23-
24-
**Don't use** a _FileLock_ to lock the file you want to write to, instead create a separate _.lock_ file as shown above.
25-
26-
![animated example](https://raw.githubusercontent.com/tox-dev/py-filelock/main/example/example.gif)
27-
28-
## Similar libraries
29-
30-
Perhaps you are looking for something like:
31-
32-
- the [pid](https://pypi.python.org/pypi/pid) 3rd party library,
33-
- for Windows the [msvcrt](https://docs.python.org/3/library/msvcrt.html#msvcrt.locking) module in the standar library,
34-
- for UNIX the [fcntl](https://docs.python.org/3/library/fcntl.html#fcntl.flock) module in the standard library.
35-
36-
## Installation
37-
38-
_py-filelock_ is available via PyPi:
39-
40-
```bash
41-
python -m pip install filelock
42-
```
43-
44-
## Documentation
45-
46-
The documentation for the API is available on [readthedocs.org](https://filelock.readthedocs.io/).
47-
48-
### Examples
49-
50-
A _FileLock_ is used to indicate another process of your application that a resource or working directory is currently
51-
used. To do so, create a _FileLock_ first:
52-
53-
```python
54-
from filelock import Timeout, FileLock
55-
56-
file_path = "high_ground.txt"
57-
lock_path = "high_ground.txt.lock"
58-
59-
lock = FileLock(lock_path, timeout=1)
60-
```
61-
62-
The lock object supports multiple ways for acquiring the lock, including the ones used to acquire standard Python thread
63-
locks:
64-
65-
```python
66-
with lock:
67-
open(file_path, "a").write("Hello there!")
68-
69-
lock.acquire()
70-
try:
71-
open(file_path, "a").write("General Kenobi!")
72-
finally:
73-
lock.release()
74-
```
75-
76-
The _acquire()_ method accepts also a _timeout_ parameter. If the lock cannot be acquired within _timeout_ seconds, a
77-
_Timeout_ exception is raised:
78-
79-
```python
80-
try:
81-
with lock.acquire(timeout=10):
82-
open(file_path, "a").write("I have a bad feeling about this.")
83-
except Timeout:
84-
print("Another instance of this application currently holds the lock.")
85-
```
86-
87-
The lock objects are recursive locks, which means that once acquired, they will not block on successive lock requests:
88-
89-
```python
90-
def cite1():
91-
with lock:
92-
open(file_path, "a").write("I hate it when he does that.")
93-
94-
def cite2():
95-
with lock:
96-
open(file_path, "a").write("You don't want to sell me death sticks.")
97-
98-
# The lock is acquired here.
99-
with lock:
100-
cite1()
101-
cite2()
102-
103-
# And released here.
104-
```
105-
106-
All log messages by this library are made using the _DEBUG_ level, under the `filelock` name. On how to control
107-
displaying/hiding that please consult the
108-
[logging documentation of the standard library](https://docs.python.org/3/howto/logging.html).
109-
110-
E.g. to hide these messages you can use `logging.getLogger("filelock").setLevel(logging.INFO)`.
111-
112-
## FileLock vs SoftFileLock
113-
114-
The _FileLock_ is platform dependent while the _SoftFileLock_ is not. Use the _FileLock_ if all instances of your
115-
application are running on the same host and a _SoftFileLock_ otherwise.
116-
117-
The _SoftFileLock_ only watches the existence of the lock file. This makes it ultra portable, but also more prone to
118-
dead locks if the application crashes. You can simply delete the lock file in such cases.
119-
120-
## Contributions
121-
122-
Contributions are always welcome, please make sure they pass all tests before creating a pull request. Never hesitate to
123-
open a new issue, although it may take some time for me to respond.
124-
125-
## License
126-
127-
This package is [public domain](./LICENSE).
13+
For more information checkout the [official documentation](https://py-filelock.readthedocs.io/en/latest/api.html).

docs/conf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from filelock import __version__
66

77
company = "tox-dev"
8-
name = "py-filelock"
8+
name = "filelock"
99
version = ".".join(__version__.split(".")[:2])
1010
release = __version__
1111
copyright = f"2014-{date.today().year}, {company}"
@@ -30,10 +30,12 @@
3030
today_fmt = "%B %d, %Y"
3131

3232
html_theme = "furo"
33+
html_favicon = "logo.svg"
34+
html_logo = "logo.svg"
3335
html_theme_options = {
3436
"navigation_with_keys": True,
3537
}
36-
html_title = "py-filelock"
38+
html_title = name
3739
html_last_updated_fmt = datetime.now().isoformat()
3840

3941
autoclass_content = "class"
File renamed without changes.

docs/index.rst

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,138 @@
1-
py-filelock
2-
===========
1+
filelock
2+
========
3+
4+
This package contains a single module, which implements a platform independent file lock in Python, which provides a
5+
simple way of inter-process communication:
6+
7+
.. code-block:: python
8+
9+
from filelock import Timeout, FileLock
10+
11+
lock = FileLock("high_ground.txt.lock")
12+
with lock:
13+
open("high_ground.txt", "a").write("You were the chosen one.")
14+
15+
**Don't use** a :class:`FileLock <filelock.FileLock>` to lock the file you want to write to, instead create a separate
16+
``.lock`` file as shown above.
17+
18+
.. image:: example.gif
19+
:alt: Example gif
20+
21+
22+
Similar libraries
23+
-----------------
24+
25+
Perhaps you are looking for something like:
26+
27+
- the `pid <https://pypi.python.org/pypi/pid>`_ 3rd party library,
28+
- for Windows the `msvcrt <https://docs.python.org/3/library/msvcrt.html#msvcrt.locking>`_ module in the standard
29+
library,
30+
- for UNIX the `fcntl <https://docs.python.org/3/library/fcntl.html#fcntl.flock>`_ module in the standard library.
31+
32+
Installation
33+
------------
34+
35+
``filelock`` is available via PyPi, so you can pip install it:
36+
37+
.. code-block:: bash
38+
39+
python -m pip install filelock
40+
41+
Tutorial
42+
--------
43+
44+
### Examples
45+
46+
A :class:`FileLock <filelock.FileLock>` is used to indicate another process of your application that a resource or
47+
working directory is currently used. To do so, create a :class:`FileLock <filelock.FileLock>` first:
48+
49+
.. code-block:: python
50+
51+
from filelock import Timeout, FileLock
52+
53+
file_path = "high_ground.txt"
54+
lock_path = "high_ground.txt.lock"
55+
56+
lock = FileLock(lock_path, timeout=1)
57+
58+
The lock object supports multiple ways for acquiring the lock, including the ones used to acquire standard Python thread
59+
locks:
60+
61+
.. code-block:: python
62+
63+
with lock:
64+
open(file_path, "a").write("Hello there!")
65+
66+
lock.acquire()
67+
try:
68+
open(file_path, "a").write("General Kenobi!")
69+
finally:
70+
lock.release()
71+
72+
The :meth:`acquire <filelock.BaseFileLock.acquire>` method accepts also a ``timeout`` parameter. If the lock cannot be
73+
acquired within ``timeout`` seconds, a :class:`Timeout <filelock.Timeout>` exception is raised:
74+
75+
.. code-block:: python
76+
77+
try:
78+
with lock.acquire(timeout=10):
79+
open(file_path, "a").write("I have a bad feeling about this.")
80+
except Timeout:
81+
print("Another instance of this application currently holds the lock.")
82+
83+
The lock objects are recursive locks, which means that once acquired, they will not block on successive lock requests:
84+
85+
.. code-block:: python
86+
87+
def cite1():
88+
with lock:
89+
open(file_path, "a").write("I hate it when he does that.")
90+
91+
92+
def cite2():
93+
with lock:
94+
open(file_path, "a").write("You don't want to sell me death sticks.")
95+
96+
97+
# The lock is acquired here.
98+
with lock:
99+
cite1()
100+
cite2()
101+
# And released here.
102+
103+
104+
Logging
105+
-------
106+
All log messages by this library are made using the ``DEBUG_ level``, under the ``filelock`` name. On how to control
107+
displaying/hiding that please consult the
108+
`logging documentation of the standard library <https://docs.python.org/3/howto/logging.html>`_. E.g. to hide these
109+
messages you can use:
110+
111+
.. code-block:: python
112+
113+
logging.getLogger("filelock").setLevel(logging.INFO)
114+
115+
FileLock vs SoftFileLock
116+
------------------------
117+
118+
The :class:`FileLock <filelock.FileLock>` is platform dependent while the :class:`SoftFileLock <filelock.SoftFileLock>`
119+
is not. Use the :class:`FileLock <filelock.FileLock>` if all instances of your application are running on the same host
120+
and a :class:`SoftFileLock <filelock.SoftFileLock>` otherwise.
121+
122+
The :class:`SoftFileLock <filelock.SoftFileLock>` only watches the existence of the lock file. This makes it ultra
123+
portable, but also more prone to dead locks if the application crashes. You can simply delete the lock file in such
124+
cases.
125+
126+
Contributions and issues
127+
------------------------
128+
129+
Contributions are always welcome, please make sure they pass all tests before creating a pull request. This module is
130+
hosted on `GitHub <https://github.com/tox-dev/py-filelock>`_. If you have any questions or suggestions, don't hesitate
131+
to open a new issue 😊. There's no bad question, just a missed opportunity to learn more.
3132

4133
.. toctree::
134+
:hidden:
5135

136+
self
6137
api
7138
license
8-
9-
GitHub
10-
------
11-
12-
This module is hosted on
13-
`GitHub <https://github.com/tox-dev/py-filelock>`_. If you have any
14-
questions or suggestions, don't hesitate to open a new issue :).

docs/logo.svg

Lines changed: 9 additions & 0 deletions
Loading

example/example.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

example/example_sm.gif

-170 KB
Binary file not shown.

example/pic0.png

-20.2 KB
Binary file not shown.

example/pic1.png

-49 KB
Binary file not shown.

0 commit comments

Comments
 (0)