Skip to content

Commit 619cea8

Browse files
committed
sqlite-utils convert --pdb option, closes #581
1 parent 5e9a021 commit 619cea8

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

docs/cli-reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ See :ref:`cli_convert`.
648648
--drop Drop original column afterwards
649649
--no-skip-false Don't skip falsey values
650650
-s, --silent Don't show a progress bar
651+
--pdb Open pdb debugger on first error
651652
-h, --help Show this message and exit.
652653

653654

docs/cli.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,44 @@ This supports nested imports as well, for example to use `ElementTree <https://d
17321732
'xml.etree.ElementTree.fromstring(value).attrib["title"]' \
17331733
--import=xml.etree.ElementTree
17341734
1735+
.. _cli_convert_debugger:
1736+
1737+
Using the debugger
1738+
------------------
1739+
1740+
If an error occurs while running your conversion operation you may see a message like this::
1741+
1742+
user-defined function raised exception
1743+
1744+
Add the ``--pdb`` option to catch the error and open the Python debugger at that point. The conversion operation will exit after you type ``q`` in the debugger.
1745+
1746+
Here's an example debugging session. First, create a ``articles`` table with invalid XML in the ``content`` column:
1747+
1748+
.. code-block:: bash
1749+
1750+
echo '{"content": "This is not XML"}' | sqlite-utils insert content.db articles -
1751+
1752+
Now run the conversion with the ``--pdb`` option:
1753+
1754+
.. code-block:: bash
1755+
1756+
sqlite-utils convert content.db articles content \
1757+
'xml.etree.ElementTree.fromstring(value).attrib["title"]' \
1758+
--import=xml.etree.ElementTree \
1759+
--pdb
1760+
1761+
When the error occurs the debugger will open::
1762+
1763+
Exception raised, dropping into pdb...: syntax error: line 1, column 0
1764+
> .../python3.11/xml/etree/ElementTree.py(1338)XML()
1765+
-> parser.feed(text)
1766+
(Pdb) args
1767+
text = 'This is not XML'
1768+
parser = <xml.etree.ElementTree.XMLParser object at 0x102c405e0>
1769+
(Pdb) q
1770+
1771+
``args`` here shows the arguments to the current function in the stack. The Python `pdb documentation <https://docs.python.org/3/library/pdb.html#debugger-commands>`__ has full details on the other available commands.
1772+
17351773
.. _cli_convert_complex:
17361774

17371775
Defining a convert() function

sqlite_utils/cli.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import itertools
1717
import json
1818
import os
19+
import pdb
1920
import sys
2021
import csv as csv_std
2122
import tabulate
@@ -2901,6 +2902,7 @@ def _generate_convert_help():
29012902
@click.option("--drop", is_flag=True, help="Drop original column afterwards")
29022903
@click.option("--no-skip-false", is_flag=True, help="Don't skip falsey values")
29032904
@click.option("-s", "--silent", is_flag=True, help="Don't show a progress bar")
2905+
@click.option("pdb_", "--pdb", is_flag=True, help="Open pdb debugger on first error")
29042906
def convert(
29052907
db_path,
29062908
table,
@@ -2916,6 +2918,7 @@ def convert(
29162918
drop,
29172919
no_skip_false,
29182920
silent,
2921+
pdb_,
29192922
):
29202923
sqlite3.enable_callback_tracebacks(True)
29212924
db = sqlite_utils.Database(db_path)
@@ -2968,6 +2971,19 @@ def preview(v):
29682971
)
29692972
click.echo("Would affect {} row{}".format(count, "" if count == 1 else "s"))
29702973
else:
2974+
# Wrap fn with a thing that will catch errors and optionally drop into pdb
2975+
if pdb_:
2976+
fn_ = fn
2977+
2978+
def wrapped_fn(value):
2979+
try:
2980+
return fn_(value)
2981+
except Exception as ex:
2982+
print("\nException raised, dropping into pdb...:", ex)
2983+
pdb.post_mortem(ex.__traceback__)
2984+
sys.exit(1)
2985+
2986+
fn = wrapped_fn
29712987
try:
29722988
db[table].convert(
29732989
columns,

0 commit comments

Comments
 (0)