-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiconfilter.py
135 lines (111 loc) · 4.06 KB
/
iconfilter.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
# -*- coding: utf-8 -*-
import fnmatch
import os
"""
Icon Library Interface
"""
class IconFilter(object):
'''
Abstract base class for Icon Filters
'''
def __init__(self, id, prefix, types, default_icon_dir, default_output_dir):
super(IconFilter, self).__init__()
self._id = id
self._prefix = prefix
self._types = types
self._default_output_dir = default_output_dir
self._default_icon_dir = default_icon_dir
self._adjust_map = None
@property
def id(self):
return self._id
@property
def prefix(self):
return self._prefix
"""
Return list of supported filter types
"""
@property
def types(self):
return self._types
@property
def default_icon_dir(self):
return self._default_icon_dir
@property
def default_output_dir(self):
return self._default_output_dir
"""
Filter callback
return
None if not matched
tuple(file_name, icon_name)
"""
def icon_name(self, filter_type, file_name):
raise NotImplementedError()
@property
def adjusted_file_names(self):
if not self._adjust_map:
self._adjust_map = dict()
for img in self._raw_adjust_map:
(file_name, icon_name) = img.split(":")
self._adjust_map[file_name] = icon_name
return self._adjust_map
def adjust_icon_name(self, file_name, icon_name):
if not icon_name:
return None
# adjust icon name
if file_name in self.adjusted_file_names:
old_icon_name = icon_name
icon_name = self.adjusted_file_names[file_name]
print "%s: adjust icon name '%s' to '%s'" % (file_name, old_icon_name, icon_name)
return icon_name
"""
Icon Generic Filter
"""
class GenericIconFilter(IconFilter):
def __init__(self, adjust_map=None):
super(GenericIconFilter, self).__init__(
id="GenericFilter",
prefix="gn",
types=["all"],
default_icon_dir="generic_input",
default_output_dir="generic_output")
def icon_name(self, filter_type, file_name):
(root, unused) = os.path.splitext(file_name)
return root
"""
Icon Glyph Icons v1.7pro Filter
http://glyphicons.com/
"""
class GlyphIconFilter(IconFilter):
def __init__(self, adjust_map=None):
super(GlyphIconFilter, self).__init__(
id="GlyphIconFilter",
prefix="gi",
types=["normal", "large"],
default_icon_dir="glyphicons_pro/glyphicons/png",
default_output_dir="glyphicons_output")
if adjust_map is None:
self._raw_adjust_map = [
"glyphicons_073_signal.png:signal-radar", "glyphicons_079_signal.png:signal-network",
"glyphicons_091_adjust.png:adjust-contrast", "glyphicons_119_adjust.png:adjust-eq",
"glyphicons_222_share.png:share-arrow", "glyphicons_326_share.png:share-point",
"[email protected]:signal-radar", "[email protected]:signal-network",
"[email protected]:adjust-contrast", "[email protected]:adjust-eq",
"[email protected]:share-arrow", "[email protected]:share-point",
]
else:
self._raw_adjust_map = adjust_map
def icon_name(self, filter_type, file_name):
if filter_type not in self.types or not fnmatch.fnmatch(file_name, "*.png"):
return None
# validate file name for icon_type
(root, unused) = os.path.splitext(file_name)
parts = "-".join(root.split("_")[2:])
is_large = root.endswith("@2x")
if filter_type == 'large' and is_large:
return self.adjust_icon_name(file_name, parts[:-3]) # strip @2x from
elif filter_type == 'normal' and not is_large:
return self.adjust_icon_name(file_name, parts)
return None