MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / get_matches

Function get_matches

src/commoncode/fileset.py:101–151  ·  view source on GitHub ↗

Return a list of values (which are values from the matched `patterns` mappint of {pattern: value or message} if `path` is matched by any of the pattern from the `patterns` map or an empty list. If `all_matches` is False, stop and return on the first matched pattern.

(path, patterns, all_matches=False)

Source from the content-addressed store, hash-verified

99
100
101def get_matches(path, patterns, all_matches=False):
102 """
103 Return a list of values (which are values from the matched `patterns`
104 mappint of {pattern: value or message} if `path` is matched by any of the
105 pattern from the `patterns` map or an empty list.
106 If `all_matches` is False, stop and return on the first matched pattern.
107 """
108 if not path or not patterns:
109 return False
110
111 path = fileutils.as_posixpath(path).lower()
112 pathstripped = path.lstrip("/0")
113 if not pathstripped:
114 return False
115
116 segments = paths.split(pathstripped)
117
118 if TRACE:
119 logger.debug("_match: path: %(path)r patterns:%(patterns)r." % locals())
120
121 matches = []
122 if not isinstance(patterns, dict):
123 assert isinstance(patterns, (list, tuple)), "Invalid patterns: {}".format(patterns)
124 patterns = {p: p for p in patterns}
125
126 for pat, value in patterns.items():
127 if not pat or not pat.strip():
128 continue
129
130 value = value or ""
131 pat = pat.lstrip("/").lower()
132 is_plain = "/" not in pat
133
134 if is_plain:
135 if any(fnmatch.fnmatchcase(s, pat) for s in segments):
136 matches.append(value)
137 if not all_matches:
138 break
139 elif fnmatch.fnmatchcase(path, pat) or fnmatch.fnmatchcase(pathstripped, pat):
140 matches.append(value)
141 if not all_matches:
142 break
143 if TRACE:
144 logger.debug("_match: matches: %(matches)r" % locals())
145
146 if not all_matches:
147 if matches:
148 return matches[0]
149 else:
150 return False
151 return matches
152
153
154def load(location):

Callers 1

is_includedFunction · 0.70

Calls 4

splitMethod · 0.45
debugMethod · 0.45
formatMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected