velocity-python 0.9.7__py3-none-any.whl → 0.9.9__py3-none-any.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.
- velocity/__init__.py +1 -1
- velocity/aws/handlers/lambda_handler.py +8 -2
- velocity/misc/export.py +79 -10
- {velocity_python-0.9.7.dist-info → velocity_python-0.9.9.dist-info}/METADATA +1 -1
- {velocity_python-0.9.7.dist-info → velocity_python-0.9.9.dist-info}/RECORD +9 -9
- {velocity_python-0.9.7.dist-info → velocity_python-0.9.9.dist-info}/WHEEL +0 -0
- {velocity_python-0.9.7.dist-info → velocity_python-0.9.9.dist-info}/entry_points.txt +0 -0
- {velocity_python-0.9.7.dist-info → velocity_python-0.9.9.dist-info}/licenses/LICENSE +0 -0
- {velocity_python-0.9.7.dist-info → velocity_python-0.9.9.dist-info}/top_level.txt +0 -0
velocity/__init__.py
CHANGED
|
@@ -346,7 +346,11 @@ class LambdaHandler(BaseHandler):
|
|
|
346
346
|
return dict(user) if user else None
|
|
347
347
|
|
|
348
348
|
def afterAction(self, tx, context):
|
|
349
|
-
|
|
349
|
+
# As in beforeAction: a bare LambdaHandler (an SNS or scheduled
|
|
350
|
+
# handler, no WebHandler) has no activity tracking to finish.
|
|
351
|
+
enhanced = getattr(self, "_enhanced_after_action", None)
|
|
352
|
+
if callable(enhanced):
|
|
353
|
+
enhanced(tx, context)
|
|
350
354
|
logger.debug("starting LamdaHandler.afterAction")
|
|
351
355
|
|
|
352
356
|
self.end = time.time()
|
|
@@ -359,7 +363,9 @@ class LambdaHandler(BaseHandler):
|
|
|
359
363
|
)
|
|
360
364
|
|
|
361
365
|
def onError(self, tx, context, exc, tb):
|
|
362
|
-
self
|
|
366
|
+
enhanced = getattr(self, "_enhanced_error_handler", None)
|
|
367
|
+
if callable(enhanced):
|
|
368
|
+
enhanced(tx, context, exc, tb)
|
|
363
369
|
logger.debug("starting LamdaHandler.aws_api_activity")
|
|
364
370
|
request_id = getattr(self.aws_context, "aws_request_id", None)
|
|
365
371
|
logger.error(
|
velocity/misc/export.py
CHANGED
|
@@ -95,6 +95,68 @@ def _format_data_row(ws) -> None:
|
|
|
95
95
|
cell.number_format = code
|
|
96
96
|
|
|
97
97
|
|
|
98
|
+
class Formula(str):
|
|
99
|
+
"""A cell value written as a live spreadsheet formula (``Formula("=SUM(B2:B9)")``).
|
|
100
|
+
|
|
101
|
+
Every writer here writes a plain ``str`` as literal text, even one that
|
|
102
|
+
starts with ``=``: exported data often holds user-entered text, and a value
|
|
103
|
+
such as ``=HYPERLINK(...)`` must not become a formula when the file is
|
|
104
|
+
opened. Wrap the values meant as formulas in ``Formula`` to opt in.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
__slots__ = ()
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# Leading characters a spreadsheet application may read as the start of a
|
|
111
|
+
# formula when it opens a CSV/TSV file.
|
|
112
|
+
FORMULA_TRIGGERS = frozenset("=+-@\t\r")
|
|
113
|
+
|
|
114
|
+
_PLAIN_NUMBER = re.compile(r"[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def is_formula_text(value) -> bool:
|
|
118
|
+
"""True when ``value`` is a ``str`` (not a :class:`Formula`) that starts
|
|
119
|
+
with one of :data:`FORMULA_TRIGGERS`."""
|
|
120
|
+
return (
|
|
121
|
+
isinstance(value, str)
|
|
122
|
+
and not isinstance(value, Formula)
|
|
123
|
+
and value[:1] in FORMULA_TRIGGERS
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def csv_safe(value):
|
|
128
|
+
"""``value`` as a CSV/TSV cell should hold it: a string a spreadsheet
|
|
129
|
+
would read as a formula (see :data:`FORMULA_TRIGGERS`) gets a leading
|
|
130
|
+
``'`` so it opens as text. A plain signed number (``"-12.50"``), a
|
|
131
|
+
:class:`Formula` and non-strings are returned unchanged."""
|
|
132
|
+
if is_formula_text(value) and not _PLAIN_NUMBER.fullmatch(value):
|
|
133
|
+
return "'" + value
|
|
134
|
+
return value
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _csv_row(row) -> list:
|
|
138
|
+
return [csv_safe(v) for v in row]
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _append(ws, values) -> None:
|
|
142
|
+
"""``ws.append(values)``, then every cell openpyxl typed as a formula
|
|
143
|
+
from a plain ``str`` is written as text instead: only a :class:`Formula`
|
|
144
|
+
stays a formula."""
|
|
145
|
+
values = list(values)
|
|
146
|
+
ws.append(values)
|
|
147
|
+
if not values:
|
|
148
|
+
return
|
|
149
|
+
# openpyxl stores the value as a plain str, so the Formula marker is read
|
|
150
|
+
# from the values passed in, by position.
|
|
151
|
+
for cell, value in zip(ws[ws.max_row], values):
|
|
152
|
+
if (
|
|
153
|
+
cell.data_type == "f"
|
|
154
|
+
and isinstance(value, str)
|
|
155
|
+
and not isinstance(value, Formula)
|
|
156
|
+
):
|
|
157
|
+
cell.data_type = "s"
|
|
158
|
+
|
|
159
|
+
|
|
98
160
|
def extract(d: dict, keys: List[str]) -> List:
|
|
99
161
|
"""Extract values from a dictionary based on a list of keys."""
|
|
100
162
|
return [d.get(key) for key in keys]
|
|
@@ -247,6 +309,8 @@ def create_spreadsheet(
|
|
|
247
309
|
Timezone-aware values are written as the wall time in
|
|
248
310
|
``display_timezone`` (a ``tzinfo`` or IANA name; UTC when ``None``),
|
|
249
311
|
since Excel cells carry no zone (see :func:`resolve_display_timezone`).
|
|
312
|
+
A ``str`` is always written as text, even one starting with ``=``; wrap
|
|
313
|
+
a value in :class:`Formula` to write it as a formula.
|
|
250
314
|
"""
|
|
251
315
|
import openpyxl
|
|
252
316
|
from openpyxl.styles import Alignment, Border, Font, NamedStyle, Side
|
|
@@ -313,9 +377,9 @@ def create_spreadsheet(
|
|
|
313
377
|
wb.add_named_style(style)
|
|
314
378
|
|
|
315
379
|
# Add headers and rows
|
|
316
|
-
ws
|
|
380
|
+
_append(ws, headers)
|
|
317
381
|
for row in rows:
|
|
318
|
-
ws
|
|
382
|
+
_append(ws, _excel_row(row, zone))
|
|
319
383
|
_format_data_row(ws)
|
|
320
384
|
|
|
321
385
|
# Set freeze panes
|
|
@@ -392,6 +456,8 @@ def create_workbook(
|
|
|
392
456
|
unless their column has a ``number_formats`` entry. Timezone-aware
|
|
393
457
|
values are written as the wall time in ``display_timezone`` (a
|
|
394
458
|
``tzinfo`` or IANA name; UTC when ``None``): Excel cells carry no zone.
|
|
459
|
+
A ``str`` is written as text, even one starting with ``=``; a
|
|
460
|
+
:class:`Formula` is written as a formula.
|
|
395
461
|
- ``number_formats`` — ``{header: format_code}`` applied to that column's
|
|
396
462
|
data cells (e.g. ``{"Total Sales": "$#,##0.00"}``); wins over the
|
|
397
463
|
date/time defaults.
|
|
@@ -414,12 +480,12 @@ def create_workbook(
|
|
|
414
480
|
ws = wb.create_sheet(title=sheet_title(spec["title"], used))
|
|
415
481
|
headers = list(spec.get("headers") or [])
|
|
416
482
|
if headers:
|
|
417
|
-
ws
|
|
483
|
+
_append(ws, headers)
|
|
418
484
|
if spec.get("bold_header", True):
|
|
419
485
|
for cell in ws[1]:
|
|
420
486
|
cell.font = Font(bold=True)
|
|
421
487
|
for row in spec.get("rows") or ():
|
|
422
|
-
ws
|
|
488
|
+
_append(ws, [_sheet_cell(v, zone) for v in row])
|
|
423
489
|
_format_data_row(ws)
|
|
424
490
|
formats = spec.get("number_formats") or {}
|
|
425
491
|
if formats:
|
|
@@ -586,8 +652,8 @@ def _render(
|
|
|
586
652
|
if result_format in ("csv", "tsv"):
|
|
587
653
|
buf = StringIO()
|
|
588
654
|
writer = csv.writer(buf, delimiter="\t" if result_format == "tsv" else ",")
|
|
589
|
-
writer.writerow(headers)
|
|
590
|
-
writer.writerows(rows)
|
|
655
|
+
writer.writerow(_csv_row(headers))
|
|
656
|
+
writer.writerows(_csv_row(row) for row in rows)
|
|
591
657
|
return buf.getvalue().encode("utf-8")
|
|
592
658
|
if result_format == "jsonl":
|
|
593
659
|
out = BytesIO()
|
|
@@ -645,9 +711,9 @@ def _render(
|
|
|
645
711
|
zone = resolve_display_timezone(display_timezone)
|
|
646
712
|
wb = Workbook()
|
|
647
713
|
ws = wb.active
|
|
648
|
-
ws
|
|
714
|
+
_append(ws, headers)
|
|
649
715
|
for row in rows:
|
|
650
|
-
ws
|
|
716
|
+
_append(ws, _excel_row(row, zone))
|
|
651
717
|
_format_data_row(ws)
|
|
652
718
|
buf = BytesIO()
|
|
653
719
|
wb.save(buf)
|
|
@@ -674,8 +740,11 @@ def build_buffer(
|
|
|
674
740
|
HTML cell text is escaped and Markdown ``|`` is escaped. Excel/XLSX cells
|
|
675
741
|
stay native; date/time cells display human readable on a 12-hour clock,
|
|
676
742
|
and timezone-aware values are written as the wall time in
|
|
677
|
-
``display_timezone`` (a ``tzinfo`` or IANA name; UTC when ``None``).
|
|
678
|
-
|
|
743
|
+
``display_timezone`` (a ``tzinfo`` or IANA name; UTC when ``None``).
|
|
744
|
+
Spreadsheet formula injection is refused: an XLSX ``str`` cell is always
|
|
745
|
+
text (only a :class:`Formula` is a formula), and a CSV/TSV string that
|
|
746
|
+
starts with ``= + - @``, tab or CR gets a leading ``'`` (:func:`csv_safe`).
|
|
747
|
+
The other text formats write values as given.
|
|
679
748
|
"""
|
|
680
749
|
spec = EXPORT_FORMATS.get(result_format)
|
|
681
750
|
if spec is None:
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
velocity/__init__.py,sha256=
|
|
1
|
+
velocity/__init__.py,sha256=PP7GMkXGxAUUaFlMwjCwn7mB2CVTVBOg8lSnIaLfsmA,362
|
|
2
2
|
velocity/cache.py,sha256=pthvw7gDVEXRzkTcQtA5YVf4Q1IxAamWsba4UReT0RE,8933
|
|
3
3
|
velocity/hooks.py,sha256=jY_qtAapNHvptmryFSD-Zc7RvLhV6e5nJEH3x2xTgCw,20575
|
|
4
4
|
velocity/logging.py,sha256=MAsZap1cDSdOBNRc_9ezQxup5dHOS0ARxU9NI_-UK3E,4037
|
|
@@ -56,7 +56,7 @@ velocity/aws/handlers/base_handler.py,sha256=1A_EO_71clM3-h-hAIUJxs_2LL7w3kkwj9N
|
|
|
56
56
|
velocity/aws/handlers/context.py,sha256=Yrq-vtl2-0WeoTaFVMuKOQRM2Cnyq21rNvVtUvGjxx0,61416
|
|
57
57
|
velocity/aws/handlers/context_factory.py,sha256=8rhYKrylzwQxBFqBckmF7jgWGZhfomLOT_Yu404CtJQ,784
|
|
58
58
|
velocity/aws/handlers/exceptions.py,sha256=J-4VkiKASE-z2X-sr5EUq7FWv3x7fzR7Yl536G5VeZc,5026
|
|
59
|
-
velocity/aws/handlers/lambda_handler.py,sha256=
|
|
59
|
+
velocity/aws/handlers/lambda_handler.py,sha256=yphzgJ6w7tCCvdvwKZCHMaDQhvM3XVeURxeBjdPIpe4,22146
|
|
60
60
|
velocity/aws/handlers/masquerade.py,sha256=ZMXGGB-w7pXRV8Fq0qBmJOL1Eq55hXP3Rq3dWjJk6Ck,5157
|
|
61
61
|
velocity/aws/handlers/perf.py,sha256=zLAcr_IrvpnZS1zzzXi-YhopRgCv3OeetQgOx_S43Xw,1421
|
|
62
62
|
velocity/aws/handlers/response.py,sha256=Xi5mzBPiYC-VVS3qFyCgfuV3YyuGuhS1O54QGI7jGHs,17201
|
|
@@ -181,7 +181,7 @@ velocity/metadata/merge.py,sha256=asV1fpR8_46ol6s-EAOsWxYcZe5Ot1enlO_UFu2MUjk,95
|
|
|
181
181
|
velocity/metadata/validate.py,sha256=EgIF_7GQ-sSCGdruq-8QPaLK-JlEwoHkhZGqahHnucE,3330
|
|
182
182
|
velocity/misc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
183
183
|
velocity/misc/db.py,sha256=MPgt-kkukKR_Wh_S_5W-MyDgaeoZ4YLoDJ54wU2ppm4,2830
|
|
184
|
-
velocity/misc/export.py,sha256=
|
|
184
|
+
velocity/misc/export.py,sha256=O9UwQL6MMzpiPrb4Xo_a3lQR-4zHPsRS4Ha21KmU-Y0,27325
|
|
185
185
|
velocity/misc/format.py,sha256=YHkmeUeBPBaM3auwZYu3z4-qCGTfQiNJNC1CjaqDgs0,4332
|
|
186
186
|
velocity/misc/mail.py,sha256=h-CbGVCNLpIol3PIkCPcySomTxJivBg0rDxZZhstNKQ,20097
|
|
187
187
|
velocity/misc/merge.py,sha256=J5WIKWLs6t1jZg0BNiI0OJ962mYa16liHlBwxlFSmK8,2144
|
|
@@ -258,9 +258,9 @@ velocity/workflow/points.py,sha256=g_vQ3cdHdx_JloVCS5dVjo_jJoSolO92K5p3suDzxlE,1
|
|
|
258
258
|
velocity/workflow/schema.py,sha256=1ZAkGZ_4YrTAfY6TIPCviWQJGLhhmAJmkxisfLyy2og,21597
|
|
259
259
|
velocity/workflow/traveler.py,sha256=Ik1InoEo69sZB1wVV-UinVo4P0P5QlcV_hh858p3OWE,20562
|
|
260
260
|
velocity/workflow/views.py,sha256=oTPjpuheND8y4VbcTY8c-4fMdSHKb0jgGzXMuT_G7_s,32175
|
|
261
|
-
velocity_python-0.9.
|
|
262
|
-
velocity_python-0.9.
|
|
263
|
-
velocity_python-0.9.
|
|
264
|
-
velocity_python-0.9.
|
|
265
|
-
velocity_python-0.9.
|
|
266
|
-
velocity_python-0.9.
|
|
261
|
+
velocity_python-0.9.9.dist-info/licenses/LICENSE,sha256=aoN245GG8s9oRUU89KNiGTU4_4OtnNmVi4hQeChg6rM,1076
|
|
262
|
+
velocity_python-0.9.9.dist-info/METADATA,sha256=RUZxKrl-7g2uQ0co30nmIZWAvmSaaq9JeNhr6xAVm7I,17789
|
|
263
|
+
velocity_python-0.9.9.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
264
|
+
velocity_python-0.9.9.dist-info/entry_points.txt,sha256=nXUKJHkOGQCSPoagdmMNd_qycrle1LVU-GhYknLGM1M,56
|
|
265
|
+
velocity_python-0.9.9.dist-info/top_level.txt,sha256=JW2vJPmodgdgSz7H6yoZvnxF8S3fTMIv-YJWCT1sNW0,9
|
|
266
|
+
velocity_python-0.9.9.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|