Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/util/fnmatch.py @ 312:f5f54b9c3552
treesum: Extensively improved "help patterns":
- glob syntax rules
- examples
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 10 Mar 2025 01:57:25 +0100 |
| parents | 652870b20f9e |
| children | 48430941c18c |
comparison
equal
deleted
inserted
replaced
| 311:6d1add815d14 | 312:f5f54b9c3552 |
|---|---|
| 17 | 17 |
| 18 from . import PY2 | 18 from . import PY2 |
| 19 from . import glob | 19 from . import glob |
| 20 | 20 |
| 21 | 21 |
| 22 HELP_DESCRIPTION = """\ | 22 HELP_DESCRIPTION = r""" |
| 23 PATTERNs | 23 PATTERNs |
| 24 ======== | 24 ======== |
| 25 | 25 |
| 26 glob: case-sensitive, anchored at the begin and end | 26 Filename matching allows several types of patterns. Each pattern starts |
| 27 iglob: case-insensitive variant of "glob" | 27 with its type specification. |
| 28 re: regular expression | 28 |
| 29 path: plain text example (rooted), can be a file or a directory or a prefix | 29 glob: |
| 30 thereof | 30 case-sensitive, anchored at the begin and end |
| 31 fullpath: exactly a single full path (file or directory), relative to the | 31 |
| 32 root of the tree | 32 iglob: |
| 33 case-insensitive variant of "glob" | |
| 34 | |
| 35 re: | |
| 36 regular expression (Python style) | |
| 37 | |
| 38 path: | |
| 39 plain path name (rooted), can be a file or a directory or a prefix | |
| 40 thereof | |
| 41 | |
| 42 fullpath: | |
| 43 exactly a single full path (file or directory), relative to the | |
| 44 root of the tree | |
| 45 | |
| 46 The default if no type is given explicitely is "glob:". | |
| 47 | |
| 48 | |
| 49 Glob Syntax Rules | |
| 50 ----------------- | |
| 51 | |
| 52 - The `*' character matches zero or more characters of a name | |
| 53 component without crossing directory boundaries. | |
| 54 | |
| 55 - The `**' characters matches zero or more characters crossing | |
| 56 directory boundaries. | |
| 57 | |
| 58 - `**/' matches zero or more subdirectories; files do not match. | |
| 59 | |
| 60 - The `?' character matches exactly one character of a name component. | |
| 61 | |
| 62 - The backslash character (`\') is used to escape characters that | |
| 63 would otherwise be interpreted as special characters. The expression | |
| 64 `\\' matches a single backslash and `\{' matches a left brace for | |
| 65 example. | |
| 66 | |
| 67 - The `[ ]' characters are a bracket expression that match a single | |
| 68 character of a name component out of a set of characters. For example, | |
| 69 `[abc]' matches "a", "b", or "c". The hyphen (`-') may | |
| 70 be used to specify a range so `[a-z]' specifies a range that matches | |
| 71 from "a" to "z" (inclusive). These forms can be mixed so | |
| 72 `[abce-g]' matches "a", "b", "c", "e", "f" or "g". | |
| 73 | |
| 74 If the character after the `[' is a `!' then it is used for negation | |
| 75 so `[!a-c]' matches any character except "a", "b", or "c". | |
| 76 | |
| 77 Within a bracket expression the `*', `?' and `\' characters match | |
| 78 themselves. | |
| 79 | |
| 80 The `-' character matches itself if it is the first or last character | |
| 81 within the brackets, or the first or last character after the `!' if | |
| 82 negating. | |
| 83 | |
| 84 Also, the `]' character matches itself if it is the first character | |
| 85 within the brackets, or the first character after the `!' if negating. | |
| 86 | |
| 87 - The curly brace characters `{ }' denote a group of subpatterns, where | |
| 88 the group matches if any subpattern in the group matches. | |
| 89 | |
| 90 The `,' character is used to separate the subpatterns. Groups can be | |
| 91 nested. | |
| 92 | |
| 93 - Leading period/dot characters in file name are treated as regular characters | |
| 94 in match operations. For example, the `*' glob pattern matches file name | |
| 95 `.login'. | |
| 96 | |
| 97 - All other characters match themselves. | |
| 98 | |
| 99 | |
| 100 Examples | |
| 101 -------- | |
| 102 | |
| 103 glob:*.py | |
| 104 any name ending with ".py" in the root directory | |
| 105 | |
| 106 *.py | |
| 107 the same as "glob:*.py" (because "glob:" is the default) | |
| 108 | |
| 109 re:\A[^/]*\.py\Z | |
| 110 the same as "glob:*.py" | |
| 111 | |
| 112 glob:**.py | |
| 113 any name ending with ".py" anywhere | |
| 114 | |
| 115 re:\.py\Z | |
| 116 the same as "glob:**.py" | |
| 117 | |
| 118 glob:dir/* | |
| 119 any name in directory "dir" | |
| 120 | |
| 121 Each of these patterns specify any name below directory "dir": | |
| 122 | |
| 123 glob:dir/** | |
| 124 | |
| 125 re:\Adir/ | |
| 126 | |
| 127 path:dir/ | |
| 128 | |
| 129 Each of these patterns specify any name in any directory that ends with | |
| 130 "file": | |
| 131 | |
| 132 glob:**/file | |
| 133 | |
| 134 re:(^|/)file\Z | |
| 135 | |
| 136 These patterns specify a single path: | |
| 137 | |
| 138 filepath:dir1/dir2/file | |
| 139 | |
| 140 re:\Adir1/dir2/file\Z | |
| 33 | 141 |
| 34 """ | 142 """ |
| 35 | 143 |
| 36 | 144 |
| 37 def glob_factory(pattern): | 145 def glob_factory(pattern): |
