|
| 1 | +.. _adr-004: |
| 2 | + |
| 3 | +[ADR-004] Supporting Single Path Sequences |
| 4 | +========================================== |
| 5 | + |
| 6 | +:bdg-danger:`Rejected` |
| 7 | + |
| 8 | +Context and Problem Statement |
| 9 | +----------------------------- |
| 10 | + |
| 11 | +This proposal is to make the ranges in a sequence optional, |
| 12 | +such that a sequence with no ranges (i.e. a single file) is still considered a valid sequence. |
| 13 | + |
| 14 | +Use cases for this include: |
| 15 | + |
| 16 | +* Retrieving a sequence string from an unknown source, |
| 17 | + and not knowing whether it will be a single file or a sequence. |
| 18 | + For example, a user may have a sequence string stored in a database, and want to |
| 19 | + use pathseq to loop over the files in that sequence. If the sequence string is for |
| 20 | + a single file, they would still want to be able to use pathseq to loop over it. |
| 21 | + |
| 22 | + Where this type of pattern has been seen before, |
| 23 | + users have typically run the equivalent of ``.with_existing_files`` |
| 24 | + immediately after creating the sequence. |
| 25 | + |
| 26 | +Currently, a sequence with only empty ranges is considered to be empty. |
| 27 | +A single path sequence would have no ranges, and would be considered as having one file. |
| 28 | +These two definitions are somewhat in conflict, |
| 29 | +and so introducing single path sequences would erode the concept of a sequence. |
| 30 | + |
| 31 | +PathSeq defines a "stem" slightly differently to pathlib. |
| 32 | +In pathlib, the stem of a path is the final path component without its suffix. |
| 33 | +In PathSeq, the stem of a path is the final path component without the ranges and any suffixes. |
| 34 | +This difference is achievable because the ranges are an additional component |
| 35 | +that creates a clear separation between the stem from the suffixes. |
| 36 | +In single path sequences, there is no clear separation between the stem and suffixes, |
| 37 | +hence why pathlib behaves the way it does. |
| 38 | +pathlib puts the burden on users to parse the stem and suffixes themselves, |
| 39 | +and PathSeq would ideally do the same, |
| 40 | +else risk users reporting unintuitive/inconsistent parsing of suffixes |
| 41 | +(e.g. "file.tar.gz" having a stem of "file.tar" and suffixes of ".gz" |
| 42 | +instead of a stem of "file" and suffixes of ".tar.gz"). |
| 43 | + |
| 44 | +.. note:: |
| 45 | + |
| 46 | + This is already an issue for loose path sequences |
| 47 | + where the ranges exist at the start or end of the sequence string, |
| 48 | + and therefore there is no separation between the stem and suffixes. |
| 49 | + The loose format already warns users that ambiguity exists throughout |
| 50 | + the API of ``LoosePathSequence``, |
| 51 | + so the effect on loose path sequences is not considered significant. |
| 52 | + |
| 53 | +Supporting single path sequences does not significantly complicate the implementation. |
| 54 | +Wherever we support sequences of an unknown number of ranges |
| 55 | +we already support sequences with no ranges. |
| 56 | + |
| 57 | + |
| 58 | +Considered Options |
| 59 | +------------------ |
| 60 | + |
| 61 | +* Change the signature of ``.with_existing_files`` from ``PathSequence`` to ``PathSequence | None``. |
| 62 | + |
| 63 | + Supporting single path sequences would complicate the API in the following ways: |
| 64 | + |
| 65 | + * Users would have to check the type of the return value before using it. |
| 66 | + This applies even for those users that are always using a sequence with ranges. |
| 67 | + Essentially, users end up needing to check whether the sequence has any ranges |
| 68 | + or not before using ``.with_existing_files``. |
| 69 | + So users may as well check this upon creation of the sequence, |
| 70 | + and not have to worry about it for the rest of the sequence's lifetime. |
| 71 | + |
| 72 | + * Proper use of ``.with_existing_files`` can be type checked. |
| 73 | + |
| 74 | + * For users that aren't using type checking, |
| 75 | + improper use of ``.with_existing_files`` could go unnoticed until |
| 76 | + it is called on a single path sequence for which the file does not exist. |
| 77 | + |
| 78 | + * The common use case would be written as: |
| 79 | + |
| 80 | + .. code-block:: python |
| 81 | +
|
| 82 | + def do_something_with_sequence(seq: str): |
| 83 | + files: Iterable[Path] = PathSequence(seq).with_existing_files() or [Path(seq)] |
| 84 | + for file in files: |
| 85 | + # do something with the file |
| 86 | + ... |
| 87 | +
|
| 88 | +* ``.with_existing_files`` will raise an error if it is called on a single file sequence, |
| 89 | + for which the file does not exist. |
| 90 | + |
| 91 | + * Proper use of ``.with_existing_files`` cannot be type checked. |
| 92 | + |
| 93 | + * For users that aren't using type checking, |
| 94 | + improper use of ``.with_existing_files`` could go unnoticed until |
| 95 | + it is called on a single path sequence for which the file does not exist. |
| 96 | + |
| 97 | + * The common use case would be written as: |
| 98 | + |
| 99 | + .. code-block:: python |
| 100 | +
|
| 101 | + def do_something_with_sequence(seq: str): |
| 102 | + files: Iterable[Path] |
| 103 | + try: |
| 104 | + files = PathSequence(seq).with_existing_files() |
| 105 | + except FileNotFoundError: |
| 106 | + files = [Path(seq)] |
| 107 | +
|
| 108 | + for file in files: |
| 109 | + # do something with the file |
| 110 | + ... |
| 111 | +
|
| 112 | +* We will not support single path sequences. |
| 113 | + and instead raise an error if a PathSequence is constructed with a single file sequence. |
| 114 | + |
| 115 | + * Users will not have to worry about whether ``.with_existing_files`` can be used safely. |
| 116 | + Checking is done upon creation of the sequence. |
| 117 | + |
| 118 | + * The methods that construct an instance of ``BasePurePathSequence`` will |
| 119 | + need to raise an error if the sequence string is for a single file. |
| 120 | + Users already need to be aware of a ``ParseError`` being raised in these methods, |
| 121 | + so this is not a significant change to the API. |
| 122 | + |
| 123 | + * The common use case would be written as: |
| 124 | + |
| 125 | + .. code-block:: python |
| 126 | +
|
| 127 | + def do_something_with_sequence(seq: str): |
| 128 | + files: Iterable[Path] |
| 129 | + try: |
| 130 | + files = PathSequence(seq).with_existing_files() |
| 131 | + except NotASequenceError: |
| 132 | + files = [Path(seq)] |
| 133 | +
|
| 134 | + for file in files: |
| 135 | + # do something with the file |
| 136 | + ... |
| 137 | +
|
| 138 | +
|
| 139 | +Decision Outcome |
| 140 | +---------------- |
| 141 | + |
| 142 | +We will not support single path sequences. |
| 143 | +A single file and a path sequence occasionally needs to be treated in differently, |
| 144 | +and it's best for users to be aware of this distinction when they create the sequence. |
0 commit comments