generated from aboutmydreams/quick-pylib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add find files and folder function
- Loading branch information
1 parent
7f12111
commit b4f8e14
Showing
6 changed files
with
215 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import shutil | ||
import unittest | ||
from io import StringIO | ||
from unittest.mock import patch | ||
from way3 import tree | ||
|
||
|
||
class TestTreeFunction(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
os.makedirs("test_dir/sub_dir1") | ||
os.makedirs("test_dir/sub_dir2") | ||
with open("test_dir/file1.txt", "w") as f: | ||
f.write("file1") | ||
with open("test_dir/file2.txt", "w") as f: | ||
f.write("file2") | ||
with open("test_dir/sub_dir1/file3.txt", "w") as f: | ||
f.write("file3") | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
shutil.rmtree("test_dir") | ||
|
||
@patch("sys.stdout", new_callable=StringIO) | ||
def test_tree_basic(self, mock_stdout): | ||
tree("test_dir") | ||
output = mock_stdout.getvalue().strip() | ||
expected_output = "\n".join( | ||
[ | ||
"|-- file1.txt", | ||
"|-- file2.txt", | ||
"|-- sub_dir1", | ||
"| |-- file3.txt", | ||
"|-- sub_dir2", | ||
] | ||
) | ||
self.assertEqual(output, expected_output) | ||
|
||
@patch("sys.stdout", new_callable=StringIO) | ||
def test_tree_full_path(self, mock_stdout): | ||
tree("test_dir", full_path=True) | ||
output = mock_stdout.getvalue().strip() | ||
expected_output = "\n".join( | ||
[ | ||
f"|-- {os.path.join('test_dir', 'file1.txt')}", | ||
f"|-- {os.path.join('test_dir', 'file2.txt')}", | ||
f"|-- {os.path.join('test_dir', 'sub_dir1')}", | ||
f"| |-- {os.path.join('test_dir', 'sub_dir1', 'file3.txt')}", | ||
f"|-- {os.path.join('test_dir', 'sub_dir2')}", | ||
] | ||
) | ||
self.assertEqual(output, expected_output) | ||
|
||
@patch("sys.stdout", new_callable=StringIO) | ||
def test_tree_show_permissions(self, mock_stdout): | ||
tree("test_dir", show_permissions=True) | ||
output = mock_stdout.getvalue().strip() | ||
self.assertIn("|-- ", output) | ||
self.assertIn(" file1.txt", output) | ||
self.assertIn(" sub_dir1", output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import List | ||
import os | ||
from way3.utils.ignore_rule import parse_gitignore, is_gitignored | ||
|
||
|
||
def find_files_by_name( | ||
search_string: str, directory=".", should_ignore=True, ignore_file_path=None | ||
) -> List[str]: | ||
""" | ||
寻找某个路径下,文件名包含某个字符串的所有文件名绝对路径列表 | ||
""" | ||
matching_files = [] | ||
|
||
ignore_file_path = ( | ||
ignore_file_path | ||
if ignore_file_path is not None | ||
else os.path.join(".", ".gitignore") | ||
) | ||
gitignore_rules = parse_gitignore(ignore_file_path) | ||
|
||
for root, dirs, files in os.walk(directory): | ||
for file in files: | ||
if search_string in file: | ||
file_path = os.path.join(root, file) | ||
# 检查文件路径是否匹配.gitignore中的规则 | ||
if should_ignore: | ||
if not is_gitignored(file_path, gitignore_rules): | ||
matching_files.append(file_path) | ||
else: | ||
matching_files.append(file_path) | ||
|
||
return matching_files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import List | ||
import os | ||
from way3.utils.ignore_rule import parse_gitignore, is_gitignored | ||
|
||
|
||
def find_folders_by_name( | ||
directory: str, search_string: str, should_ignore=True, ignore_file_path=None | ||
) -> List[str]: | ||
""" | ||
寻找某个路径下,文件夹包含某个字符串的所有文件夹列表 | ||
""" | ||
matching_folders = [] | ||
|
||
ignore_file_path = ( | ||
ignore_file_path | ||
if ignore_file_path is not None | ||
else os.path.join(".", ".gitignore") | ||
) | ||
gitignore_rules = parse_gitignore(ignore_file_path) | ||
|
||
for root, dirs, files in os.walk(directory): | ||
for dir in dirs: | ||
dir_path = os.path.join(root, dir) | ||
# 检查文件夹路径是否匹配.gitignore中的规则 | ||
if should_ignore: | ||
if ( | ||
not is_gitignored(dir_path, gitignore_rules) | ||
and search_string in dir | ||
): | ||
matching_folders.append(dir_path) | ||
else: | ||
if search_string in dir: | ||
matching_folders.append(dir_path) | ||
|
||
return matching_folders |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import os | ||
|
||
|
||
def tree( | ||
directory, | ||
prefix="", | ||
show_hidden=False, | ||
max_depth=None, | ||
full_path=False, | ||
sort_by_size=False, | ||
show_permissions=False, | ||
current_depth=0, | ||
): | ||
# 先获取目录中的所有文件和文件夹 | ||
entries = os.listdir(directory) | ||
|
||
if not show_hidden: | ||
entries = [entry for entry in entries if not entry.startswith(".")] | ||
|
||
if sort_by_size: | ||
entries.sort(key=lambda entry: os.path.getsize(os.path.join(directory, entry))) | ||
else: | ||
entries.sort() | ||
|
||
files = [] | ||
dirs = [] | ||
for entry in entries: | ||
full_path_entry = os.path.join(directory, entry) | ||
if os.path.isdir(full_path_entry): | ||
dirs.append(entry) | ||
else: | ||
files.append(entry) | ||
|
||
def format_entry(entry, path): | ||
if full_path: | ||
entry = path | ||
if show_permissions: | ||
entry = f"{oct(os.stat(path).st_mode)[-3:]} {entry}" | ||
return entry | ||
|
||
# 打印当前目录下的文件 | ||
for file in files: | ||
file_path = os.path.join(directory, file) | ||
print(prefix + "|-- " + format_entry(file, file_path)) | ||
|
||
# 递归打印子目录 | ||
if max_depth is None or current_depth < max_depth: | ||
for i, dir in enumerate(dirs): | ||
dir_path = os.path.join(directory, dir) | ||
print(prefix + "|-- " + format_entry(dir, dir_path)) | ||
new_prefix = prefix + "| " if i < len(dirs) - 1 else prefix + " " | ||
tree( | ||
dir_path, | ||
new_prefix, | ||
show_hidden, | ||
max_depth, | ||
full_path, | ||
sort_by_size, | ||
show_permissions, | ||
current_depth + 1, | ||
) | ||
|
||
|
||
# 使用示例 | ||
# tree( | ||
# "./way3", | ||
# show_hidden=True, | ||
# max_depth=2, | ||
# full_path=False, | ||
# sort_by_size=True, | ||
# show_permissions=False, | ||
# ) |