scylla-cqlsh 6.0.29__cp310-cp310-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- copyutil.cp310-win_amd64.pyd +0 -0
- cqlsh/__init__.py +1 -0
- cqlsh/__main__.py +11 -0
- cqlsh/cqlsh.py +2736 -0
- cqlshlib/__init__.py +90 -0
- cqlshlib/_version.py +34 -0
- cqlshlib/authproviderhandling.py +176 -0
- cqlshlib/copyutil.py +2762 -0
- cqlshlib/cql3handling.py +1670 -0
- cqlshlib/cqlhandling.py +333 -0
- cqlshlib/cqlshhandling.py +314 -0
- cqlshlib/displaying.py +128 -0
- cqlshlib/formatting.py +601 -0
- cqlshlib/helptopics.py +190 -0
- cqlshlib/pylexotron.py +562 -0
- cqlshlib/saferscanner.py +91 -0
- cqlshlib/sslhandling.py +109 -0
- cqlshlib/tracing.py +90 -0
- cqlshlib/util.py +183 -0
- cqlshlib/wcwidth.py +379 -0
- scylla_cqlsh-6.0.29.dist-info/METADATA +108 -0
- scylla_cqlsh-6.0.29.dist-info/RECORD +26 -0
- scylla_cqlsh-6.0.29.dist-info/WHEEL +5 -0
- scylla_cqlsh-6.0.29.dist-info/entry_points.txt +2 -0
- scylla_cqlsh-6.0.29.dist-info/licenses/LICENSE.txt +204 -0
- scylla_cqlsh-6.0.29.dist-info/top_level.txt +3 -0
cqlshlib/formatting.py
ADDED
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
2
|
+
# or more contributor license agreements. See the NOTICE file
|
|
3
|
+
# distributed with this work for additional information
|
|
4
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
5
|
+
# to you under the Apache License, Version 2.0 (the
|
|
6
|
+
# "License"); you may not use this file except in compliance
|
|
7
|
+
# with the License. You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
import calendar
|
|
18
|
+
import datetime
|
|
19
|
+
import math
|
|
20
|
+
import os
|
|
21
|
+
import re
|
|
22
|
+
import sys
|
|
23
|
+
|
|
24
|
+
from collections import defaultdict
|
|
25
|
+
|
|
26
|
+
from cassandra.cqltypes import EMPTY
|
|
27
|
+
from cassandra.util import datetime_from_timestamp
|
|
28
|
+
from . import wcwidth
|
|
29
|
+
from .displaying import colorme, get_str, FormattedValue, DEFAULT_VALUE_COLORS, NO_COLOR_MAP
|
|
30
|
+
from .util import UTC
|
|
31
|
+
|
|
32
|
+
unicode_controlchars_re = re.compile(r'[\x00-\x1f\x7f-\xa0]')
|
|
33
|
+
controlchars_re = re.compile(r'[\x00-\x1f\x7f-\xff]')
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _show_control_chars(match):
|
|
37
|
+
txt = repr(match.group(0))
|
|
38
|
+
if txt.startswith('u'):
|
|
39
|
+
txt = txt[2:-1]
|
|
40
|
+
else:
|
|
41
|
+
txt = txt[1:-1]
|
|
42
|
+
return txt
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
bits_to_turn_red_re = re.compile(r'\\([^uUx]|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|U[0-9a-fA-F]{8})')
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _make_turn_bits_red_f(color1, color2):
|
|
49
|
+
def _turn_bits_red(match):
|
|
50
|
+
txt = match.group(0)
|
|
51
|
+
if txt == '\\\\':
|
|
52
|
+
return '\\'
|
|
53
|
+
return color1 + txt + color2
|
|
54
|
+
return _turn_bits_red
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
default_null_placeholder = 'null'
|
|
58
|
+
default_float_precision = 3
|
|
59
|
+
default_colormap = DEFAULT_VALUE_COLORS
|
|
60
|
+
empty_colormap = defaultdict(lambda: '')
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def format_by_type(val, cqltype, encoding, colormap=None, addcolor=False,
|
|
64
|
+
nullval=None, date_time_format=None, float_precision=None,
|
|
65
|
+
decimal_sep=None, thousands_sep=None, boolean_styles=None):
|
|
66
|
+
if nullval is None:
|
|
67
|
+
nullval = default_null_placeholder
|
|
68
|
+
if val is None:
|
|
69
|
+
return colorme(nullval, colormap, 'error')
|
|
70
|
+
if addcolor is False:
|
|
71
|
+
colormap = empty_colormap
|
|
72
|
+
elif colormap is None:
|
|
73
|
+
colormap = default_colormap
|
|
74
|
+
if date_time_format is None:
|
|
75
|
+
date_time_format = DateTimeFormat()
|
|
76
|
+
if float_precision is None:
|
|
77
|
+
float_precision = default_float_precision
|
|
78
|
+
return format_value(val, cqltype=cqltype, encoding=encoding, colormap=colormap,
|
|
79
|
+
date_time_format=date_time_format, float_precision=float_precision,
|
|
80
|
+
nullval=nullval, decimal_sep=decimal_sep, thousands_sep=thousands_sep,
|
|
81
|
+
boolean_styles=boolean_styles)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def color_text(bval, colormap, displaywidth=None):
|
|
85
|
+
# note that here, we render natural backslashes as just backslashes,
|
|
86
|
+
# in the same color as surrounding text, when using color. When not
|
|
87
|
+
# using color, we need to double up the backslashes so it's not
|
|
88
|
+
# ambiguous. This introduces the unique difficulty of having different
|
|
89
|
+
# display widths for the colored and non-colored versions. To avoid
|
|
90
|
+
# adding the smarts to handle that in to FormattedValue, we just
|
|
91
|
+
# make an explicit check to see if a null colormap is being used or
|
|
92
|
+
# not.
|
|
93
|
+
if displaywidth is None:
|
|
94
|
+
displaywidth = len(bval)
|
|
95
|
+
tbr = _make_turn_bits_red_f(colormap['blob'], colormap['text'])
|
|
96
|
+
coloredval = colormap['text'] + bits_to_turn_red_re.sub(tbr, bval) + colormap['reset']
|
|
97
|
+
if colormap['text']:
|
|
98
|
+
displaywidth -= bval.count(r'\\')
|
|
99
|
+
return FormattedValue(bval, coloredval, displaywidth)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
DEFAULT_NANOTIME_FORMAT = '%H:%M:%S.%N'
|
|
103
|
+
DEFAULT_DATE_FORMAT = '%Y-%m-%d'
|
|
104
|
+
|
|
105
|
+
DEFAULT_TIMESTAMP_FORMAT = os.environ.get('CQLSH_DEFAULT_TIMESTAMP_FORMAT', '')
|
|
106
|
+
if not DEFAULT_TIMESTAMP_FORMAT:
|
|
107
|
+
DEFAULT_TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S.%f%z'
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class DateTimeFormat:
|
|
111
|
+
|
|
112
|
+
def __init__(self, timestamp_format=DEFAULT_TIMESTAMP_FORMAT, date_format=DEFAULT_DATE_FORMAT,
|
|
113
|
+
nanotime_format=DEFAULT_NANOTIME_FORMAT, timezone=None, milliseconds_only=False):
|
|
114
|
+
self.timestamp_format = timestamp_format
|
|
115
|
+
self.date_format = date_format
|
|
116
|
+
self.nanotime_format = nanotime_format
|
|
117
|
+
self.timezone = timezone
|
|
118
|
+
self.milliseconds_only = milliseconds_only # the microseconds part, .NNNNNN, wil be rounded to .NNN
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class CqlType:
|
|
122
|
+
"""
|
|
123
|
+
A class for converting a string into a cql type name that can match a formatter
|
|
124
|
+
and a list of its sub-types, if any.
|
|
125
|
+
"""
|
|
126
|
+
pattern = re.compile('^([^<]*)<(.*)>$') # *<*>
|
|
127
|
+
|
|
128
|
+
def __init__(self, typestring, ksmeta=None):
|
|
129
|
+
self.type_name, self.sub_types, self.formatter = self.parse(typestring, ksmeta)
|
|
130
|
+
|
|
131
|
+
def __str__(self):
|
|
132
|
+
return "%s%s" % (self.type_name, self.sub_types or '')
|
|
133
|
+
|
|
134
|
+
__repr__ = __str__
|
|
135
|
+
|
|
136
|
+
def get_n_sub_types(self, num):
|
|
137
|
+
"""
|
|
138
|
+
Return the sub-types if the requested number matches the length of the sub-types (tuples)
|
|
139
|
+
or the first sub-type times the number requested if the length of the sub-types is one (list, set),
|
|
140
|
+
otherwise raise an exception
|
|
141
|
+
"""
|
|
142
|
+
if len(self.sub_types) == num:
|
|
143
|
+
return self.sub_types
|
|
144
|
+
elif len(self.sub_types) == 1:
|
|
145
|
+
return [self.sub_types[0]] * num
|
|
146
|
+
else:
|
|
147
|
+
raise Exception("Unexpected number of subtypes %d - %s" % (num, self.sub_types))
|
|
148
|
+
|
|
149
|
+
def parse(self, typestring, ksmeta):
|
|
150
|
+
"""
|
|
151
|
+
Parse the typestring by looking at this pattern: *<*>. If there is no match then the type
|
|
152
|
+
is either a simple type or a user type, otherwise it must be a composite type
|
|
153
|
+
for which we need to look-up the sub-types. For user types the sub types can be extracted
|
|
154
|
+
from the keyspace metadata.
|
|
155
|
+
"""
|
|
156
|
+
while True:
|
|
157
|
+
m = self.pattern.match(typestring)
|
|
158
|
+
if not m: # no match, either a simple or a user type
|
|
159
|
+
name = typestring
|
|
160
|
+
if ksmeta and name in ksmeta.user_types: # a user type, look at ks meta for sub types
|
|
161
|
+
sub_types = [CqlType(t, ksmeta) for t in ksmeta.user_types[name].field_types]
|
|
162
|
+
return name, sub_types, format_value_utype
|
|
163
|
+
else:
|
|
164
|
+
return name, [], self._get_formatter(name)
|
|
165
|
+
else:
|
|
166
|
+
if m.group(1) == 'frozen': # ignore frozen<>
|
|
167
|
+
typestring = m.group(2)
|
|
168
|
+
continue
|
|
169
|
+
|
|
170
|
+
name = m.group(1) # a composite type, parse sub types
|
|
171
|
+
return name, self.parse_sub_types(m.group(2), ksmeta), self._get_formatter(name)
|
|
172
|
+
|
|
173
|
+
@staticmethod
|
|
174
|
+
def _get_formatter(name):
|
|
175
|
+
return _formatters.get(name.lower())
|
|
176
|
+
|
|
177
|
+
@staticmethod
|
|
178
|
+
def parse_sub_types(val, ksmeta):
|
|
179
|
+
"""
|
|
180
|
+
Split val into sub-strings separated by commas but only if not within a <> pair
|
|
181
|
+
Return a list of CqlType instances where each instance is initialized with the sub-strings
|
|
182
|
+
that were found.
|
|
183
|
+
"""
|
|
184
|
+
last = 0
|
|
185
|
+
level = 0
|
|
186
|
+
ret = []
|
|
187
|
+
for i, c in enumerate(val):
|
|
188
|
+
if c == '<':
|
|
189
|
+
level += 1
|
|
190
|
+
elif c == '>':
|
|
191
|
+
level -= 1
|
|
192
|
+
elif c == ',' and level == 0:
|
|
193
|
+
ret.append(val[last:i].strip())
|
|
194
|
+
last = i + 1
|
|
195
|
+
|
|
196
|
+
if last < len(val) - 1:
|
|
197
|
+
ret.append(val[last:].strip())
|
|
198
|
+
|
|
199
|
+
return [CqlType(r, ksmeta) for r in ret]
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def format_value_default(val, colormap, **_):
|
|
203
|
+
val = str(val)
|
|
204
|
+
escapedval = val.replace('\\', '\\\\')
|
|
205
|
+
bval = controlchars_re.sub(_show_control_chars, escapedval)
|
|
206
|
+
return bval if colormap is NO_COLOR_MAP else color_text(bval, colormap)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
# Mapping cql type base names ("int", "map", etc) to formatter functions,
|
|
210
|
+
# making format_value a generic function
|
|
211
|
+
_formatters = {}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def format_value(val, cqltype, **kwargs):
|
|
215
|
+
if val == EMPTY:
|
|
216
|
+
return format_value_default('', **kwargs)
|
|
217
|
+
formatter = get_formatter(val, cqltype)
|
|
218
|
+
return formatter(val, cqltype=cqltype, **kwargs)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def get_formatter(val, cqltype):
|
|
222
|
+
if cqltype and cqltype.formatter:
|
|
223
|
+
return cqltype.formatter
|
|
224
|
+
|
|
225
|
+
return _formatters.get(type(val).__name__.lower(), format_value_default)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def formatter_for(typname):
|
|
229
|
+
def registrator(f):
|
|
230
|
+
_formatters[typname.lower()] = f
|
|
231
|
+
return f
|
|
232
|
+
return registrator
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class BlobType:
|
|
236
|
+
def __init__(self, val):
|
|
237
|
+
self.val = val
|
|
238
|
+
|
|
239
|
+
def __str__(self):
|
|
240
|
+
return str(self.val)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
@formatter_for('BlobType')
|
|
244
|
+
def format_value_blob(val, colormap, **_):
|
|
245
|
+
bval = '0x' + val.hex()
|
|
246
|
+
return colorme(bval, colormap, 'blob')
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
formatter_for('bytearray')(format_value_blob)
|
|
250
|
+
formatter_for('buffer')(format_value_blob)
|
|
251
|
+
formatter_for('blob')(format_value_blob)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def format_python_formatted_type(val, colormap, color, quote=False):
|
|
255
|
+
bval = str(val)
|
|
256
|
+
if quote:
|
|
257
|
+
bval = "'%s'" % bval
|
|
258
|
+
return colorme(bval, colormap, color)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
@formatter_for('Decimal')
|
|
262
|
+
def format_value_decimal(val, float_precision, colormap, decimal_sep=None, thousands_sep=None, **_):
|
|
263
|
+
if (decimal_sep and decimal_sep != '.') or thousands_sep:
|
|
264
|
+
return format_floating_point_type(val, colormap, float_precision, decimal_sep, thousands_sep)
|
|
265
|
+
return format_python_formatted_type(val, colormap, 'decimal')
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
@formatter_for('UUID')
|
|
269
|
+
def format_value_uuid(val, colormap, **_):
|
|
270
|
+
return format_python_formatted_type(val, colormap, 'uuid')
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
formatter_for('timeuuid')(format_value_uuid)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
@formatter_for('inet')
|
|
277
|
+
def formatter_value_inet(val, colormap, quote=False, **_):
|
|
278
|
+
return format_python_formatted_type(val, colormap, 'inet', quote=quote)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
@formatter_for('bool')
|
|
282
|
+
def format_value_boolean(val, colormap, boolean_styles=None, **_):
|
|
283
|
+
if boolean_styles:
|
|
284
|
+
val = boolean_styles[0] if val else boolean_styles[1]
|
|
285
|
+
return format_python_formatted_type(val, colormap, 'boolean')
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
formatter_for('boolean')(format_value_boolean)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def format_floating_point_type(val, colormap, float_precision, decimal_sep=None, thousands_sep=None, **_):
|
|
292
|
+
if math.isnan(val):
|
|
293
|
+
bval = 'NaN'
|
|
294
|
+
elif math.isinf(val):
|
|
295
|
+
bval = 'Infinity' if val > 0 else '-Infinity'
|
|
296
|
+
else:
|
|
297
|
+
if thousands_sep:
|
|
298
|
+
dpart, ipart = math.modf(val)
|
|
299
|
+
bval = format_integer_with_thousands_sep(ipart, thousands_sep)
|
|
300
|
+
dpart_str = ('%.*f' % (float_precision, math.fabs(dpart)))[2:].rstrip('0')
|
|
301
|
+
if dpart_str:
|
|
302
|
+
bval += '%s%s' % ('.' if not decimal_sep else decimal_sep, dpart_str)
|
|
303
|
+
else:
|
|
304
|
+
exponent = int(math.log10(abs(val))) if abs(val) > sys.float_info.epsilon else -sys.maxsize - 1
|
|
305
|
+
if -4 <= exponent < float_precision:
|
|
306
|
+
# when this is true %g will not use scientific notation,
|
|
307
|
+
# increasing precision should not change this decision
|
|
308
|
+
# so we increase the precision to take into account the
|
|
309
|
+
# digits to the left of the decimal point
|
|
310
|
+
float_precision = float_precision + exponent + 1
|
|
311
|
+
bval = '%.*g' % (float_precision, val)
|
|
312
|
+
if decimal_sep:
|
|
313
|
+
bval = bval.replace('.', decimal_sep)
|
|
314
|
+
|
|
315
|
+
return colorme(bval, colormap, 'float')
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
formatter_for('float')(format_floating_point_type)
|
|
319
|
+
formatter_for('double')(format_floating_point_type)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def format_integer_type(val, colormap, thousands_sep=None, **_):
|
|
323
|
+
# base-10 only for now; support others?
|
|
324
|
+
bval = format_integer_with_thousands_sep(val, thousands_sep) if thousands_sep else str(val)
|
|
325
|
+
bval = str(bval)
|
|
326
|
+
return colorme(bval, colormap, 'int')
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def format_integer_with_thousands_sep(val, thousands_sep=','):
|
|
330
|
+
return "{:,.0f}".format(val).replace(',', thousands_sep)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
formatter_for('long')(format_integer_type)
|
|
334
|
+
formatter_for('int')(format_integer_type)
|
|
335
|
+
formatter_for('bigint')(format_integer_type)
|
|
336
|
+
formatter_for('varint')(format_integer_type)
|
|
337
|
+
formatter_for('duration')(format_integer_type)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
@formatter_for('datetime')
|
|
341
|
+
def format_value_timestamp(val, colormap, date_time_format, quote=False, **_):
|
|
342
|
+
if isinstance(val, datetime.datetime):
|
|
343
|
+
bval = strftime(date_time_format.timestamp_format,
|
|
344
|
+
calendar.timegm(val.utctimetuple()),
|
|
345
|
+
microseconds=val.microsecond,
|
|
346
|
+
timezone=date_time_format.timezone)
|
|
347
|
+
if date_time_format.milliseconds_only:
|
|
348
|
+
bval = round_microseconds(bval)
|
|
349
|
+
else:
|
|
350
|
+
bval = str(val)
|
|
351
|
+
|
|
352
|
+
if quote:
|
|
353
|
+
bval = "'%s'" % bval
|
|
354
|
+
return colorme(bval, colormap, 'timestamp')
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
formatter_for('timestamp')(format_value_timestamp)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def strftime(time_format, seconds, microseconds=0, timezone=None):
|
|
361
|
+
ret_dt = datetime_from_timestamp(seconds) + datetime.timedelta(microseconds=microseconds)
|
|
362
|
+
ret_dt = ret_dt.replace(tzinfo=UTC())
|
|
363
|
+
if timezone:
|
|
364
|
+
ret_dt = ret_dt.astimezone(timezone)
|
|
365
|
+
try:
|
|
366
|
+
return ret_dt.strftime(time_format)
|
|
367
|
+
except ValueError:
|
|
368
|
+
# CASSANDRA-13185: if the date cannot be formatted as a string, return a string with the milliseconds
|
|
369
|
+
# since the epoch. cqlsh does the exact same thing for values below datetime.MINYEAR (1) or above
|
|
370
|
+
# datetime.MAXYEAR (9999). Some versions of strftime() also have problems for dates between MIN_YEAR and 1900.
|
|
371
|
+
# cqlsh COPY assumes milliseconds from the epoch if it fails to parse a datetime string, and so it is
|
|
372
|
+
# able to correctly import timestamps exported as milliseconds since the epoch.
|
|
373
|
+
return '%d' % (seconds * 1000.0)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
microseconds_regex = re.compile(r"(.*)(?:\.(\d{1,6}))(.*)")
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def round_microseconds(val):
|
|
380
|
+
"""
|
|
381
|
+
For COPY TO, we need to round microsecond to milliseconds because server side
|
|
382
|
+
TimestampSerializer.dateStringPatterns only parses milliseconds. If we keep microseconds,
|
|
383
|
+
users may try to import with COPY FROM a file generated with COPY TO and have problems if
|
|
384
|
+
prepared statements are disabled, see CASSANDRA-11631.
|
|
385
|
+
"""
|
|
386
|
+
m = microseconds_regex.match(val)
|
|
387
|
+
if not m:
|
|
388
|
+
return val
|
|
389
|
+
|
|
390
|
+
milliseconds = int(m.group(2)) * pow(10, 3 - len(m.group(2)))
|
|
391
|
+
return '%s.%03d%s' % (m.group(1), milliseconds, '' if not m.group(3) else m.group(3))
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
@formatter_for('Date')
|
|
395
|
+
def format_value_date(val, colormap, **_):
|
|
396
|
+
return format_python_formatted_type(val, colormap, 'date')
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
@formatter_for('Time')
|
|
400
|
+
def format_value_time(val, colormap, **_):
|
|
401
|
+
return format_python_formatted_type(val, colormap, 'time')
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
@formatter_for('Duration')
|
|
405
|
+
def format_value_duration(val, colormap, **_):
|
|
406
|
+
return format_python_formatted_type(duration_as_str(val.months, val.days, val.nanoseconds), colormap, 'duration')
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def duration_as_str(months, days, nanoseconds):
|
|
410
|
+
builder = list()
|
|
411
|
+
if months < 0 or days < 0 or nanoseconds < 0:
|
|
412
|
+
builder.append('-')
|
|
413
|
+
|
|
414
|
+
remainder = append(builder, abs(months), MONTHS_PER_YEAR, "y")
|
|
415
|
+
append(builder, remainder, 1, "mo")
|
|
416
|
+
append(builder, abs(days), 1, "d")
|
|
417
|
+
|
|
418
|
+
if nanoseconds != 0:
|
|
419
|
+
remainder = append(builder, abs(nanoseconds), NANOS_PER_HOUR, "h")
|
|
420
|
+
remainder = append(builder, remainder, NANOS_PER_MINUTE, "m")
|
|
421
|
+
remainder = append(builder, remainder, NANOS_PER_SECOND, "s")
|
|
422
|
+
remainder = append(builder, remainder, NANOS_PER_MILLI, "ms")
|
|
423
|
+
remainder = append(builder, remainder, NANOS_PER_MICRO, "us")
|
|
424
|
+
append(builder, remainder, 1, "ns")
|
|
425
|
+
|
|
426
|
+
return ''.join(builder)
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def append(builder, dividend, divisor, unit):
|
|
430
|
+
if dividend == 0 or dividend < divisor:
|
|
431
|
+
return dividend
|
|
432
|
+
|
|
433
|
+
builder.append(str(dividend / divisor))
|
|
434
|
+
builder.append(unit)
|
|
435
|
+
return dividend % divisor
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
def decode_vint(buf):
|
|
439
|
+
return decode_zig_zag_64(decode_unsigned_vint(buf))
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def decode_unsigned_vint(buf):
|
|
443
|
+
"""
|
|
444
|
+
Cassandra vints are encoded differently than the varints used in protocol buffer.
|
|
445
|
+
The Cassandra vints are encoded with the most significant group first. The most significant byte will contains
|
|
446
|
+
the information about how many extra bytes need to be read as well as the most significant bits of the integer.
|
|
447
|
+
The number extra bytes to read is encoded as 1 bits on the left side.
|
|
448
|
+
For example, if we need to read 3 more bytes the first byte will start with 1110.
|
|
449
|
+
"""
|
|
450
|
+
|
|
451
|
+
first_byte = next(buf)
|
|
452
|
+
if (first_byte >> 7) == 0:
|
|
453
|
+
return first_byte
|
|
454
|
+
|
|
455
|
+
size = number_of_extra_bytes_to_read(first_byte)
|
|
456
|
+
retval = first_byte & (0xff >> size)
|
|
457
|
+
for i in range(size):
|
|
458
|
+
b = next(buf)
|
|
459
|
+
retval <<= 8
|
|
460
|
+
retval |= b & 0xff
|
|
461
|
+
|
|
462
|
+
return retval
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def number_of_extra_bytes_to_read(b):
|
|
466
|
+
return 8 - (~b & 0xff).bit_length()
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
def decode_zig_zag_64(n):
|
|
470
|
+
return (n >> 1) ^ -(n & 1)
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
@formatter_for('str')
|
|
474
|
+
def format_value_text(val, encoding, colormap, quote=False, **_):
|
|
475
|
+
escapedval = val.replace('\\', '\\\\')
|
|
476
|
+
if quote:
|
|
477
|
+
escapedval = escapedval.replace("'", "''")
|
|
478
|
+
escapedval = unicode_controlchars_re.sub(_show_control_chars, escapedval)
|
|
479
|
+
bval = escapedval
|
|
480
|
+
if quote:
|
|
481
|
+
bval = "'{}'".format(bval)
|
|
482
|
+
return bval if colormap is NO_COLOR_MAP else color_text(bval, colormap, wcwidth.wcswidth(bval))
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
# name alias
|
|
486
|
+
formatter_for('unicode')(format_value_text)
|
|
487
|
+
formatter_for('text')(format_value_text)
|
|
488
|
+
formatter_for('ascii')(format_value_text)
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
def format_simple_collection(val, cqltype, lbracket, rbracket, encoding,
|
|
492
|
+
colormap, date_time_format, float_precision, nullval,
|
|
493
|
+
decimal_sep, thousands_sep, boolean_styles):
|
|
494
|
+
subs = [format_value(sval, cqltype=stype, encoding=encoding, colormap=colormap,
|
|
495
|
+
date_time_format=date_time_format, float_precision=float_precision,
|
|
496
|
+
nullval=nullval, quote=True, decimal_sep=decimal_sep,
|
|
497
|
+
thousands_sep=thousands_sep, boolean_styles=boolean_styles)
|
|
498
|
+
for sval, stype in zip(val, cqltype.get_n_sub_types(len(val)))]
|
|
499
|
+
bval = lbracket + ', '.join(get_str(sval) for sval in subs) + rbracket
|
|
500
|
+
if colormap is NO_COLOR_MAP:
|
|
501
|
+
return bval
|
|
502
|
+
|
|
503
|
+
lb, sep, rb = [colormap['collection'] + s + colormap['reset']
|
|
504
|
+
for s in (lbracket, ', ', rbracket)]
|
|
505
|
+
coloredval = lb + sep.join(sval.coloredval for sval in subs) + rb
|
|
506
|
+
displaywidth = 2 * len(subs) + sum(sval.displaywidth for sval in subs)
|
|
507
|
+
return FormattedValue(bval, coloredval, displaywidth)
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
@formatter_for('list')
|
|
511
|
+
def format_value_list(val, cqltype, encoding, colormap, date_time_format, float_precision, nullval,
|
|
512
|
+
decimal_sep, thousands_sep, boolean_styles, **_):
|
|
513
|
+
return format_simple_collection(val, cqltype, '[', ']', encoding, colormap,
|
|
514
|
+
date_time_format, float_precision, nullval,
|
|
515
|
+
decimal_sep, thousands_sep, boolean_styles)
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
@formatter_for('tuple')
|
|
519
|
+
def format_value_tuple(val, cqltype, encoding, colormap, date_time_format, float_precision, nullval,
|
|
520
|
+
decimal_sep, thousands_sep, boolean_styles, **_):
|
|
521
|
+
return format_simple_collection(val, cqltype, '(', ')', encoding, colormap,
|
|
522
|
+
date_time_format, float_precision, nullval,
|
|
523
|
+
decimal_sep, thousands_sep, boolean_styles)
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
@formatter_for('set')
|
|
527
|
+
def format_value_set(val, cqltype, encoding, colormap, date_time_format, float_precision, nullval,
|
|
528
|
+
decimal_sep, thousands_sep, boolean_styles, **_):
|
|
529
|
+
return format_simple_collection(val, cqltype, '{', '}', encoding, colormap,
|
|
530
|
+
date_time_format, float_precision, nullval,
|
|
531
|
+
decimal_sep, thousands_sep, boolean_styles)
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
formatter_for('frozenset')(format_value_set)
|
|
535
|
+
formatter_for('sortedset')(format_value_set)
|
|
536
|
+
formatter_for('SortedSet')(format_value_set)
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
@formatter_for('dict')
|
|
540
|
+
def format_value_map(val, cqltype, encoding, colormap, date_time_format, float_precision, nullval,
|
|
541
|
+
decimal_sep, thousands_sep, boolean_styles, **_):
|
|
542
|
+
def subformat(v, t):
|
|
543
|
+
return format_value(v, cqltype=t, encoding=encoding, colormap=colormap,
|
|
544
|
+
date_time_format=date_time_format, float_precision=float_precision,
|
|
545
|
+
nullval=nullval, quote=True, decimal_sep=decimal_sep,
|
|
546
|
+
thousands_sep=thousands_sep, boolean_styles=boolean_styles)
|
|
547
|
+
|
|
548
|
+
subs = [(subformat(k, cqltype.sub_types[0]), subformat(v, cqltype.sub_types[1])) for (k, v) in sorted(val.items())]
|
|
549
|
+
bval = '{' + ', '.join(get_str(k) + ': ' + get_str(v) for (k, v) in subs) + '}'
|
|
550
|
+
if colormap is NO_COLOR_MAP:
|
|
551
|
+
return bval
|
|
552
|
+
|
|
553
|
+
lb, comma, colon, rb = [colormap['collection'] + s + colormap['reset']
|
|
554
|
+
for s in ('{', ', ', ': ', '}')]
|
|
555
|
+
coloredval = lb \
|
|
556
|
+
+ comma.join(k.coloredval + colon + v.coloredval for (k, v) in subs) \
|
|
557
|
+
+ rb
|
|
558
|
+
displaywidth = 4 * len(subs) + sum(k.displaywidth + v.displaywidth for (k, v) in subs)
|
|
559
|
+
return FormattedValue(bval, coloredval, displaywidth)
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
formatter_for('OrderedDict')(format_value_map)
|
|
563
|
+
formatter_for('OrderedMap')(format_value_map)
|
|
564
|
+
formatter_for('OrderedMapSerializedKey')(format_value_map)
|
|
565
|
+
formatter_for('map')(format_value_map)
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
def format_value_utype(val, cqltype, encoding, colormap, date_time_format, float_precision, nullval,
|
|
569
|
+
decimal_sep, thousands_sep, boolean_styles, **_):
|
|
570
|
+
def format_field_value(v, t):
|
|
571
|
+
if v is None:
|
|
572
|
+
return colorme(nullval, colormap, 'error')
|
|
573
|
+
return format_value(v, cqltype=t, encoding=encoding, colormap=colormap,
|
|
574
|
+
date_time_format=date_time_format, float_precision=float_precision,
|
|
575
|
+
nullval=nullval, quote=True, decimal_sep=decimal_sep,
|
|
576
|
+
thousands_sep=thousands_sep, boolean_styles=boolean_styles)
|
|
577
|
+
|
|
578
|
+
def format_field_name(name):
|
|
579
|
+
return format_value_text(name, encoding=encoding, colormap=colormap, quote=False)
|
|
580
|
+
|
|
581
|
+
subs = [(format_field_name(k), format_field_value(v, t)) for ((k, v), t) in zip(list(val._asdict().items()),
|
|
582
|
+
cqltype.sub_types)]
|
|
583
|
+
bval = '{' + ', '.join(get_str(k) + ': ' + get_str(v) for (k, v) in subs) + '}'
|
|
584
|
+
if colormap is NO_COLOR_MAP:
|
|
585
|
+
return bval
|
|
586
|
+
|
|
587
|
+
lb, comma, colon, rb = [colormap['collection'] + s + colormap['reset']
|
|
588
|
+
for s in ('{', ', ', ': ', '}')]
|
|
589
|
+
coloredval = lb \
|
|
590
|
+
+ comma.join(k.coloredval + colon + v.coloredval for (k, v) in subs) \
|
|
591
|
+
+ rb
|
|
592
|
+
displaywidth = 4 * len(subs) + sum(k.displaywidth + v.displaywidth for (k, v) in subs)
|
|
593
|
+
return FormattedValue(bval, coloredval, displaywidth)
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
NANOS_PER_MICRO = 1000
|
|
597
|
+
NANOS_PER_MILLI = 1000 * NANOS_PER_MICRO
|
|
598
|
+
NANOS_PER_SECOND = 1000 * NANOS_PER_MILLI
|
|
599
|
+
NANOS_PER_MINUTE = 60 * NANOS_PER_SECOND
|
|
600
|
+
NANOS_PER_HOUR = 60 * NANOS_PER_MINUTE
|
|
601
|
+
MONTHS_PER_YEAR = 12
|