forked from navinpeiris/jsca2js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
titanium-mobile.py
executable file
·69 lines (51 loc) · 1.85 KB
/
titanium-mobile.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
#!/usr/bin/env python
__author__ = "Navin Peiris"
__copyright__ = "Copyright 2011, Navin Peiris. All rights reserved."
__email__ = "[email protected]"
__status__ = "Development"
import re
import sys
import urllib2
import json
from jsca2js import convertJsca2Js
DEFAULT_HTTP_TIMEOUT_SECS = 10
TITANIUM_VERSION_REGEX = re.compile('\d\.\d\.\d')
def retrieveJsca(version):
url = 'http://developer.appcelerator.com/apidoc/mobile/' + version + '/api.json'
try:
response = urllib2.urlopen(url, timeout=DEFAULT_HTTP_TIMEOUT_SECS)
content = response.read()
return json.JSONDecoder().decode(content)
except urllib2.HTTPError as e:
raise Exception('Unable to retrieve API for Titanium version ' + version)
def writeJsFile(content, filepath):
try:
file = open(filepath, 'w')
file.write(content)
file.close()
except IOError:
raise Exception('Unable to write JavaScript to file: ' + TITANIUM_JS_FILE_PATH)
def errorExit(message=None):
if message:
sys.stderr.write(message)
sys.exit(1)
def main():
if len(sys.argv) != 2:
errorExit('USAGE: ' + sys.argv[0] + ' <titanium-version>')
version = sys.argv[1]
if not TITANIUM_VERSION_REGEX.match(version):
errorExit('ERROR: Invalid titanium version specified. Must be in the format X.X.X')
print('Retrieving Titanium JSCA API for version: ' + version)
jsca = retrieveJsca(version)
print('Converting API to JavaScript')
javascript = convertJsca2Js(jsca)
outputFilePath = 'titanium-mobile-' + version + '.js'
print('Writing JavaScript to file: ' + outputFilePath)
writeJsFile(javascript, outputFilePath)
if __name__ == '__main__':
try:
main()
print 'Conversion completed successfully'
except Exception as e:
print('ERROR: ' + e.message)
sys.exit(1)