-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileMerge.py
More file actions
324 lines (304 loc) · 13.4 KB
/
FileMerge.py
File metadata and controls
324 lines (304 loc) · 13.4 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import os, shutil, datetime, json
from typing import List
from StringManage import cut_and_pad_string
USAGE = '''
Usage: python py-Merge.py [OPTIONS]
Options:
-l, --load-config <path> Load options from a configuration file.
if not provided, the script will look for a file named "merge_config.json" in the current directory.
-s, --save-config <path> Save options to a configuration file.
if not provided, the script will save the options to a file named "merge_config.json" in the current directory.
-t, --target-path <path> Path to the directory where the files to be merged are located.
-o, --output-path <path> Path to the directory where the merged file will be saved.
-e, --extensions <ext1,ext2,ext3> Comma-separated list of file extensions to be merged, without the dot.
-p, --rename-pattern <pattern> Pattern to be used to rename the merged file.
The pattern can include the following placeholders:
{name} Name of the merged file (without extension).
{ext} Extension of the merged file.
{index} 0-based index of the file being merged.
{date} Current date in the format "YYYY-MM-DD".
{time} Current time in the format "HH-MM-SS".
{root} Root directory of the file being merged.
placeholders can be used together, for example: "merged_{name}_{index}.{ext}".
If not provided, the merged file will be named "{index}.{ext}".
-w, --overwrite Overwrite existing paths and files.
-r, --run run the script.
-h, --help Show this help message and exit.
-d, --debug Enable debug mode.
'''
FULL_COMMAND_2_SHORT_COMMAND = {
'--load-config': '-l',
'--save-config': '-s',
'--target-path': '-t',
'--output-path': '-o',
'--extensions': '-e',
'--rename-pattern': '-p',
'--overwrite': '-w',
'--run': '-r',
'--help': '-h',
'--debug': '-d'
}
class FileMerger:
def __init__(self, target_path:str=None, output_path:str=None, extensions:List[str]=None, overwrite:bool=False, rename_pattern:str=None, debug:bool=False) -> None:
self._set_debug(debug)
if debug:
os.system('color')
if target_path is None:
target_path = os.getcwd()
if output_path is None:
output_path = target_path + '_merged'
if rename_pattern is None:
rename_pattern = '{index}.{ext}'
self._set_target_path(target_path)
self._set_output_path(output_path)
self._set_extensions(extensions)
self._set_rename_pattern(rename_pattern)
self._SetOriFileName(None)
self._SetTargetFileName(None)
self._set_ori_file_root(None)
self._set_index(0)
self._set_file_list([])
self._set_path_list([])
self._set_file_sum(0)
self._set_path_sum(0)
self._set_index_width(0)
self._set_overwrite(overwrite)
#region Setters
def _set_overwrite(self, overwrite:bool) -> None:
self.overwrite = overwrite
def _set_debug(self, debug:bool) -> None:
self.debug = debug
def _set_target_path(self, target_path:str) -> None:
self.target_path = target_path
def _set_output_path(self, output_path:str) -> None:
self.output_path = output_path
def _set_extensions(self, extensions:List[str]) -> None:
self.extensions = extensions
def _set_rename_pattern(self, rename_pattern:str) -> None:
self.rename_pattern = rename_pattern
def _SetOriFileName(self, file_name:str) -> None:
self.ori_file_name = file_name
def _SetTargetFileName(self, file_name:str) -> None:
self.target_file_name = file_name
def _set_ori_file_root(self, file_root:str) -> None:
self.ori_file_root = file_root
def _set_index(self, index:int) -> None:
self.index = index
def _set_file_list(self, file_list:List[str]) -> None:
self._set_file_sum(len(file_list))
self._set_index_width(len(str(self._get_file_sum())))
self.file_list = file_list
def _set_file_sum(self, file_sum:int) -> None:
self.file_sum = file_sum
def _set_path_list(self, path_list:List[str]) -> None:
self._set_path_sum(len(path_list))
self.path_list = path_list
def _set_path_sum(self, path_sum:int) -> None:
self.path_sum = path_sum
def _set_index_width(self, index_width:int) -> None:
self.index_width = index_width
#endregion
#region Getters
def _get_overwrite(self) -> bool:
return self.overwrite
def _get_debug(self) -> bool:
return self.debug
def _get_target_path(self) -> str:
return self.target_path
def _get_output_path(self) -> str:
return self.output_path
def _get_extensions(self) -> List[str]:
return self.extensions
def _get_rename_pattern(self) -> str:
return self.rename_pattern
def _get_ori_file_name(self) -> str:
return self.ori_file_name
def _get_target_file_name(self) -> str:
return self.target_file_name
def _get_ori_file_root(self) -> str:
return self.ori_file_root
def _get_index(self) -> int:
return self.index
def _get_file_list(self) -> List[str]:
return self.file_list
def _get_file_sum(self) -> int:
return self.file_sum
def _get_path_list(self) -> List[str]:
return self.path_list
def _get_path_sum(self) -> int:
return self.path_sum
def _get_index_width(self) -> int:
return self.index_width
#endregion
def _next_index(self) -> None:
self.index += 1
def _pop_file(self) -> str:
if self._get_file_sum() == 0:
return None
else:
self._add_file_sum(-1)
return self.file_list.pop(0)
def _pop_path(self) -> str:
if self._get_path_sum() == 0:
return None
else:
self._add_path_sum(-1)
return self.path_list.pop(0)
def _add_file(self, file_name:str) -> None:
self._add_file_sum(1)
self.file_list.append(file_name)
def _add_path(self, path:str) -> None:
self._add_path_sum(1)
self.path_list.append(path)
def _add_file_sum(self, index:int) -> None:
self.file_sum += index
def _add_path_sum(self, index:int) -> None:
self.path_sum += index
def _generate_next_file_name(self) -> None:
name_ = '.'.join(self._get_ori_file_name().split('.')[:-1])
ext_ = self._get_ori_file_name().split('.')[-1]
self._SetTargetFileName(self._get_rename_pattern().format(
name=name_,
ext=ext_,
index=str(self._get_index()).zfill(self._get_index_width()),
date=datetime.datetime.now().strftime('%Y-%m-%d'),
time=datetime.datetime.now().strftime('%H-%M-%S'),
root=self._get_ori_file_root()))
self._next_index()
def _search_files(self) -> None:
if not os.path.isdir(self._get_target_path()):
print(f'Target path not found: {self._get_target_path()}')
return
self._set_path_list([self._get_target_path()])
while self._get_path_sum() > 0:
path_ = self._pop_path()
for file_name in os.listdir(path_):
if os.path.isfile(os.path.join(path_, file_name)):
if self._get_extensions() is None or file_name.split('.')[-1] in self._get_extensions():
self._add_file(os.path.join(path_, file_name))
else:
if self._get_debug():
print(f'Skipping file \033[33m{cut_and_pad_string(file_name, 30)}\033[0m (extension not in \033[33m{self._get_extensions()}\033[0m)')
else:
self._add_path(os.path.join(path_, file_name))
self._set_index_width(len(str(self._get_file_sum())))
def _merge_files(self) -> None:
if self._get_file_sum() == 0:
print('No files to merge')
return
else:
if not os.path.isdir(self._get_output_path()):
os.makedirs(self._get_output_path())
while self._get_file_sum() > 0:
file_name_ = self._pop_file()
self._SetOriFileName(os.path.basename(file_name_))
self._set_ori_file_root(os.path.dirname(file_name_).split(os.sep)[-1])
self._generate_next_file_name()
if self._get_debug():
print(f'\033[33m⊕\033[36m{str(self._get_index()).zfill(self._get_index_width())}\033[0m-Merging file \033[33m{cut_and_pad_string(file_name_, 30)}\033[0m to \033[33m{cut_and_pad_string(os.path.join(self._get_output_path(), self._get_target_file_name()), 30)}\033[0m', end='')
shutil.copy(file_name_, os.path.join(self._get_output_path(), self._get_target_file_name()))
if self._get_debug():
print(f'\r\033[32m√\033[36m{str(self._get_index()).zfill(self._get_index_width())}\033[0m-Merging file \033[33m{cut_and_pad_string(file_name_, 30)}\033[0m to \033[33m{cut_and_pad_string(os.path.join(self._get_output_path(), self._get_target_file_name()), 30)}\033[0m')
def run(self) -> None:
if self._get_overwrite():
if os.path.isdir(self._get_output_path()):
shutil.rmtree(self._get_output_path())
self._search_files()
self._merge_files()
def load_config(self, config_path:str) -> None:
if not os.path.isfile(config_path):
print(f'Config file not found: {config_path}')
return
with open(config_path, 'r') as f:
config_dict = json.load(f)
self._set_target_path(config_dict['target_path'])
self._set_output_path(config_dict['output_path'])
self._set_extensions(config_dict['extensions'])
self._set_rename_pattern(config_dict['rename_pattern'])
self._set_debug(config_dict['debug'])
def save_config(self, config_path:str) -> None:
config_dict = {
'target_path': self._get_target_path(),
'output_path': self._get_output_path(),
'extensions': self._get_extensions(),
'rename_pattern': self._get_rename_pattern(),
'debug': self._get_debug()
}
with open(config_path, 'w') as f:
json.dump(config_dict, f, indent=4)
if __name__ == '__main__':
import sys
if len(sys.argv) == 1:
print('No options provided')
print('Type "python py-Merge.py -h" for help')
else:
command_dict = {
'-l' : None,#load_config,
'-s' : None,#save_config,
'-t' : None,#SetTargetPath,
'-o' : None,#SetOutputPath,
'-e' : None,#SetExtensions,
'-p' : None,#SetRenamePattern,
'-w' : None,#SetOverwrite,
'-r' : None,#run,
'-h' : None,#ShowHelp,
'-d' : None #SetDebug
}
max_arg_index = len(sys.argv) - 1
i = 1
while i <= max_arg_index:
arg = sys.argv[i]
if arg in FULL_COMMAND_2_SHORT_COMMAND:
arg = FULL_COMMAND_2_SHORT_COMMAND[arg]
if arg in command_dict:
i += 1
value_list = []
while i <= max_arg_index and sys.argv[i][0] != '-':
if sys.argv[i][0] in ['"', "'"] and sys.argv[i][-1] == sys.argv[i][0]:
value_list.append(sys.argv[i][1:-1])
else:
value_list.append(sys.argv[i])
i += 1
if len(value_list) == 0:
if arg in ['-l', '-s', '-t', '-o', '-e', '-p']:
print(f'Error: {arg} option requires a value')
print('Type "python py-Merge.py -h" for help')
break
elif len(value_list) == 1:
if arg in ['-w', '-r', '-h', '-d']:
print(f'Error: {arg} option does not require a value')
print('Type "python py-Merge.py -h" for help')
break
elif len(value_list) > 1:
if arg in ['-l', '-s', '-t', '-o', '-p', 'w', '-r', '-h', '-d']:
print(f'Error: {arg} option can only have one value')
print('Type "python py-Merge.py -h" for help')
break
if len(value_list) == 0:
command_dict[arg] = True
elif len(value_list) == 1:
command_dict[arg] = value_list[0]
else:
command_dict[arg] = value_list
else:
print(f'Error: Unknown option: {arg}')
print('Type "python py-Merge.py -h" for help')
# Create MergeFile object
# Based on the command_dict, set the corresponding options
if command_dict['-h']:
print(USAGE)
else:
MyFileMerger = FileMerger(
target_path=command_dict['-t'],
output_path=command_dict['-o'],
extensions=command_dict['-e'],
overwrite=command_dict['-w'],
rename_pattern=command_dict['-p'],
debug=command_dict['-d']
)
if command_dict['-l']:
MyFileMerger.load_config(command_dict['-l'])
if command_dict['-s']:
MyFileMerger.save_config(command_dict['-s'])
if command_dict['-r']:
MyFileMerger.run()