-
Notifications
You must be signed in to change notification settings - Fork 25
/
titanium-mobile.py
executable file
·129 lines (99 loc) · 3.87 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/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 urllib
import json
import os.path
from jsca2js import convertJsca2Js
DEFAULT_HTTP_TIMEOUT_SECS = 10
TITANIUM_VERSION_REGEX = re.compile('\d\.\d\.\d')
starts_with = ('4.','3.','2.')
def retrieveJsca(version, module='titanium'):
if version.startswith(starts_with):
url = 'http://docs.appcelerator.com/titanium/data/' + version + '/'
else:
url = 'http://developer.appcelerator.com/apidoc/mobile/' + version + '/'
url += 'api.json' if module == 'titanium' else module + '_api.json'
cache = 'titanium-js/api-' + module.lower() + '-' + version + '.json'
try:
if not os.path.isfile(cache):
def reporthook(blocknum, blocksize, totalsize):
readsofar = blocknum * blocksize
if totalsize > 0:
percent = readsofar * 1e2 / totalsize
s = "\r%5.1f%% %*d / %d" % (
percent, len(str(totalsize)), readsofar, totalsize)
sys.stderr.write(s)
if readsofar >= totalsize: # near the end
sys.stderr.write("\n")
else: # total size is unknown
sys.stderr.write("read %d\n" % (readsofar,))
urllib.urlretrieve(url, cache, reporthook)
file = open(cache, 'r')
content = file.read()
file.close()
return json.JSONDecoder().decode(content)
except Exception as e:
if module == 'titanium':
errStr = 'Unable to retrieve API for Titanium version ' + version
else:
errStr = module + ' was not found'
raise Exception(errStr)
def writeJsFile(content, filepath):
try:
file = open(filepath, 'w')
file.write(content.encode('utf8'))
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 + '\n')
sys.exit(1)
#
# Function tries to find Alloy framework files with the same version which
# Titanium have. If it finds - download it and convert it too
#
def tryFindAlloy(version):
print 'Searching for Alloy Framework corresponding to Titanium version ' + version
try:
jsca = retrieveJsca(version,'alloy')
except Exception, e:
errorExit('Not found for this version')
print('Converting API to JavaScript')
javascript = convertJsca2Js(jsca, version)
outputFilePath = 'titanium-js/titanium-mobile-alloy-' + version + '.js'
print('Writing JavaScript to file: ' + outputFilePath)
writeJsFile(javascript, outputFilePath)
def main():
if len(sys.argv) < 2 or len(sys.argv) > 3:
errorExit('USAGE: ' + sys.argv[0] + ' <titanium-version> (optional)<alloy-titanium-version>')
version = sys.argv[1]
if len(sys.argv) > 2:
alloy_version = sys.argv[2]
else:
alloy_version = version
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)
try:
jsca = retrieveJsca(version)
except Exception, e:
errorExit(str(e))
print('Converting API to JavaScript')
javascript = convertJsca2Js(jsca, version)
outputFilePath = 'titanium-js/titanium-mobile-' + version + '.js'
print('Writing JavaScript to file: ' + outputFilePath)
writeJsFile(javascript, outputFilePath)
tryFindAlloy(alloy_version)
if __name__ == '__main__':
#try:
main()
print 'Conversion completed successfully'
#except Exception as e:
# print('ERROR: ' + e.message)
# sys.exit(1)