This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
111 lines (82 loc) · 3.16 KB
/
utils.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
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
import tkinter as tk
import tempfile
import os
import shutil
import sys
from PIL import Image
import io
import widgets
class Page:
'''A page object that contains the path to an image file, and the loaded thumbnail of that file'''
size = 250
def __init__(self, path):
self.path = path
self.name = os.path.splitext(os.path.basename(self.path))[0]
self.thumb = self.make_thumbnail()
# Returns None if no image can be loaded
def make_thumbnail(self):
try:
image = Image.open(self.path)
image.thumbnail((Page.size, Page.size))
b = io.BytesIO()
image.save(b, 'gif')
return tk.PhotoImage(data=b.getvalue())
except (OSError, IsADirectoryError) as err:
print("Error Loading Image: {0}".format(err))
return None
# TODO: include group image
class PageGroup:
'''An object that holds a group of pages'''
def __init__(self, pages):
self.pages = pages
self.name = "Group"
def _create_backup(files, path):
backup = tempfile.TemporaryDirectory()
for file in files:
origin = os.path.join(path, file)
destination = os.path.join(backup.name, file)
if os.path.isfile(origin):
shutil.copy2(origin, destination)
else:
shutil.copytree(origin, destination)
return backup
def _restore_backup(backup, path):
if os.path.exists(path):
shutil.rmtree(path)
shutil.copytree(backup.name, path)
def rename_files(path, start_number, prefix):
files = os.listdir(path)
files.sort()
progress = widgets.ProgressPopup("Renaming Files", len(files))
progress.log_message("Creating backup")
backup = _create_backup(files, path)
digits = len(os.listdir(path)) + start_number
# Create temporary directory for renaming
renamed_directory = path + "_renamed"
if os.path.exists(renamed_directory):
shutil.rmtree(renamed_directory)
os.mkdir(renamed_directory)
try:
progress.log_message("Copying Files")
# Move all of the files to a new directory, with new names
for file in files:
origin = os.path.join(path, file)
if os.path.isfile(origin):
end = os.path.splitext(file)[1]
file_name = str(start_number).zfill(len(str(digits))) + end
file_name = prefix + file_name
dest = os.path.join(renamed_directory, file_name)
shutil.copyfile(origin, dest)
progress.next()
progress.log_message("Renamed {0} as {1}".format(origin, file_name))
start_number += 1
else:
dest = os.path.join(renamed_directory, file)
shutil.copytree(origin, dest)
progress.next()
progress.log_message("Did not modify {0}".format(origin))
progress.log_message("Rename Completed")
except:
progress.log_message("Rename failed, restoring backup")
_restore_backup(backup, path)
progress.destroy()