Skip to content

Commit

Permalink
adapt ipynbs detection to new naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoMF committed Aug 9, 2023
1 parent 57c7bd3 commit 1c14086
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion gepetuto/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def generate(tp_id: List[int], **kwargs):
def generate_from_id(tp_id: int):
"""Find the corresponding ipynb and folder for a given tp_id."""
folder = Path() / f"tp{tp_id}"
ipynb = next(Path().glob(f"{tp_id}_*.ipynb"))
ipynb = next(Path().glob(f"{tp_id}-*.ipynb"))
generate_ipynb(ipynb, folder)


Expand Down
4 changes: 2 additions & 2 deletions gepetuto/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
def lint(files, **kwargs):
"""Lint python scripts."""
LOG.info("linting tutorial sources.")
for tp_number in files.keys():
for tp_file in files[tp_number]:
for tp_files in files.values():
for tp_file in tp_files:
lint_file(tp_file)
LOG.info("lint done.")

Expand Down
5 changes: 2 additions & 3 deletions gepetuto/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse_args(args=None) -> argparse.Namespace:
)
parser.add_argument(
"tp_id",
default=[],
default=get_tp_id(),
type=int,
nargs="*",
help="choose which tp to process. Default to all.",
Expand Down Expand Up @@ -107,10 +107,9 @@ def retrieve_python_interpreter():

def get_files(args):
"""Get the list of files we use action on."""
tp_id = args.tp_id or get_tp_id()
file = [Path(f) for f in args.file]
files = {}
for n in tp_id:
for n in args.tp_id:
folder = Path(f"tp{n}")
tp_files = list(folder.glob("*.py"))
if file != []:
Expand Down
41 changes: 22 additions & 19 deletions gepetuto/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,43 @@ def test(files, **kwargs):
"""Test python scripts."""
python_interpreter = kwargs["python"]
LOG.info("testing tutorial sources.")
ipynbs = get_ipynbs(files)
for tp_number in files.keys():
for tp_file in files[tp_number]:
for tp_files in files.values():
for tp_file in tp_files:
LOG.debug(f"Checking {tp_file}")
check_call([python_interpreter, tp_file])
for ipynb_key in ipynbs.keys():
for ipynb in ipynbs[ipynb_key]:
check_ipynb(ipynb, ipynb_key, python_interpreter)
ipynbs = get_ipynbs(files)
for tp_ipynbs in ipynbs.values():
for tp_ipynb in tp_ipynbs:
check_ipynb(tp_ipynb, python_interpreter)
LOG.info("test passed.")


def get_ipynbs(files):
"""Get the dictionary of ipynbs to test."""
ipynbs = {}
for ipynb in Path().glob("*.ipynb"):
if str(ipynb)[0].isdigit():
tp_number = int(str(ipynb)[0])
if tp_number in files.keys():
if tp_number not in ipynbs.keys():
ipynbs[tp_number] = [ipynb]
else:
ipynbs[tp_number].append(ipynb)
prefix = str(ipynb).split("-")[0]
if prefix.isdecimal():
ipynbs = add_value_in_ipynbs(ipynbs, ipynb, prefix)
else:
if "appendix" not in ipynbs.keys():
ipynbs["appendix"] = [ipynb]
else:
ipynbs["appendix"].append(ipynb)
ipynbs = add_value_in_ipynbs(ipynbs, ipynb, prefix)
return ipynbs


def add_value_in_ipynbs(ipynbs, ipynb, prefix):
"""Add an ipynb file to the dictionary ipynbs."""
if prefix not in ipynbs.keys():
ipynbs[prefix] = [ipynb]
else:
ipynbs[prefix].append(ipynb)
return ipynbs


def check_ipynb(ipynb, ipynb_key, python_interpreter):
def check_ipynb(ipynb, python_interpreter):
"""Check .ipynb files from given tp_number."""
check_call(["jupyter", "nbconvert", "--to", "script", f"{ipynb}"])
converted_ipynb = next(Path().glob(f"{ipynb_key}_*.py"))
prefix = str(ipynb).split("-")[0]
converted_ipynb = next(Path().glob(f"{prefix}-*.py"))
LOG.debug(f"Checking temporary file {converted_ipynb}")
check_call([python_interpreter, converted_ipynb])
Path.unlink(converted_ipynb)

0 comments on commit 1c14086

Please sign in to comment.