Skip to content

Commit

Permalink
shelephant_dump: adding filter on extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Dec 14, 2022
1 parent 2f60ab5 commit 3887640
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions shelephant/cli/shelephant_dump.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import os
import pathlib
import re
import subprocess
import sys
Expand All @@ -23,6 +24,14 @@ def _shelephant_dump_parser():
parser.add_argument(
"-e", "--exclude", type=str, action="append", help="Exclude files matching this pattern."
)
parser.add_argument(
"-E",
"--exclude-extension",
type=str,
action="append",
default=[],
help='Exclude files based on their extension (e.g. ".bak").',
)
parser.add_argument("--fmt", type=str, help='Formatter of each line, e.g. ``"mycmd {}"``.')
parser.add_argument(
"-c",
Expand Down Expand Up @@ -82,6 +91,9 @@ def shelephant_dump(args: list[str]):
excl = np.logical_or(excl, np.array([re.match(pattern, file) for file in files]))
files = [file for file, ex in zip(files, excl) if not ex]

if args.exclude_extension:
files = [file for file in files if pathlib.Path(file).suffix not in args.exclude_extension]

if args.sort:
files = sorted(files)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np

import shelephant
from shelephant.cli.shelephant_dump import shelephant_dump


def run(cmd, verbose=False):
Expand Down Expand Up @@ -240,6 +241,17 @@ def test_append(self):
os.remove("bar.pdf")
os.remove("shelephant_dump.yaml")

def test_exclude(self):

shelephant_dump(["a.txt", "b.bak", "c.h5", "-E", ".bak", "-o", "dump.yaml"])

self.assertEqual(
shelephant.yaml.read("dump.yaml"),
["a.txt", "c.h5"],
)

os.remove("dump.yaml")


class Test_extract(unittest.TestCase):
def test_single_path(self):
Expand Down

0 comments on commit 3887640

Please sign in to comment.