-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPKG-INFO
159 lines (118 loc) · 6.31 KB
/
PKG-INFO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Metadata-Version: 1.1
Name: globre
Version: 0.1.2-zato.1
Summary: A glob matching library, providing an interface similar to the "re" module.
Home-page: http://github.com/metagriffin/globre
Author: metagriffin
Author-email: mg.pypi@uberdev.org
License: LGPLv3+
Description: ==========================
Glob-Like Pattern Matching
==========================
Converts a glob-matching pattern to a regular expression, using Apache
Cocoon style rules (with some extensions).
TL;DR
=====
Install:
.. code-block:: bash
$ pip install globre
Use:
.. code-block:: python
import globre
names = [
'/path/to/file.txt',
'/path/to/config.ini',
'/path/to/subdir/base.ini',
]
txt_names = [name for name in names if globre.match('/path/to/*.txt', name)]
assert txt_names == ['/path/to/file.txt']
ini_names = [name for name in names if globre.match('/path/to/*.ini', name)]
assert ini_names == ['/path/to/config.ini']
all_ini_names = [name for name in names if globre.match('/path/to/**.ini', name)]
assert all_ini_names == ['/path/to/config.ini', '/path/to/subdir/base.ini']
Details
=======
This 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:
========= ====================================================================
Sequence Meaning
========= ====================================================================
``?`` Matches any single character except the slash
('/') character.
``*`` Matches zero or more characters *excluding* the slash
('/') character, e.g. ``/etc/*.conf`` which will *not*
match "/etc/foo/bar.conf".
``**`` Matches zero or more characters *including* the slash
('/') character, e.g. ``/lib/**.so`` which *will*
match "/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 a "foo" with 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\}}.txt`` which
will match "foo-bar.txt" but not "foo-012.txt".
========= ====================================================================
The `globre` package exports the following functions:
* ``globre.match(pattern, string, flags=0)``:
Tests whether or not the glob `pattern` matches the `string`. If it
does, a `re.MatchObject` is returned, otherwise ``None``. The `string`
must be matched in its entirety. See `globre.compile` for details on
the `flags` parameter. Example:
.. code-block:: python
globre.match('/etc/**.conf', '/etc/rsyslog.conf')
# => truthy
* ``globre.search(pattern, string, flags=0)``:
Similar to `globre.match`, but the pattern does not need to match
the entire string. Example:
.. code-block:: python
globre.search('lib/**.so', '/var/lib/python/readline.so.6.2')
# => truthy
* ``globre.compile(pattern, flags=0, split_prefix=False)``:
Compiles the specified `pattern` into a matching object that has the
same API as the regular expression object returned by `re.compile`.
The `flags` bit mask can contain all the standard `re` flags, in
addition to the ``globre.EXACT`` flag. 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.
If `split_prefix` is 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 pattern ``foo/**.ini`` would result in a tuple
equivalent to ``('foo/', re.compile('foo/.*\\.ini'))``.
Example:
.. code-block:: python
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/base.ini',
]
for name in names:
if not expr.match(name):
continue
# ... do something with:
# - /path/to/config.ini
# - /path/to/subdir/base.ini
What About the ``glob`` Module
==============================
This package is different from the standard Python `glob` module in
the following critical ways:
* The `glob` module operates on the actual filesystem; `globre` can be
used to match both files on the filesystem as well as any other
sources of strings to match.
* The `glob` module does not provide the ``**`` "descending" matcher.
* The `glob` module does not provide the ``{...}`` regular expression
inlining feature.
Keywords: python glob pattern matching regular expression
Platform: any
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python
Classifier: Operating System :: OS Independent
Classifier: Natural Language :: English
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)