Skip to content

Commit

Permalink
add directory argument
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoMF committed Aug 16, 2023
1 parent fe9d982 commit 403b3a6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
12 changes: 6 additions & 6 deletions gepetuto/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
HASHTAGS = ["jupyter_snippet"]


def generate(tp_id: List[int], **kwargs):
def generate(tp_ids: List[int], **kwargs):
"""Parse python scripts to generate snippets."""
LOG.info("generating snippets from tutorial sources.")
for n in tp_id:
for n in tp_ids:
LOG.debug("Looking for tp %s", n)
generate_from_id(n)
generate_from_id(n, kwargs["directory"])


def generate_from_id(tp_id: int):
def generate_from_id(tp_id: int, directory):
"""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"))
folder = Path(directory) / f"tp{tp_id}"
ipynb = next(Path(directory).glob(f"{tp_id}-*.ipynb"))
generate_ipynb(ipynb, folder)


Expand Down
32 changes: 20 additions & 12 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=get_tp_id(),
default=[],
type=int,
nargs="*",
help="choose which tp to process. Default to all.",
Expand All @@ -67,6 +67,13 @@ def parse_args(args=None) -> argparse.Namespace:
action="store_true",
help="check if linters change files.",
)
parser.add_argument(
"-C",
"--directory",
default=".",
type=str,
help="choose directory to run action on.",
)

args = parser.parse_args(args=args)

Expand All @@ -81,16 +88,16 @@ def parse_args(args=None) -> argparse.Namespace:
return args


def get_tp_id():
def get_tp_ids(directory):
"""Find tp to process."""
tp_id = []
tp_ids = []
current_tp_id = 0
while True:
folder = Path(f"tp{current_tp_id}")
folder = Path(directory) / f"tp{current_tp_id}"
if folder.exists():
tp_id.append(current_tp_id)
tp_ids.append(current_tp_id)
elif current_tp_id != 0:
return tp_id
return tp_ids
current_tp_id += 1


Expand All @@ -111,12 +118,12 @@ def retrieve_python_interpreter():
return sys.executable


def get_files(args):
def get_files(args, tp_ids):
"""Get the list of files we use action on."""
file = [Path(f) for f in args.file]
files = {}
for n in args.tp_id:
folder = Path(f"tp{n}")
for n in tp_ids:
folder = Path(args.directory) / f"tp{n}"
tp_files = list(folder.glob("*.py"))
if file != []:
tp_files = [f for f in tp_files if f in file]
Expand All @@ -134,9 +141,10 @@ def get_files(args):
def main():
"""Run command."""
args = parse_args()
files = get_files(args)
tp_ids = get_tp_ids(args.directory) if args.tp_id == [] else args.tp_id
files = get_files(args, tp_ids)
if args.action == "generate":
generate(**vars(args))
generate(tp_ids, **vars(args))
elif args.action == "lint":
lint(files, **vars(args))
elif args.action == "test":
Expand All @@ -145,4 +153,4 @@ def main():
LOG.debug("no action specified, running all 3.")
lint(files, **vars(args))
test(files, **vars(args))
generate(**vars(args))
generate(tp_ids, **vars(args))
17 changes: 10 additions & 7 deletions gepetuto/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ def test(files, **kwargs):
for tp_file in tp_files:
LOG.debug("Checking %s", tp_file)
check_call([python_interpreter, tp_file])
ipynbs = get_ipynbs(files)
ipynbs = get_ipynbs(files, kwargs["directory"])
for tp_ipynbs in ipynbs.values():
for tp_ipynb in tp_ipynbs:
check_ipynb(tp_ipynb, python_interpreter)
check_ipynb(tp_ipynb, python_interpreter, kwargs["directory"])
LOG.info("test passed.")


def get_ipynbs(files):
def get_ipynbs(files, directory):
"""Get the dictionary of ipynbs to test."""
ipynbs = defaultdict(list)
for ipynb in Path().glob("*.ipynb"):
for ipynb in Path(directory).glob("*.ipynb"):
prefix = str(ipynb).split("-")[0]
prefix = prefix.replace(directory + "/", "")
if prefix.isdecimal():
if int(prefix) in files.keys():
ipynbs[prefix].append(ipynb)
Expand All @@ -38,14 +39,16 @@ def get_ipynbs(files):
return ipynbs


def check_ipynb(ipynb, python_interpreter):
def check_ipynb(ipynb, python_interpreter, directory):
"""Check .ipynb files from given tp_number."""
prefix = str(ipynb).split("-")[0]
tp_path = Path(f"tp{prefix}" if prefix.isdecimal() else prefix)
prefix = prefix.replace(directory + "/", "")
tp_path = Path(directory)
tp_path = tp_path / f"tp{prefix}" if prefix.isdecimal() else tp_path / prefix
if tp_path.exists():
generate_ipynb(ipynb, tp_path, True)
check_call(["jupyter", "nbconvert", "--to", "script", f"{ipynb}"])
converted_ipynb = next(Path().glob(f"{prefix}-*.py"))
converted_ipynb = next(Path(directory).glob(f"{prefix}-*.py"))
LOG.debug("Checking temporary file %s", converted_ipynb)
check_call([python_interpreter, converted_ipynb])
Path.unlink(converted_ipynb)

0 comments on commit 403b3a6

Please sign in to comment.