forked from behave/behave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextutil.py
264 lines (242 loc) · 9.34 KB
/
textutil.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# -*- coding -*-
"""
Provides some command utility functions.
TODO:
matcher that ignores empty lines and whitespace and has contains comparison
"""
from __future__ import absolute_import, print_function, unicode_literals
from hamcrest import assert_that, is_not, equal_to, contains_string
# DISABLED: from behave4cmd.hamcrest_text import matches_regexp
import codecs
DEBUG = False
# -----------------------------------------------------------------------------
# CLASS: TextProxy
# -----------------------------------------------------------------------------
# class TextProxy(object):
# """
# Simplifies conversion between (Unicode) string and its byte representation.
# Provides a ValueObject to store a string or its byte representation.
# Afterwards you can explicitly access both representations by using:
#
# EXAMPLE:
#
# .. testcode::
#
# from behave4cmd.textutil import TextProxy
# message = TextProxy("Hello world", encoding="UTF-8")
# assert message.data == "Hello world" # -- RAW DATA access.
# assert isinstance(message.text, basestring)
# assert isinstance(message.bytes, bytes)
# assert message == "Hello world"
# assert len(message) == len(message.data) == 11
# """
# encoding_errors = "strict"
# default_encoding = "UTF-8"
#
# def __init__(self, data=None, encoding=None, errors=None):
# self.encoding = encoding or self.default_encoding
# self.errors = errors or self.encoding_errors
# self.set(data)
#
# def get(self):
# return self.data
#
# def set(self, data):
# self.data = data or ""
# self._text = None
# self._bytes = None
#
# def clear(self):
# self.set(None)
#
# @property
# def text(self):
# """Provide access to string-representation of the data."""
# if self._text is None:
# if isinstance(self.data, basestring):
# _text = self.data
# elif isinstance(self.data, bytes):
# _text = codecs.decode(self.data, self.encoding, self.errors)
# else:
# _text = str(self.data)
# self._text = _text
# assert isinstance(self._text, basestring)
# return self._text
#
# @property
# def bytes(self):
# """Provide access to byte-representation of the data."""
# if self._bytes is None:
# if isinstance(self.data, bytes) and not isinstance(self.data, str):
# self._bytes = self.data
# else:
# text = self.data
# if not isinstance(text, basestring):
# text = unicode(text)
# assert isinstance(text, basestring)
# self._bytes = codecs.encode(text, self.encoding, self.errors)
# assert isinstance(self._bytes, bytes)
# return self._bytes
#
# def __repr__(self):
# """Textual representation of this object."""
# data = self.data or ""
# prefix = ""
# if isinstance(data, bytes) and not isinstance(data, basestring):
# prefix= u"b"
# # str(self.text)
# # str(self.encoding)
# # str(prefix)
# # _ = u"<TextProxy data[size=%d]=x'x', encoding=x>" % len(self)
# # _ = u"<TextProxy data[size=x]=%s'x', encoding=x>" % prefix
# # _ = u"<TextProxy data[size=x]=x'%s', encoding=x>" % self.text
# # _ = u"<TextProxy data[size=x]=x'x', encoding=%s>" % self.encoding
# return u"<TextProxy data[size=%d]=%s'%s', encoding=%s>" %\
# (len(self), prefix, self.text, self.encoding)
#
# def __str__(self):
# """Conversion into str() object."""
# return self.text
#
# def __bytes__(self):
# """Conversion into bytes() object."""
# return self.bytes
#
# def __bool__(self):
# """Conversion into a bool value, used for truth testing."""
# return bool(self.data)
#
# def __iter__(self):
# """Conversion into an iterator."""
# return iter(self.data)
#
# def __contains__(self, item):
# """Check if item is contained in raw data."""
# if isinstance(item, basestring):
# return item in self.text
# elif isinstance(item, bytes):
# return item in self.bytes
# else:
# return item in self.data
#
# def __len__(self):
# if self.data is None:
# return 0
# return len(self.data)
#
# def __nonzero__(self):
# return len(self) > 0
#
# def __eq__(self, other):
# if isinstance(other, basestring):
# return self.text == other
# elif isinstance(other, bytes):
# return self.bytes == other
# else:
# return self.data == other
#
# def __ne__(self, other):
# return not (self == other)
#
# -----------------------------------------------------------------------------
# UTILITY FUNCTIONS:
# -----------------------------------------------------------------------------
def template_substitute(text, **kwargs):
"""
Replace placeholders in text by using the data mapping.
Other placeholders that is not represented by data is left untouched.
:param text: Text to search and replace placeholders.
:param data: Data mapping/dict for placeholder key and values.
:return: Potentially modified text with replaced placeholders.
"""
for name, value in kwargs.items():
placeholder_pattern = "{%s}" % name
if placeholder_pattern in text:
text = text.replace(placeholder_pattern, value)
return text
def text_remove_empty_lines(text):
"""
Whitespace normalization:
- Strip empty lines
- Strip trailing whitespace
"""
lines = [ line.rstrip() for line in text.splitlines() if line.strip() ]
return "\n".join(lines)
def text_normalize(text):
"""
Whitespace normalization:
- Strip empty lines
- Strip leading whitespace in a line
- Strip trailing whitespace in a line
- Normalize line endings
"""
# if not isinstance(text, str):
if isinstance(text, bytes):
# -- MAYBE: command.ouput => bytes, encoded stream output.
text = codecs.decode(text)
lines = [ line.strip() for line in text.splitlines() if line.strip() ]
return "\n".join(lines)
# -----------------------------------------------------------------------------
# ASSERTIONS:
# -----------------------------------------------------------------------------
def assert_text_should_equal(actual_text, expected_text):
assert_that(actual_text, equal_to(expected_text))
def assert_text_should_not_equal(actual_text, expected_text):
assert_that(actual_text, is_not(equal_to(expected_text)))
def assert_text_should_contain_exactly(text, expected_part):
assert_that(text, contains_string(expected_part))
def assert_text_should_not_contain_exactly(text, expected_part):
assert_that(text, is_not(contains_string(expected_part)))
def assert_text_should_contain(text, expected_part):
assert_that(text, contains_string(expected_part))
def assert_text_should_not_contain(text, unexpected_part):
assert_that(text, is_not(contains_string(unexpected_part)))
def assert_normtext_should_equal(actual_text, expected_text):
expected_text2 = text_normalize(expected_text.strip())
actual_text2 = text_normalize(actual_text.strip())
assert_that(actual_text2, equal_to(expected_text2))
def assert_normtext_should_not_equal(actual_text, expected_text):
expected_text2 = text_normalize(expected_text.strip())
actual_text2 = text_normalize(actual_text.strip())
assert_that(actual_text2, is_not(equal_to(expected_text2)))
def assert_normtext_should_contain(text, expected_part):
expected_part2 = text_normalize(expected_part)
actual_text = text_normalize(text.strip())
if DEBUG:
print("expected:\n{0}".format(expected_part2))
print("actual:\n{0}".format(actual_text))
assert_text_should_contain(actual_text, expected_part2)
def assert_normtext_should_not_contain(text, unexpected_part):
unexpected_part2 = text_normalize(unexpected_part)
actual_text = text_normalize(text.strip())
if DEBUG:
print("expected:\n{0}".format(unexpected_part2))
print("actual:\n{0}".format(actual_text))
assert_text_should_not_contain(actual_text, unexpected_part2)
# def assert_text_should_match_pattern(text, pattern):
# """
# Assert that the :attr:`text` matches the regular expression :attr:`pattern`.
#
# :param text: Multi-line text (as string).
# :param pattern: Regular expression pattern (as string, compiled regexp).
# :raise: AssertionError, if text matches not the pattern.
# """
# assert_that(text, matches_regexp(pattern))
#
# def assert_text_should_not_match_pattern(text, pattern):
# """
# Assert that the :attr:`text` matches not the regular expression
# :attr:`pattern`.
#
# :param text: Multi-line text (as string).
# :param pattern: Regular expression pattern (as string, compiled regexp).
# :raise: AssertionError, if text matches the pattern.
# """
# assert_that(text, is_not(matches_regexp(pattern)))
#
# -----------------------------------------------------------------------------
# MAIN:
# -----------------------------------------------------------------------------
if __name__ == "__main__":
import doctest
doctest.testmod()