-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-manipulator.py
81 lines (63 loc) · 2.19 KB
/
svg-manipulator.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
import re
import subprocess
from pathlib import Path
svg_files = Path.cwd() / 'svg'
file_content = ''
def push():
"""
Used to push to repo by executing these commands in the terminal:
1. Run `git add .` - Stage all files
2. Run `git commit -m <commit_msg>` - Commit with the provided message
3. Run `git push` - Push to repo
"""
def run(*args):
return subprocess.check_call(['git'] + list(args))
commit_msg = 'Change SVG properties'
run('add', '.')
run('commit', '-m', commit_msg)
run('push')
def change_svg_properties(svg_properties):
"""
Used to change properties value of each svg
"""
# Iterate each file in the directory
for svg in svg_files.iterdir():
# Open a file and retrieve its content
with open(svg, 'r') as file:
file_content = file.read()
file.close()
# Change width
current_width = (re.findall('width=".*"', file_content)[0].split(' ')[0])
file_content = file_content.replace(current_width, f"width=\"{svg_properties['width']}\"")
# Change height
current_height = (re.findall('height=".*"', file_content)[0].split(' ')[0])
file_content = file_content.replace(current_height, f"height=\"{svg_properties['height']}\"")
# Change fill color
current_height = (re.findall('fill=".*"', file_content)[0].split(' ')[0])
file_content = file_content.replace(current_height, f"fill=\"{svg_properties['fill']}\"")
# Write the new content to the file
with open(svg, 'w') as file:
file.write(file_content)
file.close()
def prompt():
"""
Used to prompt user for new properties values
"""
svg_properties = {
'fill': '',
'width': '',
'height': ''
}
new_width = input('New width: ')
new_height = input('New height: ')
new_color = input('New fill color: ')
svg_properties['fill'] = new_color
svg_properties['width'] = new_width
svg_properties['height'] = new_height
return svg_properties
def main():
new_values = prompt()
change_svg_properties(new_values)
push()
# run the program
main()