Background
The `_legalize_stage` function was causing issues with Mypy due to
inconsistent type usage between the `path` and `extension` parameters.
This inconsistency stemmed from the `fragment` parameter influencing the
types of these variables.
Key issues
1. `path` was defined as `str`, while `extension` was `bytes`.
2. Depending on `fragment`, `extension` could be either `str` or `bytes`.
3. `path` was sometimes converted to `bytes` within `_legalize_stage`.
Item.destination` method
- The `fragment` parameter determined the output format:
- `False`: Returned absolute path as bytes (default)
- `True`: Returned path relative to library directory as str
Thus
- Rename `fragment` parameter to `relative_to_libdir` for clarity
- Ensure `Item.destination` returns `bytes` in all cases
- Code expecting strings now converts the output to `str`
- Use only `str` type in `_legalize_stage` and `_legalize_path`
functions
- These functions are no longer dependent on `relative_to_libdir`
## Description
My library is managed using Beets for organization and
[git-annex](https://git-annex.branchable.com/) as storage backend.
Therefore when using this system, while my library files always exists
on my filesystem, some files may be empty (without content). In this
case, when I'm running the `convert` plugin, I don't wants it to process
files which are empty (same apply for any Beets plugin). Hence, I added
a check that the file is readable as a `MediaFile` before doing any
process.
Before this fix, trying to encode an empty file would have lead to an
error while leaving `convert` doing its side-effects **and** `convert`
would also copy empty files to destination for files that doesn't need
to be re-encoded.
In my case, this is empty files, but the problem can be anything else
(depending on the storage backend) and/or corrupted files. Conclusion, I
think **checking that the file is readable is always recommended before
proceeding to heavy operation** like this.
- Fix imports
- Fix pytest issues
- Do not assign lambda as variable
- Use isinstance instead of type to check type
- Rename ambiguously named variables
- Name custom errors with Error suffix
- Remove initial comment around playlist entry condition (which is better
suited for user docs anyway, and stated there already)
- Add explanation above the items_paths playlist contents creation list
comprehension.
Make sure we stay with the beets standard of handling everything internally as
bytes.
- M3UFile.write() method writes in wb mode.
- Playlist contents and EXTM3U header is handled as bytes.
- item.destination() gives us unicode string paths, we tranlate to bytes
using util.bytestring_path().
- Fix test_playlist*write* tests to encode UTF-8 assert strings as bytes using
bytestring_path() before comparision.
- Move the creation of the playlist file to the very end, right after
self._parallel_convert, in the convert plugin's main function.
- In the test code, the destination directory is created when the
conversion happens, thus this fixes test_playlist and doesn't hurt the
feature - The playlist creation can as well be the very last step in
the process.
Use Item.destination method for generation of relative paths to media
files in playlist. The fragment keyword enables returning the path as
unicode instead of bytes, let's keep that in mind.
- Improve --help text
- Use unicode instead of bytes when adding media file paths to the
playlist file.
- The "standard" (?) of m3u8 defines that unicode should ensure support
of special characters in media file names. util.displayable_path() is
used to do the conversion from bytes. We save everything in bytes in
the config since it seemes to be the way this plugin or beets in
general likes to save paths.
- Join dest and playlist in the config reader method already to have it
ready in both methods that require the full path to the playlist file.
The idea in this PR is to converge on Python's `fsdecode` and `fsencode`
for argument manipulation. This seems to match up with the Python
standard library's assumptions: namely, on Windows, we use `fsdecode` to
get back to Unicode strings:
54bbb5e336/Lib/subprocess.py (L561)
So let's start by dropping this utility and going straight for
`fsdecode` here to match.
It looks like the convert option for wma used to be called windows media. We could just remove this alias, but might be good to keep for backwards compatibility.
Rectify a couple of things in that PR, pointed out here:
https://github.com/beetbox/beets/pull/4226#issuecomment-1011499620
- Undo the `pretend` sensitivity in the import path, because it's not
clear how this setting could ever be true.
- Preserve the log message in debug mode, even when quiet.
When the delete_originals was set, beets would print the following, regardless
of the presence of the quiet parameter:
convert: Removing original file /path/to/file.ext
This commit ensures that the log is only printed when quiet is not present.