Skip to content

Commit 8cc80bc

Browse files
committed
feat: add get_directory_file_list and get_directory_folder_list
1 parent 6ccc3a9 commit 8cc80bc

File tree

5 files changed

+138
-49
lines changed

5 files changed

+138
-49
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="way3",
8-
version="0.0.24",
8+
version="0.0.25",
99
author="aboutmydreams",
1010
author_email="aboutmydreams@163.com",
1111
description="Simplified file path management for Python developers",

tests/test_file_find.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
from way3 import get_current_dir, get_files_in_directory
1+
from way3 import (
2+
get_current_dir,
3+
get_files_in_directory,
4+
get_directory_structure,
5+
get_directory_folder_list,
6+
get_directory_file_list,
7+
)
28

39
import unittest
10+
import os
411

512

613
class TestFileFind(unittest.TestCase):
@@ -15,3 +22,39 @@ def test_get_files_in_directory(self):
1522
"tests/test_file_find.py", files, "result fail: expected test_file_find.py"
1623
)
1724
self.assertEqual(len(files) > 1, True, "result fail: expected > 3")
25+
26+
def test_directory_structure(self):
27+
# 示例:获取当前文件夹的目录结构
28+
directory_structure = get_directory_structure(os.getcwd() + "/tests")
29+
self.assertIn(
30+
"dirs",
31+
list(directory_structure[get_current_dir(__file__)].keys()),
32+
"result fail: expected dirs",
33+
)
34+
self.assertEqual(
35+
list,
36+
type(directory_structure[get_current_dir(__file__)]["dirs"]),
37+
"result fail: expected dirs",
38+
)
39+
self.assertIn(
40+
"files",
41+
list(directory_structure[get_current_dir(__file__)].keys()),
42+
"result fail: expected files",
43+
)
44+
self.assertEqual(
45+
list,
46+
type(directory_structure[get_current_dir(__file__)]["files"]),
47+
"result fail: expected files",
48+
)
49+
50+
def test_get_directory_folder_list(self):
51+
directory_list = get_directory_folder_list(os.getcwd())
52+
self.assertIn(
53+
"tests", directory_list, "result fail: not find tests in directory_list"
54+
)
55+
56+
def test_get_directory_file_list(self):
57+
file_list = get_directory_file_list(os.getcwd())
58+
self.assertIn(
59+
"LICENSE", file_list, "result fail: not find LICENSE in file_list"
60+
)

way3/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@
1313
from .file_find.traverse_files_from_folder import ( # noqa: E402
1414
get_files_in_directory as get_files_in_directory,
1515
)
16+
from .file_find.traverse_files_from_folder import ( # noqa: E402
17+
get_directory_structure as get_directory_structure,
18+
)
19+
from .file_find.traverse_files_from_folder import ( # noqa: E402
20+
get_directory_folder_list as get_directory_folder_list,
21+
)
22+
from .file_find.traverse_files_from_folder import ( # noqa: E402
23+
get_directory_file_list as get_directory_file_list,
24+
)
Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,6 @@
11
import os
2-
import fnmatch
3-
4-
5-
def parse_gitignore(
6-
gitignore_path=".gitignore", ignored_files=[".git/", ".venv/", ".github/"]
7-
):
8-
"""
9-
解析 .gitignore 文件,返回忽略规则列表
10-
"""
11-
12-
if os.path.exists(gitignore_path):
13-
with open(gitignore_path, "r") as gitignore_file:
14-
for line in gitignore_file:
15-
line = line.strip()
16-
if line and not line.startswith("#"):
17-
ignored_files.append(line)
18-
return ignored_files
19-
20-
21-
def is_gitignored(file_path, gitignore_rules):
22-
"""
23-
Check if a file path is ignored according to the given gitignore rules.
24-
25-
Args:
26-
file_path (str): The absolute path to the file.
27-
gitignore_rules (list): List of gitignore rules.
28-
29-
Returns:
30-
bool: True if the file is ignored, False otherwise.
31-
"""
32-
for rule in gitignore_rules:
33-
# Handle comments and empty lines
34-
if not rule or rule.startswith("#"):
35-
continue
36-
# Handle negated rules
37-
negated = False
38-
if rule.startswith("!"):
39-
negated = True
40-
rule = rule[1:]
41-
42-
# Check if the file matches the rule
43-
if fnmatch.fnmatch(file_path, rule):
44-
return not negated
45-
if rule in str(file_path):
46-
return not negated
47-
48-
return False
2+
from typing import Dict, List
3+
from ..utils.ignore_rule import parse_gitignore, is_gitignored
494

505

516
def get_files_in_directory(directory, should_ignore=True, ignore_file_path=None):
@@ -78,3 +33,37 @@ def get_files_in_directory(directory, should_ignore=True, ignore_file_path=None)
7833
# current_directory = "." # 当前目录
7934
# files = get_files_in_directory(current_directory)
8035
# print(files)
36+
37+
38+
def get_directory_structure(path) -> Dict[str, Dict[str, List[str]]]:
39+
"""
40+
获取指定路径下的目录结构
41+
:param path: 路径
42+
:return: 目录结构
43+
"""
44+
directory_structure = {}
45+
for root, dirs, files in os.walk(path):
46+
directory_structure[root] = {"dirs": dirs, "files": files}
47+
return directory_structure
48+
49+
50+
def get_directory_folder_list(path) -> List[str]:
51+
"""
52+
获取指定路径下的目录结构
53+
:param path: 路径
54+
:return: 目录结构
55+
"""
56+
directory_structure = get_directory_structure(path)
57+
absolute_path = list(directory_structure.keys())[0]
58+
return directory_structure[absolute_path]["dirs"]
59+
60+
61+
def get_directory_file_list(path) -> List[str]:
62+
"""
63+
获取指定路径下的目录结构
64+
:param path: 路径
65+
:return: 目录结构
66+
"""
67+
directory_structure = get_directory_structure(path)
68+
absolute_path = list(directory_structure.keys())[0]
69+
return directory_structure[absolute_path]["files"]

way3/utils/ignore_rule.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import fnmatch
3+
4+
5+
def parse_gitignore(
6+
gitignore_path=".gitignore", ignored_files=[".git/", ".venv/", ".github/"]
7+
):
8+
"""
9+
解析 .gitignore 文件,返回忽略规则列表
10+
"""
11+
12+
if os.path.exists(gitignore_path):
13+
with open(gitignore_path, "r") as gitignore_file:
14+
for line in gitignore_file:
15+
line = line.strip()
16+
if line and not line.startswith("#"):
17+
ignored_files.append(line)
18+
return ignored_files
19+
20+
21+
def is_gitignored(file_path, gitignore_rules):
22+
"""
23+
Check if a file path is ignored according to the given gitignore rules.
24+
25+
Args:
26+
file_path (str): The absolute path to the file.
27+
gitignore_rules (list): List of gitignore rules.
28+
29+
Returns:
30+
bool: True if the file is ignored, False otherwise.
31+
"""
32+
for rule in gitignore_rules:
33+
# Handle comments and empty lines
34+
if not rule or rule.startswith("#"):
35+
continue
36+
# Handle negated rules
37+
negated = False
38+
if rule.startswith("!"):
39+
negated = True
40+
rule = rule[1:]
41+
42+
# Check if the file matches the rule
43+
if fnmatch.fnmatch(file_path, rule):
44+
return not negated
45+
if rule in str(file_path):
46+
return not negated
47+
48+
return False

0 commit comments

Comments
 (0)