-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilerRepository.py
More file actions
69 lines (60 loc) · 2.6 KB
/
Copy pathFilerRepository.py
File metadata and controls
69 lines (60 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import shutil
from BaseNodeObject import DirectoryNodeObject, FileNodeObject
class FilerRepository:
"""ローカルのファイルシステムからノード(オブジェクト)を取得し、操作するラッパー"""
def get_directory_node(self, dir_path: str) -> DirectoryNodeObject:
"""指定されたパスのディレクトリノードを作成し、中身のノード群を詰めて返す"""
dir_node = DirectoryNodeObject(dir_path)
if not os.path.isdir(dir_path):
return dir_node
try:
for entry in os.scandir(dir_path):
if entry.is_dir():
dir_node.children.append(DirectoryNodeObject(entry.path))
else:
dir_node.children.append(FileNodeObject(entry.path))
except PermissionError:
pass # アクセス権限がない場合は空のまま返す
return dir_node
def create_directory(self, parent_path: str, name: str) -> bool:
"""指定された親ディレクトリ配下に新しいディレクトリを作成する"""
target_path = os.path.join(parent_path, name)
try:
os.makedirs(target_path, exist_ok=False)
return True
except Exception:
return False
def create_file(self, parent_path: str, name: str) -> bool:
"""指定された親ディレクトリ配下に新しい空ファイルを作成する"""
target_path = os.path.join(parent_path, name)
try:
# ファイルが既に存在する場合はエラーになるように 'x' モードを使用
with open(target_path, 'x'):
pass
return True
except Exception:
return False
def rename_node(self, node_path: str, new_name: str) -> bool:
"""ノードの名前を変更する"""
if not os.path.exists(node_path):
return False
parent_dir = os.path.dirname(node_path)
new_path = os.path.join(parent_dir, new_name)
try:
os.rename(node_path, new_path)
return True
except Exception:
return False
def delete_node(self, node_path: str) -> bool:
"""ノード(ファイルまたはディレクトリ)を削除する"""
if not os.path.exists(node_path):
return False
try:
if os.path.isdir(node_path):
shutil.rmtree(node_path)
else:
os.remove(node_path)
return True
except Exception:
return False