-
Notifications
You must be signed in to change notification settings - Fork 4
/
ios2android.py
78 lines (61 loc) · 2.17 KB
/
ios2android.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
#! /usr/bin/env python
# coding=utf-8
import unicodedata
import os
from os import listdir
from os.path import isfile, join
from shutil import copyfile
import sys
__author__ = 'matheusjardimb'
# ios2android.py path comes into sys.argv[0]
if len(sys.argv) != 2:
print('usage: ios2android.py [path | file.zip]')
exit(-1)
retina_sufix_2x = '@2x'
retina_sufix_1x = '@1x'
encoding = "utf-8"
allowed_extensions = ['.png']
res = 'res'
res_ldpi = 'res\\drawable-ldpi'
res_mdpi = 'res\\drawable-mdpi'
res_hdpi = 'res\\drawable-hdpi'
res_xhdpi = 'res\\drawable-xhdpi'
res_xxhdpi = 'res\\drawable-xxhdpi'
res_dirs = [res, res_ldpi, res_mdpi, res_hdpi, res_xhdpi, res_xxhdpi]
def remove_accents(input_str):
nkfd_form = unicodedata.normalize('NFKD', input_str)
return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])
def sanitize_filename(name):
dot = name.rfind('.')
extension = name[dot:]
name = name[:dot]
new_filename = name.replace(retina_sufix_1x, '')
new_filename = name.replace('(', '').replace(')', '').replace('.', '')
new_filename = new_filename.replace(retina_sufix_2x, '')
new_filename = new_filename.replace('-', '_')
new_filename = new_filename.replace(' ', '_')
new_filename = new_filename.replace(" ", '_')
new_filename = new_filename.lower()
new_filename = remove_accents(new_filename)
return new_filename + extension
def create_dir(dir_path):
if not os.path.exists(dir_path):
os.makedirs(dir_path)
path_of_images = sys.argv[1]
if path_of_images.endswith('.zip'):
import zipfile
unzip_dir = 'unzipped'
with zipfile.ZipFile(path_of_images, "r") as z:
z.extractall(unzip_dir)
path_of_images = unzip_dir
for directory in res_dirs:
create_dir(directory)
for filename in listdir(path_of_images):
if isfile(join(path_of_images, filename)) and [ext for ext in allowed_extensions if ext in filename]:
path = res_ldpi
if retina_sufix_2x in filename:
path = res_hdpi
new_name = sanitize_filename(filename)
dst_path = os.path.join(path, new_name)
src_path = os.path.join(path_of_images, filename)
copyfile(src_path, dst_path)