|
28 | 28 |
|
29 | 29 |
|
30 | 30 | class Transaction:
|
31 |
| - """ |
32 |
| - A filesystem transaction. |
| 31 | + """A filesystem transaction. |
33 | 32 |
|
34 | 33 | Its main motivation is implementing transactional Zarr dataset
|
35 | 34 | modifications, because this does not exist for Zarr yet (2024-01), see
|
36 | 35 | https://github.com/zarr-developers/zarr-python/issues/247.
|
37 | 36 |
|
38 |
| - The ``Transaction`` class is used to observe changes to a given target |
39 |
| - directory *target_dir*. |
| 37 | + The `Transaction` class is used to observe changes to a given target |
| 38 | + directory `target_dir`. |
40 | 39 |
|
41 | 40 | Changes must be explicitly registered using a "rollback callback"
|
42 | 41 | function that is provided as the result of using the
|
43 |
| - transaction instance as a context manager::: |
| 42 | + transaction instance as a context manager: |
44 | 43 |
|
| 44 | + ```python |
45 | 45 | with Transaction(target_dir, temp_dir) as rollback_cb:
|
46 | 46 | # emit rollback actions here
|
| 47 | + ``` |
47 | 48 |
|
48 | 49 | The following actions are supported:
|
49 | 50 |
|
50 |
| - * ``rollback_cb("delete_dir", path)`` if a directory has been created. |
51 |
| - * ``rollback_cb("delete_file", path)`` if a file has been created. |
52 |
| - * ``rollback_cb("replace_file", path, original_data)`` if a directory has |
| 51 | + * `rollback_cb("delete_dir", path)` if a directory has been created. |
| 52 | + * `rollback_cb("delete_file", path)` if a file has been created. |
| 53 | + * `rollback_cb("replace_file", path, original_data)` if a directory has |
53 | 54 | been changed.
|
54 | 55 |
|
55 |
| - Reported paths must be relative to *target_dir*. The empty path ``""`` |
56 |
| - refers to *target_dir* itself. |
| 56 | + Reported paths must be relative to `target_dir`. The empty path `""` |
| 57 | + refers to `target_dir` itself. |
57 | 58 |
|
58 | 59 | When entering the context, a lock file will be created which prevents
|
59 |
| - other transactions to modify *target_dir*. The lock file will be placed |
| 60 | + other transactions to modify `target_dir`. The lock file will be placed |
60 | 61 | next to *target_dir* and its name is the filename of *target_dir* with a
|
61 |
| - ``.lock`` extension. The lock file will be removed on context exit. |
62 |
| -
|
63 |
| - :param target_dir: The target directory that is subject to this |
64 |
| - transaction. All paths emitted to the rollback callback must be |
65 |
| - relative to *target_dir*. The directory may or may not exist yet. |
66 |
| - :param temp_dir: Temporary directory in which a unique subdirectory |
67 |
| - will be created that will be used to collect |
68 |
| - rollback data during the transaction. The directory must exist. |
69 |
| - :param disable_rollback: Disable rollback entirely. |
70 |
| - No rollback data will be written, however a lock file will still |
71 |
| - be created for the duration of the transaction. |
| 62 | + `.lock` extension. The lock file will be removed on context exit. |
| 63 | +
|
| 64 | + Args: |
| 65 | + target_dir: The target directory that is subject to this |
| 66 | + transaction. All paths emitted to the rollback callback must be |
| 67 | + relative to *target_dir*. The directory may or may not exist yet. |
| 68 | + temp_dir: Temporary directory in which a unique subdirectory |
| 69 | + will be created that will be used to collect |
| 70 | + rollback data during the transaction. The directory must exist. |
| 71 | + disable_rollback: Disable rollback entirely. |
| 72 | + No rollback data will be written, however a lock file will still |
| 73 | + be created for the duration of the transaction. |
| 74 | + Raises: |
| 75 | + OSError: If the target is locked or the lock could not be removed. |
| 76 | + TypeError: If the returned callback is not used appropriately. |
| 77 | + ValueError: If instances of this class are not used as a context |
| 78 | + manager, or if the returned callback is not used appropriately, |
| 79 | + and in some other cases. |
72 | 80 | """
|
73 | 81 |
|
74 | 82 | def __init__(
|
@@ -109,7 +117,7 @@ def __enter__(self):
|
109 | 117 |
|
110 | 118 | lock_file = self._lock_file
|
111 | 119 | if lock_file.exists():
|
112 |
| - raise IOError(f"Target is locked: {lock_file.uri}") |
| 120 | + raise OSError(f"Target is locked: {lock_file.uri}") |
113 | 121 | lock_file.write(self._rollback_dir.uri)
|
114 | 122 |
|
115 | 123 | if not self._disable_rollback:
|
@@ -209,5 +217,5 @@ def _add_rollback_action(
|
209 | 217 | def _assert_entered_ctx(self):
|
210 | 218 | if not self._entered_ctx:
|
211 | 219 | raise ValueError(
|
212 |
| - "Transaction instance" " must be used with the 'with' statement" |
| 220 | + "Transaction instance must be used with the 'with' statement" |
213 | 221 | )
|
0 commit comments