1
|
|
|
|
2
|
|
|
import os |
3
|
|
|
import warnings |
4
|
|
|
|
5
|
|
|
import regex |
6
|
|
|
|
7
|
|
|
from . import StateMachine |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class GlobTransform(StateMachine): |
11
|
|
|
jumps = { |
12
|
|
|
'start': { |
13
|
|
|
'': 'text', |
14
|
|
|
}, |
15
|
|
|
'text': { |
16
|
|
|
'*': 'wildcard', |
17
|
|
|
'**': 'wildcard', |
18
|
|
|
'?': 'wildcard', |
19
|
|
|
'[': 'range', |
20
|
|
|
'[!': 'range', |
21
|
|
|
'[]': 'range', |
22
|
|
|
'{': 'group', |
23
|
|
|
}, |
24
|
|
|
'text_literal': { |
25
|
|
|
'': 'text', |
26
|
|
|
}, |
27
|
|
|
'ignore': { |
28
|
|
|
'': 'text', |
29
|
|
|
}, |
30
|
|
|
'wildcard': { |
31
|
|
|
'': 'text', |
32
|
|
|
}, |
33
|
|
|
'range': { |
34
|
|
|
']': 'text_literal', |
35
|
|
|
'[.': 'posix_collating_symbol', |
36
|
|
|
'[:': 'posix_character_class', |
37
|
|
|
'[=': 'posix_equivalence_class', |
38
|
|
|
}, |
39
|
|
|
'range_literal': { |
40
|
|
|
'': 'range', |
41
|
|
|
}, |
42
|
|
|
'posix_collating_symbol': { |
43
|
|
|
'.]': 'ignore', |
44
|
|
|
}, |
45
|
|
|
'posix_character_class': { |
46
|
|
|
':]': 'range_literal', |
47
|
|
|
}, |
48
|
|
|
'posix_equivalence_class': { |
49
|
|
|
'=]': 'ignore', |
50
|
|
|
}, |
51
|
|
|
'group': { |
52
|
|
|
'}': 'group_close', |
53
|
|
|
}, |
54
|
|
|
'group_close': { |
55
|
|
|
'': 'text', |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
jumps['text'].update(('\\%s' % c, 'text_literal') for c in '\\*?[{') |
59
|
|
|
current = 'start' |
60
|
|
|
|
61
|
|
|
def __init__(self, data, sep=os.sep): |
62
|
|
|
self.sep = sep |
63
|
|
|
self.jumps['start'][sep] = self.jumps['start'][''] |
64
|
|
|
super(GlobTransform, self).__init__(data) |
65
|
|
|
|
66
|
|
|
def flush(self): |
67
|
|
|
return '%s$' % super(GlobTransform, self).flush() |
68
|
|
|
|
69
|
|
|
def transform_posix_collating_class(self, data, mark, next): |
70
|
|
|
warnings.warn( |
71
|
|
|
'Posix collating symbols (like %s%s) are not supported.' |
72
|
|
|
% (data, mark)) |
73
|
|
|
return '.' |
74
|
|
|
|
75
|
|
|
def transform_posix_equivalence_class(self, data, mark, next): |
76
|
|
|
warnings.warn( |
77
|
|
|
'Posix equivalence class expresions (like %s%s) are not supported.' |
78
|
|
|
% (data, mark)) |
79
|
|
|
return '.' |
80
|
|
|
|
81
|
|
|
def transform_ignore(self, data, mark, next): |
82
|
|
|
return '' |
83
|
|
|
|
84
|
|
|
def transform_start(self, data, mark, next): |
85
|
|
|
if mark == self.sep: |
86
|
|
|
return '^' |
87
|
|
|
return regex.escape(self.sep, special_only=True) |
88
|
|
|
|
89
|
|
|
def transform_wildcard(self, data, mark, next): |
90
|
|
|
if self.start == '**': |
91
|
|
|
return '.*' |
92
|
|
|
if self.start == '*': |
93
|
|
|
return '[^%s]*' % self.sep |
94
|
|
|
return '.' |
95
|
|
|
|
96
|
|
|
def transform_text(self, data, mark, next): |
97
|
|
|
return regex.escape(data, special_only=True) |
98
|
|
|
|
99
|
|
|
def transform_range(self, data, mark, next): |
100
|
|
|
if self.start == '[!': |
101
|
|
|
return '[^%s' % data[2:] |
102
|
|
|
if self.start == '[]': |
103
|
|
|
return '[\\]%s' % data[2:] |
104
|
|
|
return data |
105
|
|
|
|
106
|
|
|
def transform_group(self, data, mark, next): |
107
|
|
|
return '(%s' % ('|'.join(data[len(self.start):].split(','))) |
108
|
|
|
|
109
|
|
|
def transform_group_close(self, data, mark, next): |
110
|
|
|
return ')' |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def translate(data, sep=os.sep): |
114
|
|
|
self = GlobTransform(data) |
115
|
|
|
return ''.join(self) |
116
|
|
|
|