Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--pdb option to open debugger if an error occurs #51

Open
simonw opened this issue Dec 31, 2021 · 0 comments
Open

--pdb option to open debugger if an error occurs #51

simonw opened this issue Dec 31, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Dec 31, 2021

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",
 )
+@click.option(
+    "_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 simonw added the enhancement New feature or request label Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant