Last active
July 4, 2019 07:04
-
-
Save rachitmishra/616546a84c8bd04c6eef9d3c73708492 to your computer and use it in GitHub Desktop.
Python script to fix vector errors for android (.1 to 0.1)
This file contains hidden or 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 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) | |
@Hero9909 well that was the goal helping out someone with a similar problem :)
I will fix the other issue you have mentioned and update the gist.
Thanks for the coffee 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you saved me several hours of work as ive to fix 300+ files.
so ive some improvements to this nice tool:
if your file is empty it'll chrash if your adding an
if os.stat(entry.path).st_size > 0:
at line 34 this would prevent empty filesas this tool is not fixing xml structures as main feature, i would not prevent an chrash if the file contains invalid xml
it might be usefull not to determinate if the file is vaild by an prefix in the name , an option for this would be nice
and at last please provide an paypal link so i could donate you a cup of coffee :)