-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNMaterialIcons.py
executable file
·161 lines (146 loc) · 4.65 KB
/
RNMaterialIcons.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /usr/bin/env python3
import argparse
import io
import urllib.request as request
import zipfile
import os, ssl
"""
Prevent: CERTIFICATE_VERIFY_FAILED
"""
if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
getattr(ssl, '_create_unverified_context', None)):
ssl._create_default_https_context = ssl._create_unverified_context
# Author: Christopher Ventura <[email protected]>
SOURCE = 'https://material.io'
ENDPOINT = '/tools/icons/static/icons'
URL = '{}{}'.format(SOURCE, ENDPOINT)
PLATFORM = 'android'
EXTENSION = 'zip'
ASSET_EXTENSION = 'png'
# Android drawable dimensions from material.io zip
android_sizes = [
'res/drawable-mdpi/',
'res/drawable-hdpi/',
'res/drawable-xhdpi/',
'res/drawable-xxhdpi/',
'res/drawable-xxxhdpi/',
]
# React Native icons sizes
react_sizes = [
'.',
'@1x.',
'@2x.',
'@3x.',
'@4x.',
]
# React Native assets platform
platforms = [
'android',
'ios',
]
default_options = {
'theme': 'round',
'color': 'white',
'size': 24,
'platform': 'none',
}
def build_asset_url(options):
url = '{}/{}-{}'.format(
URL,
options['theme'],
options['asset'],
)
return url
def build_platform_url(options):
url = '{}-{}-{}.{}'.format(
build_asset_url(options),
PLATFORM,
options['color'],
EXTENSION,
)
return url
def build_android_filename(android_name, options):
filename = '{}{}_{}_{}_{}.png'.format(
android_name,
options['theme'],
options['asset'],
options['color'],
options['size'],
)
return filename
def build_platform_dimension(platform, dim):
if platform == 'android' or platform == 'ios':
return '{}{}.'.format(dim, platform)
else:
return dim
def build_react_filename(dim, options):
platform_dim = build_platform_dimension(options["platform"], dim)
filename = '{}{}{}'.format(
options['name'],
platform_dim,
ASSET_EXTENSION
)
return filename
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--asset', help='Asset name.', required=True,
dest='asset')
parser.add_argument('-n', '--name', help='Asset new name.', required=False,
dest='name')
parser.add_argument('-c', '--color', help='Asset color (default: white)',
required=False, dest='color')
parser.add_argument('-t', '--theme', help='Asset theme (default: round)',
required=False, dest='theme')
parser.add_argument('-s', '--size', help='Asset size (default: 24)',
required=False, dest='size')
parser.add_argument('-d', '--dir', help='Directory where asset will be downloaded (default: current)',
required=False, dest='dir')
parser.add_argument('-p', '--platform', help='Assets platform target (default: none)',
required=False, dest='platform')
args = parser.parse_args()
options = default_options
if args.asset:
options['asset'] = args.asset
# If there's no name, use asset name
options['name'] = args.asset
if args.theme:
options['theme'] = args.theme
if args.color:
options['color'] = args.color
if args.size:
options['size'] = args.size
if args.name:
options['name'] = args.name # Override
if args.platform:
options['platform'] = args.platform
file_url = build_platform_url(options)
try:
# Download from material.io
response = request.urlopen(file_url)
data = response.read()
# Read .zip downloaded file
zipped_file = zipfile.ZipFile(io.BytesIO(data))
with zipped_file as folder:
# For every file in .zip
for file in folder.namelist():
# android dimensions => react native size
for size, dim in zip(android_sizes, react_sizes):
filename = build_android_filename(size, options)
# if this file is the same we built (we've found our file)
if filename == file:
# Build react equivalent name
react_name = build_react_filename(dim, options)
with folder.open(file) as android_icon:
android_icon_content = android_icon.read()
# Save as React Native icon
with open(react_name, "wb") as react_icon:
react_icon.write(android_icon_content)
# Everything went OK
print('[*] {} icon downloaded successfully from {}'.format(options['asset'], SOURCE))
except Exception as e:
print("Error: ", e)
print('[!] Sorry, we couldn\'t find \'{}\' {} icon.'.format(
options['asset'], options['theme']))
print('[*] You can visit {} for more icon names'.format(SOURCE))
else:
print('[!] Asset name is required')