3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
21 """An interactive python shell that uses remote_api.
24 %prog [-s HOSTNAME] [-p PATH] [APPID]
26 If the -s HOSTNAME flag is not specified, the APPID must be specified.
32 from google
.appengine
.tools
import os_compat
46 from google
.appengine
.ext
.remote_api
import remote_api_stub
47 from google
.appengine
.tools
import appengine_rpc
51 from google
.appengine
.api
import memcache
52 from google
.appengine
.api
import urlfetch
53 from google
.appengine
.api
import users
54 from google
.appengine
.ext
import db
55 from google
.appengine
.ext
import ndb
58 HISTORY_PATH
= os
.path
.expanduser('~/.remote_api_shell_history')
59 DEFAULT_PATH
= '/_ah/remote_api'
60 BANNER
= """App Engine remote_api shell
62 The db, ndb, users, urlfetch, and memcache modules are imported.\
67 return (raw_input('Email: '), getpass
.getpass('Password: '))
70 def remote_api_shell(servername
, appid
, path
, secure
, rpc_server_factory
):
71 """Actually run the remote_api_shell."""
74 remote_api_stub
.ConfigureRemoteApi(appid
, path
, auth_func
,
75 servername
=servername
,
76 save_cookies
=True, secure
=secure
,
77 rpc_server_factory
=rpc_server_factory
)
78 remote_api_stub
.MaybeInvokeAuthentication()
81 os
.environ
['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'
85 appid
= os
.environ
['APPLICATION_ID']
86 sys
.ps1
= '%s> ' % appid
87 if readline
is not None:
89 readline
.parse_and_bind('tab: complete')
90 atexit
.register(lambda: readline
.write_history_file(HISTORY_PATH
))
91 if os
.path
.exists(HISTORY_PATH
):
92 readline
.read_history_file(HISTORY_PATH
)
95 if '' not in sys
.path
:
96 sys
.path
.insert(0, '')
98 preimported_locals
= {
100 'urlfetch': urlfetch
,
107 code
.interact(banner
=BANNER
, local
=preimported_locals
)
111 """Parse arguments and run shell."""
112 parser
= optparse
.OptionParser(usage
=__doc__
)
113 parser
.add_option('-s', '--server', dest
='server',
114 help='The hostname your app is deployed on. '
115 'Defaults to <app_id>.appspot.com.')
116 parser
.add_option('-p', '--path', dest
='path',
117 help='The path on the server to the remote_api handler. '
118 'Defaults to %s.' % DEFAULT_PATH
)
119 parser
.add_option('--secure', dest
='secure', action
="store_true",
120 default
=False, help='Use HTTPS when communicating '
122 (options
, args
) = parser
.parse_args()
125 if ((not options
.server
and not args
) or len(args
) > 2
126 or (options
.path
and len(args
) > 1)):
127 parser
.print_usage(sys
.stderr
)
129 print >> sys
.stderr
, 'Unexpected arguments: %s' % args
[2:]
130 elif options
.path
and len(args
) > 1:
131 print >> sys
.stderr
, 'Path specified twice.'
135 servername
= options
.server
137 path
= options
.path
or DEFAULT_PATH
144 servername
= '%s.appspot.com' % args
[0]
148 remote_api_shell(servername
, appid
, path
, options
.secure
,
149 appengine_rpc
.HttpRpcServer
)
152 if __name__
== '__main__':