Debian Codegraph @

ansible_collections/community/docker/plugins/module_utils/_api/utils/fnmatch.py

raw
1 # This code is part of the Ansible collection community.docker, but is an independent component.
2 # This particular file, and this file only, is based on the Docker SDK for Python (https://github.com/docker/docker-py/)
3 #
4 # Copyright (c) 2016-2022 Docker, Inc.
5 #
6 # It is licensed under the Apache 2.0 license (see LICENSES/Apache-2.0.txt in this collection)
7 # SPDX-License-Identifier: Apache-2.0
8
9 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
10 # Do not use this from other collections or standalone plugins/modules!
11
12 """Filename matching with shell patterns.
13
14 fnmatch(FILENAME, PATTERN) matches according to the local convention.
15 fnmatchcase(FILENAME, PATTERN) always takes case in account.
16
17 The functions operate by translating the pattern into a regular
18 expression. They cache the compiled regular expressions for speed.
19
20 The function translate(PATTERN) returns a regular expression
21 corresponding to PATTERN. (It does not compile it.)
22 """
23
24 from __future__ import annotations
25
26 import re
27
28 __all__ = ["fnmatch", "fnmatchcase", "translate"]
29
30 _cache: dict[str, re.Pattern] = {}
31 _MAXCACHE = 100
32
33
34 def _purge() -> None:
35 """Clear the pattern cache"""
36 _cache.clear()
37
38
39 def fnmatch(name: str, pat: str) -> bool:
40 """Test whether FILENAME matches PATTERN.
41
42 Patterns are Unix shell style:
43
44 * matches everything
45 ? matches any single character
46 [seq] matches any character in seq
47 [!seq] matches any char not in seq
48
49 An initial period in FILENAME is not special.
50 Both FILENAME and PATTERN are first case-normalized
51 if the operating system requires it.
52 If you do not want this, use fnmatchcase(FILENAME, PATTERN).
53 """
54
55 name = name.lower()
56 pat = pat.lower()
57 return fnmatchcase(name, pat)
58
59
60 def fnmatchcase(name: str, pat: str) -> bool:
61 """Test whether FILENAME matches PATTERN, including case.
62 This is a version of fnmatch() which does not case-normalize
63 its arguments.
64 """
65
66 try:
67 re_pat = _cache[pat]
68 except KeyError:
69 res = translate(pat)
70 if len(_cache) >= _MAXCACHE:
71 _cache.clear()
72 _cache[pat] = re_pat = re.compile(res)
73 return re_pat.match(name) is not None
74
75
76 def translate(pat: str) -> str:
77 """Translate a shell PATTERN to a regular expression.
78
79 There is no way to quote meta-characters.
80 """
81 i, n = 0, len(pat)
82 res = "^"
83 while i < n:
84 c = pat[i]
85 i = i + 1
86 if c == "*":
87 if i < n and pat[i] == "*":
88 # is some flavor of "**"
89 i = i + 1
90 # Treat **/ as ** so eat the "/"
91 if i < n and pat[i] == "/":
92 i = i + 1
93 if i >= n:
94 # is "**EOF" - to align with .gitignore just accept all
95 res = res + ".*"
96 else:
97 # is "**"
98 # Note that this allows for any # of /'s (even 0) because
99 # the .* will eat everything, even /'s
100 res = res + "(.*/)?"
101 else:
102 # is "*" so map it to anything but "/"
103 res = res + "[^/]*"
104 elif c == "?":
105 # "?" is any char except "/"
106 res = res + "[^/]"
107 elif c == "[":
108 j = i
109 if j < n and pat[j] == "!":
110 j = j + 1
111 if j < n and pat[j] == "]":
112 j = j + 1
113 while j < n and pat[j] != "]":
114 j = j + 1
115 if j >= n:
116 res = res + "\\["
117 else:
118 stuff = pat[i:j].replace("\\", "\\\\")
119 i = j + 1
120 if stuff[0] == "!":
121 stuff = "^" + stuff[1:]
122 elif stuff[0] == "^":
123 stuff = "\\" + stuff
124 res = f"{res}[{stuff}]"
125 else:
126 res = res + re.escape(c)
127
128 return res + "$"