Skip to content

Commit

Permalink
Merge pull request #40 from TheoMF/filter_arg_#32
Browse files Browse the repository at this point in the history
Filter arg #32
  • Loading branch information
nim65s authored Aug 9, 2023
2 parents df7e1e6 + 650b1c6 commit a061715
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [v1.0.0] - 2023-08-08

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

[Unreleased]: https://github.com/gepetto/gepetuto/compare/v1.0.0...main
[v1.0.0]: https://github.com/cmake-wheel/cmeel/releases/tag/v1.0.0
32 changes: 22 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,30 @@ 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]
tp_id = args.tp_id or get_tp_id()
file = [Path(f) for f in args.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")))
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

0 comments on commit a061715

Please sign in to comment.