Skip to content

Commit 321fbaf

Browse files
author
hartsantler
committed
integrated export to mobile using phonegap, automatically previews in the android emulator.
1 parent faf1644 commit 321fbaf

3 files changed

Lines changed: 102 additions & 44 deletions

File tree

pypubjs/app.py

Lines changed: 99 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -98,71 +98,126 @@ def add_js_import( url ):
9898

9999
#################### app compiler #####################
100100
win = None
101-
def compile_app():
101+
def preview_app():
102102
global win
103+
if win is None:
104+
win = nw_gui.Window.open(
105+
'',
106+
width=480,
107+
height=350,
108+
toolbar=False,
109+
frame=True
110+
)
111+
win.on(
112+
'loaded',
113+
lambda : compile_app( preview=True, css_imports=CssImports, js_imports=JsImports )
114+
)
115+
116+
else:
117+
#win.window.document.documentElement.innerHTML="" ## broken?
118+
win.window.document.body.innerHTML=""
119+
compile_app( preview=True, css_imports=CssImports, js_imports=JsImports )
120+
121+
def export_phonegap():
122+
project_name = 'testing'
123+
tmpdir = tempfile.gettempdir()
124+
args = ['create', project_name, '--name', project_name, '--id', 'com.example.hello']
125+
126+
def callback1( stdout ):
127+
print('-------phonegap project created---------')
128+
print(stdout)
129+
130+
rootdir = os.path.join( tmpdir, project_name )
131+
wwwdir = os.path.join( rootdir, 'www')
132+
jsdir = os.path.join( wwwdir, 'js' )
133+
134+
js_imports = ['phonegap.js']
135+
for path in JsImports:
136+
filename = os.path.split(path)[-1]
137+
data = open( os.path.join('pypubjs', path), 'r' ).read()
138+
open( os.path.join(jsdir, filename), 'w').write( data )
139+
js_imports.append( 'js/'+filename )
140+
141+
def callback2( html ):
142+
print('-----------saving phonegap html------------')
143+
open( os.path.join(wwwdir, 'index.html'), 'w').write( html )
144+
145+
def callback3( stdout ):
146+
print('-----------phonegap project built------------')
147+
print(stdout)
148+
#apk = open( 'platforms/android/bin/%s-debug.apk' %project_name, 'rb' ).read()
149+
subprocess.call( 'phonegap', ['install', 'android', '--emulator'], cwd=rootdir )
150+
151+
subprocess.call(
152+
'phonegap',
153+
['local', 'build', 'android'],
154+
cwd=rootdir,
155+
stdout=subprocess.PIPE,
156+
callback=callback3
157+
)
158+
159+
compile_app( preview=False, js_imports=js_imports, callback=callback2 )
160+
161+
subprocess.call( 'phonegap', args, cwd=tmpdir, stdout=subprocess.PIPE, callback=callback1 )
162+
163+
164+
103165

104-
def on_loaded():
166+
def compile_app( preview=False, css_imports=None, js_imports=None, callback=None ):
167+
168+
if preview:
105169
dev = win.showDevTools()
106170
dev.resizeTo( win.width, 130)
107171
dev.moveTo( win.x, win.y + win.height + 20 )
108172

109-
out = ['<html><head>']
173+
out = ['<html><head>']
110174

111-
for url in CssImports:
175+
if css_imports:
176+
for url in css_imports:
112177
out.append('<link rel="stylesheet" href="%s"/>' %url)
113178

114-
out.append('<style type="text/css">')
115-
out.append( css_editor.getValue() )
116-
out.append('</style>')
179+
out.append('<style type="text/css">')
180+
out.append( css_editor.getValue() )
181+
out.append('</style>')
117182

118-
for url in JsImports:
183+
if js_imports:
184+
for url in js_imports:
119185
out.append( '<script type="text/javascript" src="%s"></script>'%url )
120-
186+
187+
out.append('<script type="text/javascript">')
188+
out.append( js_head_editor.getValue() )
189+
out.append('</script>')
190+
191+
def _callback1(js):
121192
out.append('<script type="text/javascript">')
122-
out.append( js_head_editor.getValue() )
193+
out.append( js )
123194
out.append('</script>')
195+
out.append('</head>')
124196

125-
def callback1(js):
126-
out.append('<script type="text/javascript">')
127-
out.append( js )
128-
out.append('</script>')
129-
out.append('</head>')
197+
out.append('<body>')
198+
out.append( html_editor.getValue() )
130199

131-
out.append('<body>')
132-
out.append( html_editor.getValue() )
200+
## post init scripts ##
201+
out.append('<script type="text/javascript">')
202+
out.append( js_body_editor.getValue() )
203+
out.append('</script>')
133204

134-
## post init scripts ##
205+
def _callback2(js):
135206
out.append('<script type="text/javascript">')
136-
out.append( js_body_editor.getValue() )
207+
out.append( js )
137208
out.append('</script>')
138-
139-
def callback2(js):
140-
out.append('<script type="text/javascript">')
141-
out.append( js )
142-
out.append('</script>')
143-
out.append('</body></html>')
144-
data = '\n'.join(out)
145-
print(data)
209+
out.append('</body></html>')
210+
data = '\n'.join(out)
211+
if preview:
146212
win.window.document.write( data )
147213

148-
translate( {'data':py_body_editor.getValue(),'callback': callback2} )
214+
if callback:
215+
callback( data )
149216

150-
translate( {'data':py_head_editor.getValue(),'callback': callback1} )
217+
translate( {'data':py_body_editor.getValue(),'callback': _callback2} )
151218

152-
if win is None:
153-
win = nw_gui.Window.open(
154-
'',
155-
width=480,
156-
height=350,
157-
toolbar=False,
158-
frame=True
159-
)
160-
win.on('loaded', on_loaded)
219+
translate( {'data':py_head_editor.getValue(),'callback': _callback1} )
161220

162-
else:
163-
#win.window.document.documentElement.innerHTML="" ## broken?
164-
win.window.document.body.innerHTML=""
165-
on_loaded()
166221

167222

168223
def open_editor_window( filename, data ):
@@ -279,3 +334,5 @@ def on_load(event):
279334
Reader.onload = on_load
280335
Reader.readAsText( file )
281336

337+
338+

pypubjs/main.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@
102102
<li><a href="">Linux</a></li>
103103
<li><a href="">OSX</a></li>
104104
<li><a href="">Windows</a></li>
105-
<li><a href="">Mobile</a></li>
105+
<li><a href="javascript:export_phonegap()">Mobile</a></li>
106106
<li class="divider"></li>
107-
<li><a href="javascript:compile_app()">Preview</a></li>
107+
<li><a href="javascript:preview_app()">Preview</a></li>
108108
</ul>
109109
</li>
110110

pypubjs/translator_bridge.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ translate( {file:'nodejs/bindings/io.py'} );
8080
translate( {file:'nodejs/bindings/os.py'} );
8181
translate( {file:'nodejs/bindings/sys.py'} );
8282
translate( {file:'nodejs/bindings/tempfile.py'} );
83+
translate( {file:'nodejs/bindings/subprocess.py'} );
8384

8485
var translate_python_scripts = function() {
8586
var scripts = document.getElementsByTagName('script');

0 commit comments

Comments
 (0)