filefilter 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
filefilter/__init__.py ADDED
@@ -0,0 +1,20 @@
1
+ __version__ = "0.1.1"
2
+
3
+ from .config import Config
4
+ from .matcher import collect_files
5
+ import json
6
+
7
+
8
+ def filter_paths(config_json: str, resolve_base: str = 'cwd'):
9
+ """
10
+ Given a JSON string defining 'root_dir' and 'filters',
11
+ returns a list of file paths matching the rules.
12
+
13
+ Parameters:
14
+ - config_json: JSON string with 'root_dir' and 'filters'
15
+ - resolve_base: 'cwd' (default) to resolve root_dir relative to current working directory,
16
+ 'script' to resolve relative to this library's script directory.
17
+ """
18
+ data = json.loads(config_json)
19
+ cfg = Config(data, resolve_base=resolve_base)
20
+ return collect_files(cfg)
filefilter/config.py ADDED
@@ -0,0 +1,93 @@
1
+ import os
2
+ import re
3
+ from fnmatch import fnmatch
4
+
5
+
6
+ def normalize_path(p: str) -> str:
7
+ """
8
+ Collapse backslashes and multiple slashes to '/', trim whitespace.
9
+ """
10
+ p = re.sub(r"[\\/]+", "/", p)
11
+ return p.strip()
12
+
13
+
14
+ def parse_dir_patterns(patterns):
15
+ """
16
+ Parse directory glob patterns into:
17
+ - root-only patterns (first segment match)
18
+ - anywhere patterns (any ancestor segment)
19
+ Strips '**/' prefix and '/**' suffix, handles leading '/' for root-only.
20
+ """
21
+ root_patts = []
22
+ any_patts = []
23
+ for raw in patterns:
24
+ p = normalize_path(raw)
25
+ low = p.lower()
26
+ anchored = p.startswith("/")
27
+ # remove recursive markers for internal logic
28
+ low = re.sub(r"^\*\*/", "", low)
29
+ low = re.sub(r"/\*\*$", "", low)
30
+ low = low.rstrip("/")
31
+ if not low:
32
+ continue
33
+ if anchored:
34
+ root_patts.append(low)
35
+ else:
36
+ any_patts.append(low)
37
+ return root_patts, any_patts
38
+
39
+
40
+ def parse_file_patterns(patterns):
41
+ """
42
+ Normalize and lowercase filename patterns (wildcards '*' only).
43
+ """
44
+ return [normalize_path(p).lower() for p in patterns]
45
+
46
+
47
+ def parse_extensions(patterns):
48
+ """
49
+ Normalize and lowercase extensions, ensure leading '.'.
50
+ """
51
+ out = []
52
+ for e in patterns:
53
+ norm = normalize_path(e).lower().lstrip('.')
54
+ out.append('.' + norm)
55
+ return out
56
+
57
+
58
+ class Config:
59
+ def __init__(self, data: dict, resolve_base: str = ''):
60
+ """
61
+ Initialize configuration from a dict.
62
+
63
+ Parameters:
64
+ - data: parsed JSON dict containing 'root_dir' and 'filters'
65
+ - resolve_base: 'cwd' or 'script'
66
+ """
67
+ raw_root = normalize_path(data['root_dir'])
68
+ # Resolve absolute or relative root_dir
69
+ if os.path.isabs(raw_root):
70
+ root = raw_root
71
+ else:
72
+ if resolve_base == '':
73
+ base_dir = os.getcwd()
74
+
75
+ else:
76
+ base_dir = normalize_path(resolve_base)
77
+ root = os.path.join(base_dir, raw_root)
78
+ self.root_dir = os.path.normpath(root)
79
+
80
+ inc = data['filters']['include']
81
+ exc = data['filters']['exclude']
82
+
83
+ # Include/Exclude directory patterns
84
+ self.inc_dirs_root, self.inc_dirs_any = parse_dir_patterns(inc.get('dirs', []))
85
+ self.exc_dirs_root, self.exc_dirs_any = parse_dir_patterns(exc.get('dirs', []))
86
+
87
+ # Include/Exclude filename patterns
88
+ self.include_files = parse_file_patterns(inc.get('files', []))
89
+ self.exclude_files = parse_file_patterns(exc.get('files', []))
90
+
91
+ # Include/Exclude extensions
92
+ self.inc_exts = parse_extensions(inc.get('extensions', []))
93
+ self.exc_exts = parse_extensions(exc.get('extensions', []))
filefilter/matcher.py ADDED
@@ -0,0 +1,82 @@
1
+ import os
2
+ from fnmatch import fnmatch
3
+ from .config import normalize_path, Config
4
+
5
+
6
+ def should_include(full_path: str, cfg: Config) -> bool:
7
+ """
8
+ Determine if a file should be included based on the config rules.
9
+ """
10
+ rel = os.path.relpath(full_path, cfg.root_dir)
11
+ rel = normalize_path(rel).lower()
12
+ segments = rel.split('/')
13
+ name = segments[-1]
14
+ ancestors = segments[:-1]
15
+ _, ext = os.path.splitext(name)
16
+
17
+ # 1) Include-files override
18
+ for patt in cfg.include_files:
19
+ if fnmatch(name, patt):
20
+ return True
21
+
22
+ # 2) Exclude by extension
23
+ if ext in cfg.exc_exts:
24
+ return False
25
+
26
+ # 3) Exclude by filename
27
+ for patt in cfg.exclude_files:
28
+ if fnmatch(name, patt):
29
+ return False
30
+
31
+ # 4) Exclude by directory (root-only)
32
+ if ancestors and cfg.exc_dirs_root:
33
+ first = ancestors[0]
34
+ for patt in cfg.exc_dirs_root:
35
+ if fnmatch(first, patt):
36
+ return False
37
+ # Exclude by directory (anywhere)
38
+ for seg in ancestors:
39
+ for patt in cfg.exc_dirs_any:
40
+ if fnmatch(seg, patt):
41
+ return False
42
+
43
+ # 5) Include by directory if patterns exist
44
+ if cfg.inc_dirs_root or cfg.inc_dirs_any:
45
+ ok = False
46
+ if ancestors and cfg.inc_dirs_root:
47
+ first = ancestors[0]
48
+ for patt in cfg.inc_dirs_root:
49
+ if fnmatch(first, patt):
50
+ ok = True
51
+ break
52
+ if not ok and cfg.inc_dirs_any:
53
+ for seg in ancestors:
54
+ for patt in cfg.inc_dirs_any:
55
+ if fnmatch(seg, patt):
56
+ ok = True
57
+ break
58
+ if ok:
59
+ break
60
+ if not ok:
61
+ return False
62
+
63
+ # 6) Include by extension
64
+ if cfg.inc_exts and ext not in cfg.inc_exts:
65
+ return False
66
+
67
+ return True
68
+
69
+
70
+ def collect_files(cfg: Config) -> list:
71
+ """
72
+ Walk root_dir and collect files passing should_include.
73
+ """
74
+ matches = []
75
+ for root, _, files in os.walk(cfg.root_dir, followlinks=False):
76
+ for fn in files:
77
+ full = os.path.join(root, fn)
78
+ if os.path.islink(full):
79
+ continue
80
+ if should_include(full, cfg):
81
+ matches.append(os.path.normpath(full))
82
+ return matches
@@ -0,0 +1,45 @@
1
+ Metadata-Version: 2.4
2
+ Name: filefilter
3
+ Version: 0.1.1
4
+ Summary: Filter files in a directory tree based on configurable glob rules.
5
+ Author-email: "Ioannis D (devcoons)" <support@devcoons.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/devcoons/filefilter
8
+ Project-URL: Issues, https://github.com/devcoons/filefilter/issues
9
+ Keywords: filefilter
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Natural Language :: English
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3.7
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
21
+ Requires-Python: >=3.7
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Provides-Extra: dev
25
+ Requires-Dist: pip-tools; extra == "dev"
26
+ Requires-Dist: pytest; extra == "dev"
27
+ Requires-Dist: flake8; extra == "dev"
28
+ Requires-Dist: mypy; extra == "dev"
29
+ Dynamic: license-file
30
+
31
+ # filefilter
32
+
33
+ Filter files in a directory tree using include/exclude rules with globs.
34
+
35
+ ## Features
36
+
37
+ - Include/exclude by directory patterns (`**`, `*`, explicit root `/`).
38
+ - Include/exclude by filename patterns (`*`, no `?`).
39
+ - Include/exclude by file extension.
40
+ - Config driven via JSON.
41
+
42
+ ## Installation
43
+
44
+ ```bash
45
+ pip install filefilter
@@ -0,0 +1,8 @@
1
+ filefilter/__init__.py,sha256=BBMGl2EO42zyAEwBrzUjC9yTPxzcJAyCdFUV55gs4is,687
2
+ filefilter/config.py,sha256=m6iArbwjYAarWV5N7xpGxVvWlBy6bzuctL1hRRclmsU,2899
3
+ filefilter/matcher.py,sha256=3rWd-fyldrj9KgWMWmkcWbg1hb3kg4jIvXg3Tj1asyY,2443
4
+ filefilter-0.1.1.dist-info/licenses/LICENSE,sha256=soCkgLf6GpZWygVKSaEqRTpCVLep_xs-V21hMwaVD50,1097
5
+ filefilter-0.1.1.dist-info/METADATA,sha256=k3gZmIN23yr04eST-YyvzsqeRmODOg1ErXyO9_Csmko,1588
6
+ filefilter-0.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ filefilter-0.1.1.dist-info/top_level.txt,sha256=DHSDZ8eUvqz4i1YKV4IwONi3MO9xxf5OGKiTsw1FH_I,11
8
+ filefilter-0.1.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ioannis D. (devcoons)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ filefilter