Add Windows support for paths containing wildcards "*" and "?"
Img2pdf accepts paths containing wildcards such as *.jpg to efficiently refer to multiple input files that match the wildcard expression. Under POSIX environments the command line shell is expected to perform this expansion, however Windows requires the command line utility itself to expand the wildcard expression.
Ideally this would be performed by argparse as described in this draft PEP: https://mail.python.org/pipermail/python-ideas/2015-August/035244.html
Since argparse doesn't do it, this commit performs expansion directly.
Some implementation notes:
- Wildcard characters "*" and "?" are not valid in Windows filenames
- Code doesn't support bracket wildcards such as [0-3] on Windows since they are valid filename characters
- Due to expansion, the images list collected by argparse may contain sub-lists. Code uses chain.from_iterable to create a flat list.
- Paths that refer to non-existant files raise an error message, while wildcards that match no files are silently ignored.
Let me know if there's any changes you'd like me to make for merging.
-
You are not the first person having suggested this feature. What I'm curious about is proof for your claim "on windows, program is responsible for expanding wildcards such as *.jpg". I'm not a windows user and I understand the the windows built-in shells do not expand wildcards. But where is it written that on windows, wildcards are expected to work and expected to be handled by the program?
-
Good question. You can find that assertion and explanation in the draft PEP: "Yet Windows users generally expect wildcards to work. For example, most built-in commands such as
dir
andtype
accept wildcard arguments, and have since the early days of MS-DOS."Since the shell is not doing expansion, that leaves the application.
One mechanism Windows provides for resolving wildcards is FindFirstFileA: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilea Note that the sample code is using FindFirstFileA to expand a "[target_file]" parameter suggesting handling file paths with wildcards is an intended use.
When exploring solutions, I also tried python's os.walk(...) function figuring it would call into win32, but it does not seem to support wildcards.
-
Mentioned in commit 490c3f12
-
Please register or sign in to post a comment