Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrapper around Closure Compiler API
import httplib
import sys
import urllib
from contextlib import closing
################################################################################################################
# Compilation level
WHITESPACE_ONLY_CL = 'WHITESPACE_ONLY'
SIMPLE_OPTIMIZATIONS_CL = 'SIMPLE_OPTIMIZATIONS'
ADVANCED_OPTIMIZATIONS_CL = 'ADVANCED_OPTIMIZATIONS'
# Output format
TEXT_OF = 'text'
XML_OF = 'xml'
JSON_OF = 'json'
# Output information
COMPILED_CODE_OI = 'compiled_code'
WARNINGS_OI = 'warnings'
ERRORS_OI = 'errors'
STATISTICS_OI = 'statistics'
# Warning level
QUIET_WL = 'QUIET'
DEFAULT_WL = 'DEFAULT_WL'
VERBOSE_WL = 'VERBOSE_WL'
# Language
ECMASCRIPT3_LANG = 'ECMASCRIPT3'
ECMASCRIPT5_LANG = 'ECMASCRIPT5'
ECMASCRIPT5_STRICT_LANG = 'ECMASCRIPT5_STRICT'
################################################################################################################
class ClosureCompiler:
def call_closure_api(self, js_urls, pretty_print=False, print_input_delimiter=False, **kwargs):
# There are some arguments which can not be put into the kwargs dict, since the keys are repeated:
# - code_url
# - formatting
# So I must build an args list from kwargs so that I can add the extra POST parameters before calling urlendcode
args = list(kwargs.items())
if js_urls :
for js_url in js_urls :
args.append(('code_url', js_url))
if pretty_print : args.append(('formatting', 'pretty_print'))
if print_input_delimiter : args.append(('formatting', 'print_input_delimiter'))
with closing(httplib.HTTPConnection('closure-compiler.appspot.com')) as conn:
conn.request(
'POST', '/compile',
urllib.urlencode(args),
headers={"Content-type": "application/x-www-form-urlencoded"}
)
return conn.getresponse().read()
def get_js(self, js_urls=None, js_code=None, output_file_name=None, pretty_print=False, print_input_delimiter=False, use_closure_library=True,
output_format=TEXT_OF, compilation_level=WHITESPACE_ONLY_CL, warning_level=DEFAULT_WL, output_info=COMPILED_CODE_OI,
language=ECMASCRIPT3_LANG):
kwargs = dict(
compilation_level=compilation_level,
output_format=output_format,
output_info=output_info,
use_closure_library=use_closure_library,
language=language,
)
if js_code : kwargs['js_code'] = js_code
if output_file_name : kwargs['output_file_name'] = output_file_name
return self.call_closure_api(js_urls, pretty_print, print_input_delimiter, **kwargs)
################################################################################################################
closcomp = ClosureCompiler()
output_file_name = 'libphonenumber.js'
output_file_name = None
js_urls = [
'http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/phonemetadata.pb.js',
'http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/phonenumber.pb.js',
'http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/metadata.js',
'http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/phonenumberutil.js',
'http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/asyoutypeformatter.js',
]
#js_urls = None
#js_code = 'alert("hi");'
js_code = None
print closcomp.get_js(js_urls, js_code, output_file_name, compilation_level=ADVANCED_OPTIMIZATIONS_CL)
################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment