-
Notifications
You must be signed in to change notification settings - Fork 2
/
svg2swift.py
66 lines (42 loc) · 1.81 KB
/
svg2swift.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
"""
Main file
"""
import re
import argparse
from pathlib import Path
from source.ui_bezier_path_generator import UIBezierPathGenerator
def parse_command_arguments():
"""Returns parsed command arguments"""
parser = argparse.ArgumentParser(description="svg-to-swift converter")
parser.add_argument("--input_file", required=True, help="SVG file to convert.")
parser.add_argument("--output_file", default="svg.swift", help="File to save in swift code.")
return parser.parse_args()
def check_input_file(input_file_path: str):
"""Raises ValueException if file does not exist or has incorrect extension"""
path2file = Path(input_file_path)
if not path2file.is_file():
raise ValueError("File {input} not found!".format(input=input_file_path))
if not input_file_path.endswith(".svg"):
raise ValueError("File should have .svg extension")
def get_svg_file_d_attribute(path2file: str):
"""Returns d attribute content"""
def find_d_attribute(text):
return re.findall(r'<path d=\"(.*?)\"', text)
with open(path2file, "r") as input_file:
content = "".join(input_file.readlines())
d_contents = find_d_attribute(content)
if len(d_contents) != 1:
raise ValueError("For now it should be one d attribute in svg.")
return d_contents
if __name__ == "__main__":
GENERATOR = UIBezierPathGenerator()
# Extracting script arguments
ARGUMENTS = parse_command_arguments()
INPUT_FILE = ARGUMENTS.input_file
OUTPUT_FILE = ARGUMENTS.output_file
check_input_file(INPUT_FILE)
RESULT = GENERATOR.generate(get_svg_file_d_attribute(INPUT_FILE))
with open(OUTPUT_FILE, "w") as file:
for line in RESULT:
file.write(line+"\n")
print("Done. Result stored in {out}".format(out=OUTPUT_FILE))