-
Notifications
You must be signed in to change notification settings - Fork 27
/
imageprofile.py
50 lines (42 loc) · 1.49 KB
/
imageprofile.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
import yaml
import glob
from pathlib import Path
from tkinter import *
from tkinter.filedialog import asksaveasfile, askopenfile
from tools import bundle_dir
IMAGE_PROFILE_PATH = Path(bundle_dir, 'profiles')
def open_image_profile():
root = Tk()
root.withdraw()
yam_file = ''
file = askopenfile(initialdir=str(IMAGE_PROFILE_PATH), filetypes = (("YAML files","*.yaml"),("all files","*.*")), defaultextension=".yaml")
try:
yam_file = yaml.safe_load(file)
except yaml.YAMLError as exc:
print(exc)
file.close()
root.destroy()
return yam_file
def export_image_profile(profile):
root = Tk()
root.withdraw()
file = asksaveasfile(initialdir=str(IMAGE_PROFILE_PATH), mode='w', filetypes = (("YAML files","*.yaml"),("all files","*.*")), defaultextension=".yaml")
if file is None: # asksaveasfile return `None` if dialog closed with "cancel".
return
with open(file.name, 'w') as outfile:
yaml.dump(profile, outfile, sort_keys=False, default_flow_style=False)
file.close()
root.destroy()
return file.name
def load_image_profiles():
files = glob.glob(str(Path(IMAGE_PROFILE_PATH, '*.yaml')))
profiles = []
for file in files:
with open(file, 'r') as stream:
try:
yam_file = yaml.safe_load(stream)
yam_file['name'] = Path(file).stem
profiles.append(yam_file)
except yaml.YAMLError as exc:
print(exc)
return profiles