Quoth the fnmatch
docs:
Note that the filename separator (
'/'
on Unix) is not special to this module. See moduleglob
for pathname expansion (glob
usesfilter()
to match pathname segments).
So fnmatch
operates only on filenames. What if we want to translate, match or filter on paths? The table below shows the situation:
arg: filename | arg: path | |
---|---|---|
translate | fnmatch.translate() |
not supported! |
match | fnmatch.fnmatch() fnmatch.fnmatchcase() |
pathlib.PurePath.match() |
filter | fnmatch.filter() |
not supported! |
glob | N/A | pathlib.Path.glob() glob.glob() |
GH-72904 requests that we fill in that top right “not supported” cell.
I propose we add a glob.translate()
function that converts a path with shell-style wildcards to a regular expression. I have an implementation available in GH-106703, which also adjusts pathlib to call the new function for a tidy speedup.
Thoughts? Qs from my side:
- Does this seem sensible?
- Should the function support a recursive argument, like
glob()
? Should we match its default (false)? - Should the function support an include_hidden argument, like
glob()
? Should we match its default (false)? - How worried should I be about exponential execution time? IIUC the fix for this in
fnmatch.translate()
won’t carry over toglob.translate()
, because it relies on all the variable-width parts matching anything. (I may be totally wrong here.) - Am I alright to copy-paste parts of the
fnmatch.translate()
implementation, particularly[seq]
handling, which is common to both versions? Or should I look adding some sort offnmatch.Translator
class that can be subclassed inglob
? Or something else?
Ta!