Last active
May 23, 2019 07:17
-
-
Save lingarajsankaravelu/ec79c6f6b5dd5bab63e7b94711746772 to your computer and use it in GitHub Desktop.
Fixes vector path problems in Android Drawables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From Original Post https://medium.com/@rachitmishra/invalid-vector-drawable-fixing-1-to-0-1-1b430d7469e7 | |
from xml.dom.minidom import parse, parseString | |
from sys import argv | |
import os | |
def fix_path(pathdata, token): | |
# Fix missing 0 after space | |
points = pathdata.split(token) | |
for i, b in enumerate(points): | |
if b.startswith('.'): | |
points[i] = '0' + b | |
return token.join(points) | |
def fix_file(filename): | |
dom1 = parse(filename) | |
paths = dom1.getElementsByTagName('path') | |
for a in paths: | |
for s in [" ", "-", "c", "m", "l", "h", "s", "a", "v", "M", "L", "C"]: | |
pathdata = a.attributes['android:pathData'].value | |
a.setAttribute('android:pathData', fix_path(pathdata, s)) | |
return dom1.toxml() | |
def update_file(filename, content): | |
with open(filename, "w") as f: | |
f.write(content) | |
def fix_folder(path): | |
for entry in os.scandir(path): | |
if entry.is_dir(): | |
if(entry.name == "build"): | |
continue | |
fix_folder(entry.path) | |
if entry.is_file(): | |
if (entry.name.startswith('vc_')): | |
print("Fixing file " + entry.path) | |
newcontent = fix_file(entry.path) | |
print("Updating file ") | |
update_file(entry.path, newcontent) | |
def validate_arg(args): | |
if (len(argv) == 1): | |
print(""" | |
------------------------------ | |
Usage: | |
-a : Fix folder | |
-f : Fix file | |
------------------------------ | |
""") | |
return False | |
if (len(argv) > 3): | |
print(""" | |
------------------------------ | |
Error: | |
Only one of following is allowed | |
-a : Fix folder | |
-f : Fix file | |
------------------------------ | |
""") | |
return False | |
return True | |
def process_arg(args): | |
if (argv[1] == '-a'): | |
print("Fixing folder " + argv[2]) | |
fix_folder(argv[2]) | |
if (argv[1] == '-f'): | |
print("Fixing file " + argv[2]) | |
newcontent = fix_file(argv[2]) | |
print("Updating file ") | |
update_file(argv[2], newcontent) | |
if __name__ == '__main__': | |
if validate_arg(argv): | |
process_arg(argv) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment