forked from staticshreyas/Python_Project-File-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
171 lines (154 loc) · 6.03 KB
/
manager.py
File metadata and controls
171 lines (154 loc) · 6.03 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
from tkinter import *
from shutil import copy2, copytree, move, rmtree
class Manager:
def __init__(self):
self.current_root = os.getcwd()
self.current_root_1 = os.getcwd()
self.current_root_2 = os.getcwd()
self.parts = self.current_root.split('/')
self.dirs = None
self.files = None
self.new_name = None
def get_tree(self, lb):
self.lb = lb
self.current_root = self.get_active_lb_root()
self.lb.delete(0, END)
for self.root, self.dirs, self.files in os.walk(self.current_root):
self.lb.insert(END, '..')
for d in self.dirs:
self.lb.insert(END, d)
for f in self.files:
self.lb.insert(END, f)
break
def rename_file(self, *args):
lb, new_name, del_edit_spot, refresh_lb, old_name = args
self.get_tree(lb)
self.current_root = self.get_active_lb_root()
os.chdir(self.current_root)
if old_name != new_name: os.rename(old_name, new_name)
del_edit_spot()
refresh_lb()
def get_selected(self):
i = self.lb.curselection()[0]
self.selected_item = self.lb.get(i)
return self.selected_item
def double_click(self, lb):
self.lb = lb
self.get_selected()
self.get_tree(lb)
self.current_root = self.check_lb_to_path_edit(self.edit_path())
os.chdir(self.current_root)
self.get_tree(self.lb)
def edit_path(self):
if self.selected_item == '..':
parts = self.current_root.split('/')
last_part = f'/{parts[-1]}'
self.current_root = self.current_root.replace(last_part, '')
return self.current_root
elif self.selected_item in self.dirs:
self.current_root = f'{self.current_root}/{self.selected_item}'
return self.current_root
elif self.selected_item in self.files:
return self.current_root
def check_lb_to_path_edit(self, p):
if self.lb.__str__()[-1] == "2":
self.current_root_2 = p
return self.current_root_2
else:
self.current_root_1 = p
return self.current_root_1
def get_active_lb_root(self, **kwargs):
if kwargs: self.lb = kwargs['lb']
if self.lb.__str__()[-1] == "2":
self.current_root = self.current_root_2
return self.current_root_2
else:
self.current_root = self.current_root_1
return self.current_root_1
def do_copy_name(self, name):
if re.search(r'\(\d\)', name):
if name in self.dirs: # w lb_copy_here
to_change = re.findall(r'\(\d+\)', name)[-1]
nr_copy = int(to_change[1:-1]) + 1
no_change = name[0:-len(to_change)]
self.new_name = no_change + to_change.replace(to_change, f'({nr_copy})')
elif name in self.files:
to_change = re.findall(r'\(\d+\).*\.', name)[-1]
string_slices = name.rpartition(to_change)
first_bracket = to_change[0]
last_bracet = to_change.index(')')
nr = to_change[1:last_bracet]
end = to_change[last_bracet:]
to_change = f'{first_bracket}{1 + int(nr)}{end}'
self.new_name = string_slices[0] + to_change + string_slices[-1]
else:
self.new_name = name
if os.path.isdir(f'{self.current_root}/{self.new_name}') or \
os.path.isfile(f'{self.current_root}/{self.new_name}'):
return self.do_copy_name(self.new_name)
return self.new_name
elif os.path.isdir(name):
self.new_name = name + '(1)'
if os.path.isdir(f'{self.current_root}/{self.new_name}'):
return self.do_copy_name(self.new_name)
return self.new_name
elif os.path.isfile(name):
self.new_name = re.sub(r'(\.)', r'(1)\1', name)
if os.path.isfile(f'{self.current_root}/{self.new_name}'):
return self.do_copy_name(self.new_name)
return self.new_name
def subject_copy(self, name, lb_copy_here, refresh_lb):
self.lb = lb_copy_here
self.current_root = self.get_active_lb_root()
if self.current_root == self.current_root_1:
os.chdir(self.current_root_2)
else:
os.chdir(self.current_root_1)
self.get_tree(lb_copy_here)
new_name = self.do_copy_name(name)
if os.path.isdir(name):
copytree(name, f'{self.current_root}/{new_name}')
elif os.path.isfile(name):
copy2(name, f'{self.current_root}/{new_name}')
refresh_lb()
def delete_subject(self, item, lb, refresh_lb):
self.get_tree(lb)
os.chdir(self.current_root)
if item in self.dirs:
rmtree(item)
elif item in self.files:
os.remove(item)
refresh_lb()
def paste_subject(self, cuted, lb, refresh_lb):
self.lb = lb
path = cuted['path']
item = cuted['item']
cut_it = path + '/' + item
self.current_root = self.get_active_lb_root()
os.chdir(self.current_root)
if path != self.current_root:
move(cut_it, self.current_root)
refresh_lb()
def add_dir(self, *args):
lb, name, del_edit_spot, refresh_lb, item = args
self.refresh_when_add(lb)
try:
os.mkdir(name)
del_edit_spot()
refresh_lb()
except FileExistsError:
print(f'Katalog {name} istnieje')
def add_file(self, *args):
lb, name, del_edit_spot, refresh_lb, item = args
self.refresh_when_add(lb)
try:
os.mknod(name)
del_edit_spot()
refresh_lb()
except FileExistsError:
print(f'Plik {name} istnieje')
def refresh_when_add(self, lb):
self.get_tree(lb)
self.current_root = self.get_active_lb_root()
os.chdir(self.current_root)