fileutils-dir 0.1.2__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.
fileutils/__init__.py
ADDED
fileutils/files.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Iterable
|
|
3
|
+
from collections.abc import Iterable
|
|
4
|
+
|
|
5
|
+
FILE_TYPES = {
|
|
6
|
+
"image": [".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif", ".tiff"],
|
|
7
|
+
"text": [".txt", ".md", ".rst", ".log"],
|
|
8
|
+
"pdf": [".pdf"],
|
|
9
|
+
"doc": [".doc", ".docx", ".odt"],
|
|
10
|
+
"sheet": [".xls", ".xlsx", ".ods", ".csv"],
|
|
11
|
+
"presentation": [".ppt", ".pptx", ".odp"],
|
|
12
|
+
"code": [
|
|
13
|
+
".py", ".js", ".ts", ".java", ".c", ".cpp", ".h",
|
|
14
|
+
".go", ".rs", ".rb", ".php", ".sh"
|
|
15
|
+
],
|
|
16
|
+
"data": [".json", ".yaml", ".yml", ".xml", ".toml"],
|
|
17
|
+
"audio": [".mp3", ".wav", ".flac", ".ogg", ".aac", ".m4a"],
|
|
18
|
+
"video": [".mp4", ".mkv", ".avi", ".mov", ".webm"],
|
|
19
|
+
"archive": [".zip", ".tar", ".gz", ".bz2", ".7z", ".rar"],
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def in_dir(
|
|
24
|
+
directories: Iterable[str] | None = None,
|
|
25
|
+
extensions: Iterable[str] | None = None,
|
|
26
|
+
types: Iterable[str] | None = None,
|
|
27
|
+
) -> list[str]:
|
|
28
|
+
"""
|
|
29
|
+
Return files from the given directories:\n.
|
|
30
|
+
- If 'directories' are not given, only the PWD is considered.
|
|
31
|
+
- If `extensions` or `types` are provided, only matching files are returned.
|
|
32
|
+
- If neither is provided, all files in the PWD are returned.
|
|
33
|
+
"""
|
|
34
|
+
if isinstance(directories, str):
|
|
35
|
+
types = [directories]
|
|
36
|
+
directories = None
|
|
37
|
+
if directories is None:
|
|
38
|
+
directories = ["."]
|
|
39
|
+
if extensions is None:
|
|
40
|
+
extensions = []
|
|
41
|
+
if types is None:
|
|
42
|
+
types = []
|
|
43
|
+
|
|
44
|
+
normalized_extensions = {
|
|
45
|
+
ext.lower() if ext.startswith(".") else f".{ext.lower()}"
|
|
46
|
+
for ext in extensions
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for file_type in types:
|
|
50
|
+
key = file_type.strip().lower()
|
|
51
|
+
try:
|
|
52
|
+
normalized_extensions.update(FILE_TYPES[key])
|
|
53
|
+
except KeyError:
|
|
54
|
+
raise ValueError(f"Unknown file type: {file_type}")
|
|
55
|
+
|
|
56
|
+
files_to_return: list[str] = []
|
|
57
|
+
|
|
58
|
+
for path in directories:
|
|
59
|
+
for item in Path(path).iterdir():
|
|
60
|
+
if not item.is_file():
|
|
61
|
+
continue
|
|
62
|
+
|
|
63
|
+
# If no filters, accept everything
|
|
64
|
+
if not normalized_extensions:
|
|
65
|
+
files_to_return.append(str(item))
|
|
66
|
+
continue
|
|
67
|
+
|
|
68
|
+
# Otherwise, filter by extension
|
|
69
|
+
if item.suffix.lower() in normalized_extensions:
|
|
70
|
+
files_to_return.append(str(item))
|
|
71
|
+
|
|
72
|
+
return files_to_return
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fileutils-dir
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Small utilities for listing files in directories
|
|
5
|
+
Author: Jatavallabhula Sarat Anirudh
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# fileutils
|
|
11
|
+
|
|
12
|
+
Small utilities for working with files and directories.
|
|
13
|
+
|
|
14
|
+
Currently provides a helper to list files in one or more directories,
|
|
15
|
+
optionally filtered by file extension or file type.
|
|
16
|
+
|
|
17
|
+
Installation (local development)
|
|
18
|
+
|
|
19
|
+
pip install -e .
|
|
20
|
+
|
|
21
|
+
Usage
|
|
22
|
+
|
|
23
|
+
from fileutils import in_dir
|
|
24
|
+
|
|
25
|
+
# All files in current directory
|
|
26
|
+
files = in_dir()
|
|
27
|
+
|
|
28
|
+
# All Python files
|
|
29
|
+
files = in_dir(extensions=[".py"])
|
|
30
|
+
|
|
31
|
+
# Files by type
|
|
32
|
+
files = in_dir(types=["code"])
|
|
33
|
+
|
|
34
|
+
# Multiple directories
|
|
35
|
+
files = in_dir(directories=["src", "tests"], types=["code"])
|
|
36
|
+
|
|
37
|
+
Behavior
|
|
38
|
+
|
|
39
|
+
- Defaults to the current directory (.)
|
|
40
|
+
- If no extensions or types are provided, all files are returned
|
|
41
|
+
- If both are provided, the result is the union of both filters
|
|
42
|
+
- Only files are returned (directories are ignored)
|
|
43
|
+
|
|
44
|
+
Supported types
|
|
45
|
+
|
|
46
|
+
Common file groups such as:
|
|
47
|
+
- code
|
|
48
|
+
- text
|
|
49
|
+
- image
|
|
50
|
+
- pdf
|
|
51
|
+
- audio
|
|
52
|
+
- video
|
|
53
|
+
- archive
|
|
54
|
+
|
|
55
|
+
See source for the full mapping.
|
|
56
|
+
|
|
57
|
+
License
|
|
58
|
+
|
|
59
|
+
MIT
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
fileutils/__init__.py,sha256=3u-pHKVodcCPECMcj95lrkk8nr7tl6_HRqiQOw6YbBM,78
|
|
2
|
+
fileutils/files.py,sha256=j4knqqqwS8iOywxapZjJHx8kQee1SB0u6dzrVYdIhIA,2375
|
|
3
|
+
fileutils_dir-0.1.2.dist-info/METADATA,sha256=_yHXInJ_ccb18DhVFmBjVp20UezeofZy5KqVhgwWAyQ,1239
|
|
4
|
+
fileutils_dir-0.1.2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
5
|
+
fileutils_dir-0.1.2.dist-info/top_level.txt,sha256=kZPCAZpYo7OkmwY_R2a4ifuMINdW5AZd2JaNHDSXQYQ,10
|
|
6
|
+
fileutils_dir-0.1.2.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fileutils
|