Last active
December 18, 2019 11:25
-
-
Save redgoose-dev/6953f8057e557cbb6f0e1a85ab1a3c17 to your computer and use it in GitHub Desktop.
Small projects on python
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
import os | |
import sys | |
import json | |
''' | |
Convert icon svg to vue | |
svg 파일을 vue 파일로 바꿔주는 스크립트입니다. | |
변환작업이 좀 많은편이라서 파이썬으로 스크립트를 작성하게 되었습니다. | |
먼저 `./src` 경로속에 `svg`파일들을 집어넣습니다. | |
그리고 다음과 같이 스크립트를 실행하면 `svg` 파일들은 `vue`파일로 변환되고, `./src/index.json`파일이 만들어집니다. | |
usage: `python3 convert-svg-to-vue.py` | |
''' | |
# change filename | |
def changeFilename(path, name, rename): | |
os.rename(path+name, path+rename) | |
# edit file | |
def editFile(path, words): | |
# open file | |
f = open(path, 'r') | |
bodyText = f.read() | |
combineText = words[0] + bodyText + words[1] | |
f.close() | |
# write file | |
f = open(path, 'w') | |
f.write(combineText) | |
f.close() | |
def makeFileNameArrayFile(path, filename, data): | |
f = open(path+filename, 'w+') | |
data.sort() | |
data = json.dumps(data) | |
f.write(data) | |
f.close() | |
# set path | |
path = './src/' | |
# icons index filename | |
indexFileName = 'index.json' | |
# set extension | |
changeExt = '.vue' | |
# set edit text | |
combineTexts = [ | |
'<template>\n', | |
'\n</template>', | |
] | |
filesName = [] | |
# get files | |
files = os.listdir(path) | |
# processing files | |
for file in files: | |
if not os.path.isdir(file): | |
filename = os.path.splitext(file) | |
if filename[1] == '.svg': | |
filesName.append(filename[0]) | |
editFile(path+filename[0]+filename[1], combineTexts) | |
changeFilename(path, filename[0]+filename[1], filename[0]+changeExt) | |
print('change file::', path+filename[0]+changeExt) | |
# make array file | |
if len(filesName) > 0: | |
makeFileNameArrayFile(path, indexFileName, filesName) | |
# print last message | |
print('COMPLETE CONVERT ICON FILES') |
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
import os | |
import json | |
from xml.etree import ElementTree as ET | |
''' | |
Edit svg files | |
svg 파일들을 수정하는 스크립트 | |
아이콘들을 일괄적으로 속성을 편집하기 위하여 만들어 졌습니다. | |
''' | |
ET.register_namespace('', 'http://www.w3.org/2000/svg') | |
''' | |
set variables | |
''' | |
# 편집할 svg 파일들의 경로 | |
path = '../static/icons/' | |
# svg 파일들의 이름목록을 만들 json 파일 경로 | |
path_fileNames = '../components/icon/svg-index.json' | |
''' | |
functions | |
''' | |
# processing svg file | |
def processing(path, id): | |
tree = ET.parse(path) | |
root = tree.getroot() | |
# set id | |
root.set('id', id) | |
# remove attributes | |
if 'width' in root.attrib: del root.attrib['width'] | |
if 'height' in root.attrib: del root.attrib['height'] | |
if 'class' in root.attrib: del root.attrib['class'] | |
if 'stroke-width' in root.attrib: del root.attrib['stroke-width'] | |
# write file | |
tree.write(path) | |
def makeFileNameArrayFile(path, data): | |
f = open(path, 'w+') | |
data = json.dumps(data) | |
f.write(data) | |
f.close() | |
''' | |
actions | |
''' | |
# get files | |
files = os.listdir(path) | |
files.sort() | |
filesIndex = [] | |
# processing files | |
for (idx,file) in enumerate(files): | |
if not os.path.isdir(file): | |
filename = os.path.splitext(file) | |
if filename[1] == '.svg': | |
filesIndex.append(filename[0]) | |
processing(path + file, 'icon_' + filename[0]) | |
print(idx, path + file) | |
# make array file | |
if len(filesIndex) > 0: | |
makeFileNameArrayFile(path_fileNames, filesIndex) | |
# print last message | |
print('COMPLETE PROCESSING SVG FILES') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment