ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • px값을 dp값으로 바꾸는 스크립트
    과거...../개발일지 2011. 6. 15. 09:36

     며칠 전 안드로이드 소스 하나를 받았다.
     왠 걸...... layout 에 모든 수치 값이.....px로 작성이 되있었다. 해상도를 바꾸는 작업을 해야되는데....일일이 하나 하나
     엑셀에서 수정하면서 고치고있는데....팀장님이....perl 스크립트로 ..짜서 한방에 폴더안에 있는 layout파일에 px값을 dp로
     바꾸어 주셨다.

     그래서... 살짝......죄송스럽지만...자존심에 ..타격이..ㅜㅜ(내가 프로그래머일까..라는 생각을 하면서..ㅋㅋ)
     그래서 perl 이 아닌 python으로 새로 구현해 보았다.(팀장님이 작성하신 소스에 4배 길이는 되지만......)
     아래 파일을 돌리시면 된다.(경로를 인자값으로 넘겨서) 


    #description : change 'px||pt' to 'dp'
    import os
    import re
    
    def search(dirname):
    	flist = os.listdir(dirname)
    	for f in flist:
    		next = os.path.join(dirname, f)
    		if os.path.isdir(next):
    			search(next)
    		else:
    			doFileWork(next)
    			
    def doFileWork(filename):
    	ext = os.path.splitext(filename)[-1]
    	if ext != '.xml': return
    	f = open(filename,"r")
    	before = f.read()
    	f.close
    	
    	preStr = "\\d+px"
    	preStr2 = "\\d+pt"
    	
    	tmpList = re.findall(preStr,before)
    	tmpList2 = re.findall(preStr2,before)
    		
    	match = tmpList + tmpList2
    	
    	aftermatch = match[:]
    	
    	for m in range(len(aftermatch)):
    		aftermatch[m] = str(int(aftermatch[m][:-2])*2/3) + "dp"
    		
    	for m in range(len(aftermatch)):
    		before= before.replace(match[m],aftermatch[m])
    		#print match[m]
    		#print aftermatch[m]	
    		
    	f = open(filename, "w")
    	f.write(before)
    	f.close()		
    
    #ex:	
    search("C:/temp/temp")
    

Designed by Tistory.