forked from grommunio/admin-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
384 lines (329 loc) · 13.9 KB
/
Copy pathcommon.py
File metadata and controls
384 lines (329 loc) · 13.9 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: AGPL-3.0-or-later
# SPDX-FileCopyrightText: 2021 grommunio GmbH
def domainFilter(domainSpec, *filters):
from orm.domains import Domains
from sqlalchemy import and_
return and_(True if domainSpec is None else
Domains.ID == domainSpec if domainSpec.isdigit() else
Domains.domainname.ilike(domainSpec+"%"), *filters)
def domainCandidates(domainSpec, *filters):
from orm.domains import Domains
return Domains.query.filter(domainFilter(domainSpec, *filters))
def userFilter(userSpec, *filters):
from orm.users import Users
from sqlalchemy import and_
return and_(True if userSpec is None else
Users.ID == userSpec if userSpec.isdigit() else
Users.username.ilike(userSpec+"%"), *filters)
def userCandidates(userSpec, *filters):
from orm.users import Users
return Users.query.filter(userFilter(userSpec, *filters))
def userspecAutocomp(prefix, **kwargs):
from . import Cli
if Cli.rlAvail:
from orm.users import Users
return (user.username for user in userCandidates(prefix).with_entities(Users.username))
else:
return ()
class NotFound(dict):
pass
def getKey(c, keyspec):
if keyspec:
for key in keyspec:
c = c.get(key, NotFound()) if key else c
return c
def proptagCompleter(prefix, addSuffix="", **kwargs):
from tools.constants import PropTags
PropTags.lookup(None)
c = []
if prefix == "" or prefix[0].islower():
c += [tag.lower()+addSuffix for value, tag in PropTags._lookup.items() if isinstance(value, int)]
if prefix == "" or prefix[0].isupper():
c += [tag.upper()+addSuffix for value, tag in PropTags._lookup.items() if isinstance(value, int)]
if prefix == "" or prefix[0] == "0" and (len(prefix) <= 2 or not prefix[2:].isupper()):
c += ["0x{:08x}{}".format(value, addSuffix) for value in PropTags._lookup.keys() if isinstance(value, int)]
if prefix == "" or prefix[0] == "0" and (len(prefix) <= 2 or not prefix[2:].islower()):
c += ["0x{:08X}{}".format(value, addSuffix) for value in PropTags._lookup.keys() if isinstance(value, int)]
if prefix == "" or prefix.isnumeric():
c += [str(value)+addSuffix for value in PropTags._lookup.keys() if isinstance(value, int)]
return c
class Table:
"""Helper class for pretty printing of tables."""
class Styled:
"""Class to manage style information of a table cell"""
stylemarker = None
def __init__(self, data, align='a', color=None, on_color=None, attrs=[]):
"""Associate style information with data.
Parameters
----------
data : any
Data to display
align : str, optional
Alignment, can be one of 'a' (auto), 'c' (center), 'l' (left) or 'r' (right).
Automatic alignment chooses 'r' for numbers and 'l' for everything else.
The default is 'a'.
color : str, optional
Color to apply to the text. The default is None.
on_color : str, optional
Background color of the text. The default is None.
attrs : [str], optional
List of additional style attributes. The default is [].
"""
self._init()
self.align = align if align != "a" else "r" if type(data) in (int, float) else "l"
self.raw = data
self.data = str(data).expandtabs()
self.color = color
self.on_color = None
self.attrs = attrs
self.width = self._width()
@classmethod
def _init(cls):
"""Initialize stylemarker re."""
if cls.stylemarker is not None:
return
import re
cls.stylemarker = re.compile("\x1b\\[[\\d]{1,2}m")
def _cut(self, width):
"""Cut cell content to specified width.
Has no effect if width is larger than cell content.
If content is larger than width, the content is truncated, replacing the last char with an ellipsis,
preserving any terminal style codes present.
Parameters
----------
width : int
Maximum cell width
Returns
-------
str
Cell content, truncated if necessary
"""
if self.width <= width:
return self.data
if width <= 1:
return "…"
markers = self.stylemarker.findall(self.data)
if not markers:
return self.data[:width-1]+"…"
text = self.stylemarker.split(self.data)
cutIdx = -1
overshoot = self.width-width+1
while overshoot > 0:
if len(text[cutIdx]) < overshoot:
overshoot -= len(text[cutIdx])
text[cutIdx] = ""
else:
text[cutIdx] = text[cutIdx][:-overshoot]+"…"
overshoot = 0
cutIdx -= 1
segments = [None]*(len(markers)+len(text))
segments[::2] = text
segments[1::2] = markers
return "".join(segments)
def _width(self):
"""Return effective width of the string (without style markers and expanded tabs)."""
return len(self.stylemarker.sub("", self.data).expandtabs())
def print(self, cli, width, last):
"""Print styled data into string.
Parameters
----------
cli : Cli
Cli providing style formatting.
width : int
Width of the cell to fill
last : bool
Whether this is the last cell of the row
Returns
-------
data : str
Cell content
"""
data = self._cut(width)
pad = width-self.width
data = cli.col(data, self.color, self.on_color, self.attrs)
if self.align == "r":
data = " "*pad+data
elif self.align == "c":
data = " "*(pad//2)+data+(" "*((pad+1)//2) if not last else "")
elif not last:
data += " "*pad
return data
FORMATS = ("csv", "json-flat", "json-kv", "json-object", "json-structured", "pretty")
def __init__(self, data, header=None, colsep=None, empty=None):
"""Create table from data.
If colsep is not specified, the default column separator is chosen by
the output formatter (' ' for pretty, ',' for csv).
Parameters
----------
data : [[any]]
Matrix of contents
header : [any], optional
Table header. The default is None.
colsep : str, optional
Column separator. The default is None.
empty : str, optional
Text to display when table does not contain data. The default is None.
"""
self.data = [[self._styled(cell) for cell in row] for row in data] if data else None
self.header = [self._styled(col, "l", attrs=["underline"]) for col in header] if header else None
self.empty = empty
if not (header or data):
return
self.colsep = colsep
head = self.header or self.data[0]
self.columns = max(max(len(row) for row in data) if data else 0, len(head))
self.colwidth = tuple(head[i].width if i < len(head) else 0 for i in range(self.columns))
if self.data:
for line in self.data:
self.colwidth = tuple(max(self.colwidth[i], line[i].width if i < len(line) else 0)
for i in range(self.columns))
@classmethod
def _styled(cls, data, *args, **kwargs):
"""Augment data with style information.
Parameters
----------
data : any
Data to wrap
*args : any
Arguments passed on to Styled constructor
**kwargs : any
Keyword arguments passed on to Styled constructor.
Returns
-------
Styled
Data with style information
"""
return data if isinstance(data, cls.Styled) else cls.Styled(data, *args, **kwargs)
def _narrow(self):
"""Recalculate column width to fit table to terminal.
If the terminal is wider than the table or terminal width cannot be determined, no changes to column width are made.
Otherwise, columns are truncated, longest to shortest, until the table fits the terminal.
Returns
-------
list[int]
List of adjusted column widths
"""
import shutil
colsep = self.colsep or " "
sepWidth = len(colsep)*(self.columns-1) # total width occupied by separators
contentWidth = sum(self.colwidth)
termWidth = shutil.get_terminal_size((0, 0)).columns
if termWidth == 0 or contentWidth+sepWidth <= termWidth:
return self.colwidth
if termWidth <= sepWidth+self.columns: # each column would be reduced to 1 anyway
return tuple(1 for _ in range(self.columns))
widthIdx = sorted(zip(self.colwidth, range(self.columns)), reverse=True)
termWidth -= sepWidth
if termWidth <= self.columns*widthIdx[-1][0]: # smallest column width is still too wide
colwidth = [termWidth//self.columns]*self.columns
else: # cut the longest columns until it fits
narrowIdx = 1 # index in widthIdx up to which narrowing is performed
while termWidth < contentWidth - sum(w[0]-widthIdx[narrowIdx][0] for w in widthIdx[0:narrowIdx]):
narrowIdx += 1
narrowedWidth = (termWidth-sum(w[0] for w in widthIdx[narrowIdx:])) // narrowIdx
colwidth = [min(w, narrowedWidth) for w in self.colwidth]
for i in range(termWidth-sum(colwidth)): # number of columns that can be wider by 1 char
colwidth[widthIdx[i][1]] += 1
return colwidth
def printline(self, cli, line, colwidth):
"""Print a single row of data.
Parameters
----------
cli : Cli
Cli providing printing functionality
line : [Styled]
List of cells to print
"""
colsep = self.colsep or " "
cli.print(colsep.join(line[i].print(cli, colwidth[i], i == self.columns-1) for i in range(len(line))))
def print(self, cli):
"""Print the table.
Parameters
----------
cli : Cli
Cli providing printing functionality.
"""
if not self.data and self.empty:
cli.print(self.empty)
return
colwidth = self._narrow()
if self.header:
self.printline(cli, self.header, colwidth)
if self.data:
for line in self.data:
self.printline(cli, line, colwidth)
def csv(self, cli):
"""Output table as csv.
Parameters
----------
cli : Cli
Cli providing printing functionality.
"""
import csv
if not (self.header or self.data):
return
header = [cell.raw for cell in self.header] if self.header else [str(i) for i in range(len(self.data[0]))]
writer = csv.DictWriter(cli.stdout, fieldnames=header, delimiter=self.colsep or ",")
if self.header:
writer.writeheader()
if not self.data:
return
for row in self.data:
writer.writerow({name: value.raw for name, value in zip(header, row)})
def json(self, cli, structured):
"""Output table as JSON
Parameters
----------
cli : Cli
Cli providing printing functionality.
structured : bool
Whether to output data as structured JSON or array-of-arrays
"""
import json
if not self.data:
cli.print("[]")
return
header = [cell.raw for cell in self.header] if self.header else [str(i) for i in range(len(self.data[0]))]
data = [{name: value.raw for name, value in zip(header, row)} for row in self.data] if structured else\
[[cell.raw for cell in row] for row in self.data]
cli.print(json.dumps(data, default=lambda x: str(x), separators=(",", ":")))
def jsonObject(self, cli, full):
"""Create single JSON object with the first column as key
If full is True, the remaining columns are packed into an object, (like json-structured),
otherwise only the second column is used as value and any additional columns are ignored.
Parameters
----------
cli : Cli
Cli providing printing functionality
full : bool
Whether to provide value as object
"""
import json
def getV(row):
return None if len(row) < 2 else row[1].raw
def getO(row):
return {name.raw: value.raw for name, value in zip(self.header[1:], row[1:])}
if not self.data:
cli.print("{}")
return
mkValue = getO if full else getV
data = {str(row[0].raw): mkValue(row) for row in self.data}
cli.print(json.dumps(data, default=lambda x: str(x), separators=(",", ":")))
def dump(self, cli, format):
"""Dump table contents in specified format
Parameters
----------
cli : Cli
Cli providing printing functionality.
format : str
Output format. Can be one of `csv`, `json-flat` and `json-structured`. Everything else will print the table.
"""
if format == "csv":
self.csv(cli)
elif format in ("json-flat", "json-structured"):
self.json(cli, format == "json-structured")
elif format in ("json-kv", "json-object"):
self.jsonObject(cli, format == "json-object")
else:
self.print(cli)