Skip to content

Commit

Permalink
feat: add get_files_in_directory
Browse files Browse the repository at this point in the history
  • Loading branch information
aboutmydreams committed May 16, 2024
1 parent e8a85ec commit b3efee2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="way3",
version="0.0.19",
version="0.0.20",
author="aboutmydreams",
author_email="[email protected]",
description="Simplified file path management for Python developers",
Expand Down
5 changes: 5 additions & 0 deletions way3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@

# File Find
from .file_find.current_dir import get_current_dir as get_current_dir # noqa: E402
from .file_find.traverse_files_from_folder import parse_gitignore as parse_gitignore # noqa: E402
from .file_find.traverse_files_from_folder import is_gitignored as is_gitignored # noqa: E402
from .file_find.traverse_files_from_folder import ( # noqa: E402
get_files_in_directory as get_files_in_directory,
)
80 changes: 80 additions & 0 deletions way3/file_find/traverse_files_from_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import os
import fnmatch


def parse_gitignore(
gitignore_path=".gitignore", ignored_files=[".git/", ".venv/", ".github/"]
):
"""
解析 .gitignore 文件,返回忽略规则列表
"""

if os.path.exists(gitignore_path):
with open(gitignore_path, "r") as gitignore_file:
for line in gitignore_file:
line = line.strip()
if line and not line.startswith("#"):
ignored_files.append(line)
return ignored_files


def is_gitignored(file_path, gitignore_rules):
"""
Check if a file path is ignored according to the given gitignore rules.
Args:
file_path (str): The absolute path to the file.
gitignore_rules (list): List of gitignore rules.
Returns:
bool: True if the file is ignored, False otherwise.
"""
for rule in gitignore_rules:
# Handle comments and empty lines
if not rule or rule.startswith("#"):
continue
# Handle negated rules
negated = False
if rule.startswith("!"):
negated = True
rule = rule[1:]

# Check if the file matches the rule
if fnmatch.fnmatch(file_path, rule):
return not negated
if rule in str(file_path):
return not negated

return False


def get_files_in_directory(directory, should_ignore=True, ignore_file_path=None):
"""
获取指定目录下所有文件,排除.gitignore中列出的文件
"""

file_list = []
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)
print(ignore_file_path)

for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
# 检查文件路径是否匹配.gitignore中的规则
if should_ignore:
if not is_gitignored(file_path, gitignore_rules):
file_list.append(file_path)
else:
file_list.append(file_path)
return file_list


# Example usage 获取当前所有排出掉 gitignore 中的文件
# current_directory = "." # 当前目录
# files = get_files_in_directory(current_directory)
# print(files)

0 comments on commit b3efee2

Please sign in to comment.