facelessuser / wcmatch

Wilcard File Name matching library
https://facelessuser.github.io/wcmatch/
MIT License
131 stars 13 forks source link

Expose `PATHNAME` flag to `fnmatch`? #204

Closed yedpodtrzitko closed 1 year ago

yedpodtrzitko commented 1 year ago

Hi, I'd like to have fnmatch behaving similar way as glob()/wcmatch(), where * would match only one level of directories:

fn = fnmatch('a/b/c/z.txt', 'a/*/z.txt')

fn.match('a/x/z.txt') # True
fn.match('a/b/c/z.txt') # True (I'm expecting False here)

this would work as expected:

fn = fnmatch('a/b/c/z.txt', 'a/*/z.txt', flags=_wcparse.PATHNAME)

however the flag PATHNAME is not allowed in fnmatch(), and it gets stripped via _flag_transform().

Would you accept PR exposing the flag there? Or is there any way how to achieve this behaviour currently? Thanks.

facelessuser commented 1 year ago

@yedpodtrzitko Sounds like you want to use globmatch instead of fnmatch. fnmatch does file name matching, not path matching. globmatch matches file glob paths.

facelessuser commented 1 year ago

For example:

>>> from wcmatch import glob
>>> glob.globmatch('a/b/c/z.txt', 'a/**/z.txt', flags=glob.GLOBSTAR)
True
yedpodtrzitko commented 1 year ago

Thanks for your comment. Unfortunately I dont work directly with a filesystem, but with a list of paths returned from API representing filesystem. And I need to filter this list using the glob-like patterns, but since it's not filesystem the only tool I can use is fnmatch.

facelessuser commented 1 year ago

Please read the section I linked. The documentation states:

By default, globmatch and globfilter do not operate on the file system. This is to allow you to process paths from any source, even paths that are not on your current system.

You can configure it to use Windows or Unix patch logic, depending on what you need.

facelessuser commented 1 year ago

For clarification, if fnmatch were to use PATHNAME, it would operate exactly like globmatch, so what you want is globmatch.

yedpodtrzitko commented 1 year ago

Ah ok, that does the trick. Thanks