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

support ipynb ipython parser for magic and system #88

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
18 changes: 15 additions & 3 deletions pigar/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from .helpers import parse_git_config, trim_suffix

import nbformat
if sys.version_info.major == 3:
PYTHON_VERSION_3 = True
import IPython
else:
PYTHON_VERSION_3 = False
Copy link
Owner

@damnever damnever Feb 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if Python 4.x gets released?

I think this is better:

try:
    import IPython
    _transformer_manager = IPython.core.inputtransformer2.TransformerManager()
except Exception:  # ???
    _transformer_manager = None

Copy link
Author

@yasirroni yasirroni Feb 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove the comment? I don't have any idea for the comment nor the Exeption class.

    except Exception: 


Module = collections.namedtuple('Module', ['name', 'try_', 'file', 'lineno'])

Expand Down Expand Up @@ -59,9 +64,16 @@ def _read_code(fpath):
if fpath.endswith(".ipynb"):
nb = nbformat.read(fpath, as_version=4)
code = ""
for cell in nb.cells:
if cell.cell_type == "code":
code += cell.source + "\n"
if PYTHON_VERSION_3:
transformer = IPython.core.inputtransformer2.TransformerManager()
for cell in nb.cells:
if cell.cell_type == "code":
code += transformer.transform_cell(cell.source) + "\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests failed maybe because the transform_cell transformed the original code, such as adding empty lines.

# allow python version 2.7, but without !magic and !system support
else:
for cell in nb.cells:
if cell.cell_type == "code":
code += cell.source + "\n"
yasirroni marked this conversation as resolved.
Show resolved Hide resolved
return code
elif fpath.endswith(".py"):
with open(fpath, 'rb') as f:
Expand Down
1 change: 1 addition & 0 deletions py3_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ colorama == 0.4.3

# pigar/parser.py: 16
nbformat == 5.0.4
ipython == 7.0.0

# pigar/core.py: 28
# pigar/pypi.py: 19
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

install_requires = [
'colorama>=0.3.9', 'requests>=2.20.0', 'nbformat>=4.4.0',
'futures;python_version<"3.2"'
'ipython>=7.0', 'futures;python_version<"3.2"'
]

setup(
Expand Down