GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — codeql/python/extractor/semmle/path_filters.py at codeql-cli/v2.27.2 · github/codeql Expand file tree/ Copy pathpath_filters.pyMore file actions 79 lines (72 loc) · 2.81 KB / Copy pathpath_filters.pyFile metadata and controls79 lines (72 loc) · 2.81 KB 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 import os import re def escape(pattern): '''Escape special characters''' ESCAPE = "(){}[].^$+\\?|" def escape_char(char): if char in ESCAPE: return "\\" + char else: return char return ''.join(escape_char(c) for c in pattern) SEP = escape(os.sep) STAR_STAR_REGEX = "([^%s]*%s)*" % (SEP, SEP) STAR_REGEX = "[^%s]*" % SEP def validate_pattern(pattern): '''Validate that an include/exclude pattern is of the correct syntax.''' kind, glob = pattern.split(":") if not kind in ("include", "exclude"): raise SyntaxError("Illegal type: '%s'" % kind) parts = glob.split("/") for p in parts: if "**" in p and p != "**": raise SyntaxError("Illegal path element: '%s'" % p) def glob_part_to_regex(glob, add_sep): '''Convert glob part to regex pattern''' if glob == "**": return STAR_STAR_REGEX if '*' in glob: pattern = glob.replace('*', STAR_REGEX) else: pattern = glob if add_sep: return pattern + SEP else: return pattern def glob_to_regex(glob, prefix=""): '''Convert entire glob to a compiled regex''' # When the glob ends in `/`, we need to remember this so that we don't accidentally add an # extra separator to the final regex. end_sep = "" if glob.endswith("/") else SEP glob = glob.strip().strip("/") parts = glob.split("/") #Trailing '**' is redundant, so strip it off. if parts[-1] == "**": parts = parts[:-1] if not parts: return ".*" # The `glob.strip("/")` call above will have removed all trailing slashes, but if there was at # least one trailing slash, we want there to be an extra part, so we add it explicitly here in # that case, using the emptyness of `end_sep` as a proxy. if end_sep == "": parts += [""] parts = [ glob_part_to_regex(escape(p), True) for p in parts[:-1] ] + [ glob_part_to_regex(escape(parts[-1]), False) ] # we need to escape the prefix, specifically because on windows the prefix will be # something like `C:\\folder\\subfolder\\` and without escaping the # backslash-path-separators will get interpreted as regex escapes (which might be # invalid sequences, causing the extractor to crash) full_pattern = escape(prefix) + ''.join(parts) + "(?:" + end_sep + ".*|$)" return re.compile(full_pattern) def filter_from_pattern(pattern, prev_filter, prefix): '''Create a filter function from a pattern and the previous filter. The pattern takes precedence over the previous filter ''' validate_pattern(pattern) kind, glob = pattern.strip().split(":") result = kind == "include" regex = glob_to_regex(glob, prefix) def filter(path): if regex.match(path): return result return prev_filter(path) return filter |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |