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