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

Filter arg #32 #40

Merged
merged 12 commits into from
Aug 9, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- project initialisation
- add the -F/--filter argument

[Unreleased]: https://github.com/gepetto/gepetuto
38 changes: 28 additions & 10 deletions gepetuto/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def parse_args(args=None) -> argparse.Namespace:
nargs="*",
help="choose which files to process.",
)
parser.add_argument(
"-F",
"--filter",
default=[],
type=str,
nargs="*",
help="filter files to process.",
)
parser.add_argument(
"tp_id",
default=[],
Expand Down Expand Up @@ -97,26 +105,36 @@ def retrieve_python_interpreter():
return sys.executable


def get_file_list(tp_id, file):
def get_file_list(args):
"""Get the list of files we use action on."""
file = [Path(f) for f in file]
file_list = []
if tp_id == []:
if args.tp_id == []:
tp_id = get_tp_id()
else:
tp_id = args.tp_id
nim65s marked this conversation as resolved.
Show resolved Hide resolved
if args.file == []:
file = []
else:
file = [Path(f) for f in args.file]
nim65s marked this conversation as resolved.
Show resolved Hide resolved
file_list = []
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")))
tp_files = folder.glob("*.py")
if file != []:
tp_files = [f for f in tp_files if f in file]
if args.filter != []:
tp_files = [
f
for f in tp_files
if any(filter_str in str(f) for filter_str in args.filter)
]
file_list += tp_files
return file_list


def main():
"""Run command."""
print("here2")
args = parse_args()
files = get_file_list(args.tp_id, args.file)
files = get_file_list(args)
if args.action == "generate":
generate(**vars(args))
elif args.action == "lint":
Expand Down
48 changes: 43 additions & 5 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestGepetutoArguments(unittest.TestCase):
def test_no_arg(self):
"""Check files we work on when no arguments are given."""
arguments = parse_args()
file_list = get_file_list(arguments.tp_id, arguments.file)
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") in file_list)
self.assertTrue(Path("tp2/cholesky.py") in file_list)
Expand All @@ -20,7 +20,7 @@ def test_no_arg(self):
def test_tp_id_1(self):
"""Check files we work on when we specify tp_id = 1."""
arguments = parse_args(["1"])
file_list = get_file_list(arguments.tp_id, arguments.file)
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
Expand All @@ -29,7 +29,7 @@ def test_tp_id_1(self):
def test_file_cholesky(self):
"""Check files we work on when we specify a file with --file."""
arguments = parse_args(["--file", "tp1/cholesky.py"])
file_list = get_file_list(arguments.tp_id, arguments.file)
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
Expand All @@ -38,7 +38,7 @@ def test_file_cholesky(self):
def test_tp_id_1_file_cholesky(self):
"""Check files we work on when we specify tp_id = 1 and a file in tp1 folder."""
arguments = parse_args(["1", "--file", "tp1/cholesky.py"])
file_list = get_file_list(arguments.tp_id, arguments.file)
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
Expand All @@ -47,7 +47,45 @@ def test_tp_id_1_file_cholesky(self):
def test_no_file_matching(self):
"""Check files we work on when tp_id and --file has no files in common."""
arguments = parse_args(["2", "--file", "tp1/cholesky.py"])
file_list = get_file_list(arguments.tp_id, arguments.file)
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") not in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)

def test_filter_cholesky(self):
"""Check files we work on with --filter cholesky argument."""
arguments = parse_args(["--filter", "cholesky"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)

def test_tp_id_1_filter_cholesky(self):
"""Check files we work on with --filter cholesky argument."""
arguments = parse_args(["1", "--filter", "cholesky"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)

def test_file_cholesky_filter_cholesky(self):
"""Check files we work on with --filter cholesky and --file on cholesky file."""
arguments = parse_args(["--file", "tp1/cholesky.py", "--filter", "cholesky"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)

def test_no_file_matching_2(self):
"""Check files we work on when --file and --filter has no files in common."""
arguments = parse_args(
["--file", "tp1/example_script.py", "--filter", "cholesky"],
)
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") not in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
Expand Down