Package: globre
Converts a glob-matching pattern to a regular expression, using Apache
Cocoon style rules (with some extensions).TL;DRInstall:$pipinstallglobreUse:importglobrenames=['/path/to/file.txt','/path/to/config.ini','/path/to/subdir/base.ini',]txt_names=[namefornameinnamesifglobre.match('/path/to/*.txt',name)]asserttxt_names==['/path/to/file.txt']ini_names=[namefornameinnamesifglobre.match('/path/to/*.ini',name)]assertini_names==['/path/to/config.ini']all_ini_names=[namefornameinnamesifglobre.match('/path/to/**.ini',name)]assertall_ini_names==['/path/to/config.ini','/path/to/subdir/base.ini']DetailsThis package basically allows using unix shell-like filename globbing
to be used to match a string in a Python program. The glob matching
allows most characters to match themselves, with the following
sequences having special meanings:SequenceMeaning?Matches any single character except the slash
(‘/’) character.*Matches zero or more charactersexcludingthe slash
(‘/’) character, e.g./etc/*.confwhich willnotmatch “/etc/foo/bar.conf”.**Matches zero or more charactersincludingthe slash
(‘/’) character, e.g./lib/**.sowhichwillmatch “/lib/foo/bar.so”.\Escape character used to precede any of the other special
characters (in order to match them literally), e.g.foo\?will match “foo” followed by a literal question mark.[...]Matches any character in the specified regex-style character range,
e.g.foo[0-9A-F].conf.{...}Inlines a regex expression, e.g.foo-{\\D{2,4\}}.txtwhich
will match “foo-bar.txt” but not “foo-012.txt”.Theglobrepackage exports the following functions:globre.match(pattern, string, sep=None, flags=0):Tests whether or not the globpatternmatches thestring. If it
does, are.MatchObjectis returned, otherwiseNone. Thestringmust be matched in its entirety. Seeglobre.compilefor details on
thesepandflagsparameters. Example:globre.match('/etc/**.conf','/etc/rsyslog.conf')# => truthyglobre.search(pattern, string, sep=None, flags=0):Similar toglobre.match, but the pattern does not need to match
the entire string. Example:globre.search('lib/**.so','/var/lib/python/readline.so.6.2')# => truthyglobre.compile(pattern, sep=None, flags=0, split_prefix=False):Compiles the specifiedpatterninto a matching object that has the
same API as the regular expression object returned byre.compile.Thesepparameter specifies the hierarchical path component
separator to use. By default, it uses the unix-style forward-slash
separator ("/"), but can be overriden to be a sequence of
alternative valid hierarchical path component separator characters.
Note that althoughsepcouldbe set to both forward- and back-
slashes (i.e."/\\") to, theoretically, support either unix- and
windows-style path components, this has the significant flaw that
thenbothcharacters can be used within the same path as
separators.Theflagsbit mask can contain all the standardreflags, in
addition to theglobre.EXACTflag. If EXACT is set, then the
returned regex will include the equivalent of a leading ‘^’ and
trailing ‘$’, meaning that the regex must match the entire string,
from beginning to end.Ifsplit_prefixis truthy, the return value becomes a tuple with
the first element set to any initial non-wildcarded string found in
the pattern. The second element remains the regex object as before.
For example, the patternfoo/**.iniwould result in a tuple
equivalent to('foo/',re.compile('foo/.*\\.ini')).Example:prefix,expr=globre.compile('/path/to**.ini',split_prefix=True)# prefix => '/path/to'names=['/path/to/file.txt','/path/to/config.ini','/path/to/subdir/otherfile.txt','/path/to/subdir/base.ini',]fornameinnames:ifnotexpr.match(name):# ignore the two ".txt" filescontinue# and do something with:#   - /path/to/config.ini#   - /path/to/subdir/base.iniWhat About theglobModuleThis package is different from the standard Pythonglobmodule in
the following critical ways:Theglobmodule operates on the actual filesystem;globrecan be
used to match both files on the filesystem as well as any other
sources of strings to match.Theglobmodule does not provide the**“descending” matcher.Theglobmodule does not provide the{...}regular expression
inlining feature.Theglobmodule does not provide an alternate hierarchy separator
beyond/or\\.