git-history icon indicating copy to clipboard operation
git-history copied to clipboard

`--pdb` option to open debugger if an error occurs

Open simonw opened this issue 3 years ago • 0 comments

Prototype:

diff --git a/git_history/cli.py b/git_history/cli.py
index f3a4c40..83f988e 100644
--- a/git_history/cli.py
+++ b/git_history/cli.py
@@ -117,6 +117,11 @@ def cli():
     is_flag=True,
     help="Debug mode",
 )
[email protected](
+    "_pdb", "--pdb",
+    is_flag=True,
+    help="Open Python debugger if an error occurs",
+)
 @click.option(
     "--silent",
     is_flag=True,
@@ -142,6 +147,7 @@ def file(
     ignore_duplicate_ids,
     wal,
     debug,
+    _pdb,
     silent,
 ):
     "Analyze the history of a specific file and write it to SQLite"
@@ -178,7 +184,7 @@ def file(
     if not convert:
         convert = "json.loads(content)"
 
-    convert_function = compile_convert(convert, imports)
+    convert_function = compile_convert(convert, imports, _pdb)
 
     resolved_filepath = str(Path(filepath).resolve())
     resolved_repo = str(Path(repo).resolve())
@@ -457,7 +463,7 @@ def build_csv_convert_string(dialect):
     ).strip()
 
 
-def compile_convert(convert, imports):
+def compile_convert(convert, imports, pdb=False):
     # Clean up the provided code
     # If single line and no 'return', add the return
     if "\n" not in convert and not convert.strip().startswith("return "):
@@ -472,7 +478,16 @@ def compile_convert(convert, imports):
     for import_ in imports:
         globals[import_.split(".")[0]] = __import__(import_)
     exec(code_o, globals, locals)
-    return locals["fn"]
+    fn = locals["fn"]
+    if pdb:
+        def wrapper(content):
+            try:
+                yield from fn(content)
+            except Exception as e:
+                print(e)
+                import pdb; pdb.post_mortem()
+        return wrapper
+    return fn
 
 
 def remove_ignore_columns(items, ignore):

simonw avatar Dec 31 '21 17:12 simonw