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

add file argument #24

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.281
hooks:
- id: ruff
exclude: gepetuto/magic.py
Copy link
Member

Choose a reason for hiding this comment

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

This won't prevent manual runs of ruff . to fail. I think you should instead add hints in the file : either a # noqa: F821 in the end of each line where there is a get_ipython, or a # ruff: noqa: F821 at the begining of the file.

A better solution might be to add a def run(self, magic) method, which run get_ipython().run_line_magic(magic) with the correct noqa only once, and use this method in the other places.

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
Copy link
Member

Choose a reason for hiding this comment

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

Ok, we'll probably want to move to a {tp_number: [list of files in this tp]} structure at some point, but this is fine for now

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