Skip to content

Commit

Permalink
feat: add get_directory_file_list and get_directory_folder_list
Browse files Browse the repository at this point in the history
  • Loading branch information
aboutmydreams committed May 17, 2024
1 parent 6ccc3a9 commit 8cc80bc
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 49 deletions.
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.24",
version="0.0.25",
author="aboutmydreams",
author_email="[email protected]",
description="Simplified file path management for Python developers",
Expand Down
45 changes: 44 additions & 1 deletion tests/test_file_find.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from way3 import get_current_dir, get_files_in_directory
from way3 import (
get_current_dir,
get_files_in_directory,
get_directory_structure,
get_directory_folder_list,
get_directory_file_list,
)

import unittest
import os


class TestFileFind(unittest.TestCase):
Expand All @@ -15,3 +22,39 @@ def test_get_files_in_directory(self):
"tests/test_file_find.py", files, "result fail: expected test_file_find.py"
)
self.assertEqual(len(files) > 1, True, "result fail: expected > 3")

def test_directory_structure(self):
# 示例:获取当前文件夹的目录结构
directory_structure = get_directory_structure(os.getcwd() + "/tests")
self.assertIn(
"dirs",
list(directory_structure[get_current_dir(__file__)].keys()),
"result fail: expected dirs",
)
self.assertEqual(
list,
type(directory_structure[get_current_dir(__file__)]["dirs"]),
"result fail: expected dirs",
)
self.assertIn(
"files",
list(directory_structure[get_current_dir(__file__)].keys()),
"result fail: expected files",
)
self.assertEqual(
list,
type(directory_structure[get_current_dir(__file__)]["files"]),
"result fail: expected files",
)

def test_get_directory_folder_list(self):
directory_list = get_directory_folder_list(os.getcwd())
self.assertIn(
"tests", directory_list, "result fail: not find tests in directory_list"
)

def test_get_directory_file_list(self):
file_list = get_directory_file_list(os.getcwd())
self.assertIn(
"LICENSE", file_list, "result fail: not find LICENSE in file_list"
)
9 changes: 9 additions & 0 deletions way3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@
from .file_find.traverse_files_from_folder import ( # noqa: E402
get_files_in_directory as get_files_in_directory,
)
from .file_find.traverse_files_from_folder import ( # noqa: E402
get_directory_structure as get_directory_structure,
)
from .file_find.traverse_files_from_folder import ( # noqa: E402
get_directory_folder_list as get_directory_folder_list,
)
from .file_find.traverse_files_from_folder import ( # noqa: E402
get_directory_file_list as get_directory_file_list,
)
83 changes: 36 additions & 47 deletions way3/file_find/traverse_files_from_folder.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,6 @@
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
from typing import Dict, List
from ..utils.ignore_rule import parse_gitignore, is_gitignored


def get_files_in_directory(directory, should_ignore=True, ignore_file_path=None):
Expand Down Expand Up @@ -78,3 +33,37 @@ def get_files_in_directory(directory, should_ignore=True, ignore_file_path=None)
# current_directory = "." # 当前目录
# files = get_files_in_directory(current_directory)
# print(files)


def get_directory_structure(path) -> Dict[str, Dict[str, List[str]]]:
"""
获取指定路径下的目录结构
:param path: 路径
:return: 目录结构
"""
directory_structure = {}
for root, dirs, files in os.walk(path):
directory_structure[root] = {"dirs": dirs, "files": files}
return directory_structure


def get_directory_folder_list(path) -> List[str]:
"""
获取指定路径下的目录结构
:param path: 路径
:return: 目录结构
"""
directory_structure = get_directory_structure(path)
absolute_path = list(directory_structure.keys())[0]
return directory_structure[absolute_path]["dirs"]


def get_directory_file_list(path) -> List[str]:
"""
获取指定路径下的目录结构
:param path: 路径
:return: 目录结构
"""
directory_structure = get_directory_structure(path)
absolute_path = list(directory_structure.keys())[0]
return directory_structure[absolute_path]["files"]
48 changes: 48 additions & 0 deletions way3/utils/ignore_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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

0 comments on commit 8cc80bc

Please sign in to comment.