-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
62 lines (51 loc) · 2.04 KB
/
common.py
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
import os
import re
class File:
path = ''
filename_structure = ''
original_index = -1
new_index = -1
number_digits = -1
def __init__(self, file, path):
base, ext = os.path.splitext(file)
# (\d+)
self.path = path + '/' + file
res = re.findall(r'\d+', base)
if len(res) != 1:
raise Exception('Found more than 1 number in the file name. Help')
self.original_index = int(res[-1])
self.number_digits = len(res[-1])
m = re.search(r'\d+', base)
number_repl = '{{:0{}d}}'.format(self.number_digits)
self.filename_structure = path + '/' + base[0 : m.start()] + number_repl + base[m.end():-1] + ext
def __repr__(self):
return '{{ Path: {}, Original Index: {}, New Index: {}'.format(self.path, self.original_index, self.new_index)
def verify_files(files):
# Make sure that no indices are used twice
indices = []
for f in files:
if f.original_index in indices:
raise Exception('Duplicate index found {} ({})'.format(f.original_index, f.path))
indices.append(f.original_index)
indices = None
# Make sure that there is only one type of filename structure
filename_structure = files[0].filename_structure
for f in files:
if f.filename_structure != filename_structure:
raise Exception('Different file structures found: {} / {}'.format(filename_structure, f.filename_structure))
filename_structure = None
# Make sure the number of digits are the same for all
number_digits = files[0].number_digits
for f in files:
if f.number_digits != number_digits:
raise Exception('Different number of digits found: {} / {}'.format(number_digits, f.number_digits))
number_digits = None
# Sanity check for filename structure
for f in files:
new_file_path = f.filename_structure.format(f.original_index)
if f.path != new_file_path:
raise Exception('Somethings up with the path: {} / {}'.format(f.path, new_file_path))
def load_files(path):
files = [File(f, path) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
verify_files(files)
return files