Initial upload: TermDiagram v0.1.0
This commit is contained in:
32
src/termdiagram/utils/file_utils.py
Normal file
32
src/termdiagram/utils/file_utils.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
|
||||
class FileUtils:
|
||||
@staticmethod
|
||||
def get_files(
|
||||
directory: str, extensions: List[str] = None, exclude: List[str] = None
|
||||
) -> List[Path]:
|
||||
exclude = exclude or []
|
||||
files = []
|
||||
|
||||
for path in Path(directory).rglob("*"):
|
||||
if path.is_file():
|
||||
if extensions:
|
||||
if path.suffix not in extensions:
|
||||
continue
|
||||
|
||||
if any(str(path).startswith(e) for e in exclude):
|
||||
continue
|
||||
|
||||
files.append(path)
|
||||
|
||||
return files
|
||||
|
||||
@staticmethod
|
||||
def read_file(file_path: str) -> str:
|
||||
try:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
except (IOError, UnicodeDecodeError):
|
||||
return ""
|
||||
Reference in New Issue
Block a user