/usr/lib/python2.7/dist-packages/dfwinreg/glob2regex.py is in python-dfwinreg 20170706-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
"""Glob to regular expression conversion.
Also see: https://en.wikipedia.org/wiki/Glob_(programming)
"""
import re
def Glob2Regex(glob_pattern):
"""Converts a glob pattern to a regular expression.
This function supports basic glob patterns that consist of:
* matches everything
? matches any single character
[seq] matches any character in sequence
[!seq] matches any character not in sequence
Args:
glob_pattern (str): glob pattern.
Returns:
str: regular expression pattern.
Raises:
ValueError: if the glob pattern cannot be converted.
"""
if not glob_pattern:
raise ValueError('Missing glob pattern.')
regex_pattern = []
glob_pattern_index = 0
glob_pattern_length = len(glob_pattern)
while glob_pattern_index < glob_pattern_length:
character = glob_pattern[glob_pattern_index]
glob_pattern_index += 1
if character == '*':
regex_pattern.append('.*')
elif character == '?':
regex_pattern.append('.')
elif character != '[':
regex_character = re.escape(character)
regex_pattern.append(regex_character)
else:
glob_group_index = glob_pattern_index
if (glob_group_index < glob_pattern_length and
glob_pattern[glob_group_index] == '!'):
glob_group_index += 1
if (glob_group_index < glob_pattern_length and
glob_pattern[glob_group_index] == ']'):
glob_group_index += 1
while (glob_group_index < glob_pattern_length and
glob_pattern[glob_group_index] != ']'):
glob_group_index += 1
if glob_group_index >= glob_pattern_length:
regex_pattern.append('\\[')
continue
glob_group = glob_pattern[glob_pattern_index:glob_group_index]
glob_pattern_index = glob_group_index + 1
glob_group = glob_group.replace('\\', '\\\\')
regex_pattern.append('[')
if glob_group[0] == '!':
regex_pattern.append('^')
glob_group = glob_group[1:]
elif glob_group[0] == '^':
regex_pattern.append('\\')
regex_pattern.append(glob_group)
regex_pattern.append(']')
return ''.join(regex_pattern)
|