-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextractor.py
90 lines (72 loc) · 2.62 KB
/
extractor.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
from lxml import etree
import io
try:
try:
from PIL import PngImagePlugin
PngImagePlugin.MAX_TEXT_CHUNK = 1000000000
except ImportError:
pass
import Image
pil_available = True
except ImportError:
try:
from PIL import PngImagePlugin
PngImagePlugin.MAX_TEXT_CHUNK = 1000000000
from PIL import Image
pil_available = True
except ImportError:
print("Warning: Neither PIL nor Pillow is available. Checking of bundle structure is not possible.")
pil_available = False
if pil_available:
class KPP(object):
def __init__(self, filename, data=None):
self.filename = filename
self.data = data
def get_preset_text(self):
try:
if self.data is not None:
stream = io.BytesIO(self.data)
self.image = Image.open(stream)
else:
self.image = Image.open(self.filename)
except Exception as e:
print("Error: {}: can not read image: {}".format(self.filename, e))
return None
if self.image.format != 'PNG':
print("Error: {} is not a PNG file".format(self.filename))
return None
if self.image.text is None:
print("Error: {} does not contain text data".format(self.filename))
return None
if 'preset' not in self.image.text:
print("Error: {} does not contain Krita preset".format(self.filename))
return None
return self.image.text['preset']
def check(self):
text = self.get_preset_text()
if text is None:
return None
try:
preset = etree.fromstring(text)
return preset
except etree.XMLSyntaxError as e:
print("{} has invalid XML in preset info:\n{}".format(self.filename, e))
return None
def get_links(self):
result = dict()
preset = self.check()
if preset is None:
return result
item = preset.find('param[@name="Texture/Pattern/PatternFileName"]')
if item is not None:
result['PatternFileName'] = item.text
item = preset.find('param[@name="requiredBrushFile"]')
if item is not None:
result['requiredBrushFile'] = item.text
return result
else:
class KPP(object):
def __init__(self, filename):
pass
def check(self):
return dict()