|
1
|
1 |
|
class Text: |
|
2
|
|
|
|
|
3
|
1 |
|
EMPTY = '_' |
|
4
|
1 |
|
SPACE = '_' |
|
5
|
1 |
|
ALTERNATE_SPACES = ['_', '-'] |
|
6
|
1 |
|
SPECIAL_CHARACTERS = { |
|
7
|
|
|
'?': '~q', |
|
8
|
|
|
'%': '~p', |
|
9
|
|
|
'#': '~h', |
|
10
|
|
|
'/': '~s', |
|
11
|
|
|
'"': "''", |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
1 |
|
def __init__(self, path=None, *, translate_spaces=True): |
|
15
|
1 |
|
if path is None: |
|
16
|
1 |
|
self._parts = [] |
|
17
|
1 |
|
elif isinstance(path, str): |
|
18
|
1 |
|
self._parts = path.split('/') |
|
19
|
|
|
else: |
|
20
|
1 |
|
assert isinstance(path, list) |
|
21
|
1 |
|
self._parts = path |
|
22
|
1 |
|
self._translate_spaces = translate_spaces |
|
23
|
|
|
|
|
24
|
1 |
|
def __str__(self): |
|
25
|
1 |
|
return ' / '.join(self.lines) |
|
26
|
|
|
|
|
27
|
1 |
|
def __bool__(self): |
|
28
|
1 |
|
return bool(self.path.strip(self.SPACE + '/')) |
|
29
|
|
|
|
|
30
|
1 |
|
def __getitem__(self, key): |
|
31
|
1 |
|
try: |
|
32
|
1 |
|
part = self._parts[key] |
|
33
|
1 |
|
except (IndexError, ValueError): |
|
34
|
1 |
|
return "" |
|
35
|
|
|
else: |
|
36
|
1 |
|
return part.strip() |
|
37
|
|
|
|
|
38
|
1 |
|
@property |
|
39
|
|
|
def top(self): |
|
40
|
1 |
|
return self.get_line(0) |
|
41
|
|
|
|
|
42
|
1 |
|
@property |
|
43
|
|
|
def bottom(self): |
|
44
|
1 |
|
return self.get_line(1) |
|
45
|
|
|
|
|
46
|
1 |
|
@property |
|
47
|
|
|
def lines(self): |
|
48
|
1 |
|
lines = [] |
|
49
|
|
|
|
|
50
|
1 |
|
previous_part = True |
|
51
|
1 |
|
for part in self: |
|
52
|
1 |
|
if part: |
|
53
|
1 |
|
line = self._format_line(part, self._translate_spaces) |
|
54
|
1 |
|
lines.append(line) |
|
55
|
1 |
|
elif not previous_part: |
|
56
|
1 |
|
break |
|
57
|
|
|
else: |
|
58
|
1 |
|
lines.append(' ') |
|
59
|
1 |
|
previous_part = part |
|
60
|
|
|
|
|
61
|
1 |
|
return lines[:-1] |
|
62
|
|
|
|
|
63
|
1 |
|
@property |
|
64
|
|
|
def path(self): |
|
65
|
1 |
|
paths = [] |
|
66
|
|
|
|
|
67
|
1 |
|
for line in self.lines: |
|
68
|
1 |
|
path = self._format_path(line) |
|
69
|
1 |
|
paths.append(path) |
|
70
|
|
|
|
|
71
|
1 |
|
return '/'.join(paths) |
|
72
|
|
|
|
|
73
|
1 |
|
@classmethod |
|
74
|
|
|
def _format_line(cls, text, translate_spaces): |
|
75
|
1 |
|
for special, replacement in cls.SPECIAL_CHARACTERS.items(): |
|
76
|
1 |
|
text = text.replace(replacement, special) |
|
77
|
1 |
|
text = text.replace(replacement.upper(), special) |
|
78
|
|
|
|
|
79
|
1 |
|
chars = [] |
|
80
|
1 |
|
escape = None |
|
81
|
|
|
|
|
82
|
1 |
|
for index, char in enumerate(text): |
|
83
|
|
|
|
|
84
|
1 |
|
if char in cls.ALTERNATE_SPACES and translate_spaces: |
|
85
|
1 |
|
if char == escape: |
|
86
|
1 |
|
chars[-1] = escape |
|
87
|
1 |
|
escape = None |
|
88
|
|
|
else: |
|
89
|
1 |
|
chars.append(' ') |
|
90
|
1 |
|
escape = char |
|
91
|
1 |
|
continue |
|
92
|
|
|
|
|
93
|
1 |
|
escape = None |
|
94
|
|
|
|
|
95
|
1 |
|
if not char.isalpha(): |
|
96
|
1 |
|
chars.append(char) |
|
97
|
1 |
|
continue |
|
98
|
|
|
|
|
99
|
1 |
|
if len(chars) >= 2: |
|
100
|
1 |
|
if char.isupper() and chars[-1].islower() and chars[-2] != ' ': |
|
101
|
1 |
|
chars.append(' ' + char) |
|
102
|
1 |
|
continue |
|
103
|
|
|
|
|
104
|
1 |
|
if len(chars) >= 1 and len(text) > index + 1: |
|
105
|
1 |
|
n_char = text[index + 1] |
|
106
|
1 |
|
if char.isupper() and chars[-1].isupper() and n_char.islower(): |
|
107
|
1 |
|
chars.append(' ' + char) |
|
108
|
1 |
|
continue |
|
109
|
|
|
|
|
110
|
1 |
|
chars.append(char) |
|
111
|
|
|
|
|
112
|
1 |
|
return ''.join(chars).upper() |
|
113
|
|
|
|
|
114
|
1 |
|
@classmethod |
|
115
|
|
|
def _format_path(cls, line): |
|
116
|
1 |
|
if line == ' ': |
|
117
|
1 |
|
path = cls.EMPTY |
|
118
|
|
|
else: |
|
119
|
1 |
|
path = line.lower() |
|
120
|
1 |
|
for space in cls.ALTERNATE_SPACES: |
|
121
|
1 |
|
path = path.replace(space, space * 2) |
|
122
|
1 |
|
path = path.replace(' ', cls.SPACE) |
|
123
|
1 |
|
for special, replacement in cls.SPECIAL_CHARACTERS.items(): |
|
124
|
1 |
|
path = path.replace(special, replacement) |
|
125
|
|
|
|
|
126
|
1 |
|
return path |
|
127
|
|
|
|
|
128
|
1 |
|
def get_line(self, index): |
|
129
|
|
|
return self._format_line(self[index], self._translate_spaces) |
|
130
|
|
|
|