Skip to content

Commit

Permalink
Merge branch 'main' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
nim65s committed Aug 8, 2023
2 parents 92ce6e7 + d2040ce commit 082cb84
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ repos:
rev: v0.0.282
hooks:
- id: ruff
exclude: gepetuto/magic.py
args:
- --fix
- --exit-non-zero-on-fix
Expand Down
38 changes: 31 additions & 7 deletions gepetuto/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
LOG = logging.getLogger("gepetuto")


def parse_args() -> argparse.Namespace:
def parse_args(args=None) -> argparse.Namespace:
"""Check what the user want."""
parser = argparse.ArgumentParser(prog="gepetuto", description="gepetuto tools")
parser.add_argument(
Expand All @@ -35,9 +35,17 @@ def parse_args() -> argparse.Namespace:
nargs="?",
help="choose what to do. Default to 'generate'.",
)
parser.add_argument(
"-f",
"--file",
default=[],
type=str,
nargs="*",
help="choose which files to process.",
)
parser.add_argument(
"tp_id",
default=get_tp_id(),
default=[],
type=int,
nargs="*",
help="choose which tp to process. Default to all.",
Expand All @@ -49,7 +57,7 @@ def parse_args() -> argparse.Namespace:
help="choose python interpreter to use.",
)

args = parser.parse_args()
args = parser.parse_args(args=args)

if args.verbose == 0:
level = os.environ.get("GEPETUTO_LOG_LEVEL", "WARNING")
Expand Down Expand Up @@ -92,19 +100,35 @@ def retrieve_python_interpreter():
return sys.executable


def get_file_list(tp_id, file):
"""Get the list of files we use action on."""
file = [Path(f) for f in file]
file_list = []
if tp_id == []:
tp_id = get_tp_id()
for n in tp_id:
folder = Path(f"tp{n}")
if file == []:
file_list += folder.glob("*.py")
else:
file_list += list(filter(lambda f: f in file, folder.glob("*.py")))
return file_list


def main():
"""Run command."""
args = parse_args()
files = get_file_list(args.tp_id, args.file)
if args.action == "generate":
generate(**vars(args))
elif args.action == "lint":
lint(**vars(args))
lint(files, **vars(args))
elif args.action == "test":
test(**vars(args))
test(files, **vars(args))
elif args.action == "all":
LOG.debug("no action specified, running all 3.")
lint(**vars(args))
test(**vars(args))
lint(files, **vars(args))
test(files, **vars(args))
generate(**vars(args))


Expand Down
24 changes: 9 additions & 15 deletions gepetuto/lint.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
"""Add "lint" action for the "gepetuto" program."""

import logging
from pathlib import Path
from subprocess import check_call
from typing import List

LOG = logging.getLogger("gepetuto.lint")


def lint(tp_id: List[int], **kwargs):
def lint(files, **kwargs):
"""Lint python scripts."""
LOG.info("linting tutorial sources.")
for n in tp_id:
LOG.debug(f"Looking for tp {n}")
lint_tp_folder(n)
for f in files:
lint_folder(f)
LOG.info("lint done.")


def lint_tp_folder(tp_number):
"""Lint python scripts for a given tp_number."""
LOG.debug(f"Looking for tp {tp_number}")
folder = Path(f"tp{tp_number}")
for python_file in folder.glob("*.py"):
LOG.debug(f"Checking {python_file}")
check_call(["isort", python_file])
check_call(["black", python_file])
check_call(["ruff", "--fix", python_file])
def lint_folder(file):
"""Lint python scripts in folder."""
LOG.debug(f"Checking {file}")
check_call(["isort", file])
check_call(["black", file])
check_call(["ruff", "--fix", file])
21 changes: 11 additions & 10 deletions gepetuto/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
import logging
from pathlib import Path
from subprocess import check_call
from typing import List

LOG = logging.getLogger("gepetuto.test")


def test(tp_id: List[int], **kwargs):
def test(files, **kwargs):
"""Test python scripts."""
python_interpreter = kwargs["python"]
LOG.info("testing tutorial sources.")
for n in tp_id:
LOG.debug(f"Looking for tp {n}")
folder = Path(f"tp{n}")
for python_file in folder.glob("*.py"):
LOG.debug(f"Checking {python_file}")
check_call([python_interpreter, python_file])
check_ipynb(n, python_interpreter)
tp_id = int(str(files[0])[2]) # get the tp id of the first file
check_ipynb(tp_id, python_interpreter)
for f in files:
LOG.debug(f"Checking {f}")
check_call([python_interpreter, f])
current_tp_id = int(str(f)[2])
if tp_id != current_tp_id:
tp_id = current_tp_id
check_ipynb(current_tp_id, python_interpreter)
LOG.info("test passed.")


def check_ipynb(tp_number, python_interpreter):
"""Check .ipynb files from given tp_number and move it in temporary folder."""
"""Check .ipynb files from given tp_number."""
ipynb = next(Path().glob(f"{tp_number}_*.ipynb"))
check_call(["jupyter", "nbconvert", "--to", "script", f"{ipynb}"])
converted_ipynb = next(Path().glob(f"{tp_number}_*.py"))
Expand Down

0 comments on commit 082cb84

Please sign in to comment.