2013-10-05 2 views
1

클로저 컴파일러를 배포 프로세스에 통합하려고합니다. 나는 내가 필요한 구성 요소로부터 어떤 자바 스크립트를 생성 할 수있게 해주는 전체 this online tool을 보냈다. 도구에 API을 통해 액세스 할 수 있으므로 배포 스크립트에 통합하고 싶습니다.클로저 컴파일러 서비스 API에 액세스하는 Python 라이브러리

필자는 바퀴를 재발 명하고 싶지 않고 이미이 API에 사용할 수있는 파이썬 래퍼가 있는지 궁금해하고있었습니다. 제공된 examples은 매우 낮은 수준이며 대안을 찾지 못했습니다.

누가 나를 상위 레벨 파이썬 라이브러리에 연결하여 Goggle Closure Compiler Service API에 액세스 할 수 있습니까?

답변

3

developer.google.com의 예제는 실제로 파이썬을 사용하므로 좋은 출발점입니다. 그러나 API가 너무 작아서 공무원의 의사조차도 urllibhttplib 파이썬 내장 모듈을 사용하기로 결정한 것으로 보입니다. 논리를 헬퍼 함수 또는 두 개로 일반화하면 실제로 사소한 작업처럼 보입니다.

... 

params = urllib.urlencode([ 
    ('js_code', sys.argv[1]), 
    ('compilation_level', 'WHITESPACE_ONLY'), 
    ('output_format', 'text'), 
    ('output_info', 'compiled_code'), 
    ]) 

# Always use the following value for the Content-type header. 
headers = {"Content-type": "application/x-www-form-urlencoded"} 
conn = httplib.HTTPConnection('closure-compiler.appspot.com') 
conn.request('POST', '/compile', params, headers) 

... 

https://developers.google.com/closure/compiler/docs/api-tutorial1

P.S.를 참조하십시오 또한 명령 줄 도구 인 https://github.com/danielfm/closure-compiler-cli을 들여다 볼 수도 있지만 소스는 API가 얼마나 간단한지를 보여줍니다.

은 그래서 파이썬 API로 위의 전환 :

import httplib 
import sys 
import urllib 
from contextlib import closing 


def call_closure_api(**kwargs): 
    with closing(httplib.HTTPConnection('closure-compiler.appspot.com')) as conn: 
     conn.request(
      'POST', '/compile', 
      urllib.urlencode(kwargs.items()), 
      headers={"Content-type": "application/x-www-form-urlencoded"} 
     ) 
     return conn.getresponse().read() 


call_closure_api(
    js_code=sys.argv[1], 
    # feel free to introduce named constants for these 
    compilation_level='WHITESPACE_ONLY', 
    output_format='text', 
    output_info='compiled_code' 
) 
+0

좋아요, 감사합니다! 오늘 내가 나중에 할 계획이었던 일을 1 분 안에 끝내 셨습니다. 잘하면 2 분이 걸렸을 것입니다! – dangonfast

+0

@ gonvaled : 편집을 참조하십시오. –

+0

여러분의 코드를 기반으로 [this gist] (https://gist.github.com/gonvaled/6840024)를 만들었습니다. – dangonfast

관련 문제