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.
17 """An interactive python shell that uses remote_api.
20 %prog [-s HOSTNAME] [-p PATH] [--secure] [APPID]
22 If the -s HOSTNAME flag is not specified, the APPID must be specified.
29 from google
.appengine
.tools
import os_compat
43 from google
.appengine
.ext
.remote_api
import remote_api_stub
44 from google
.appengine
.tools
import appengine_rpc
48 from google
.appengine
.api
import memcache
49 from google
.appengine
.api
import urlfetch
50 from google
.appengine
.api
import users
51 from google
.appengine
.ext
import db
52 from google
.appengine
.ext
import ndb
55 HISTORY_PATH
= os
.path
.expanduser('~/.remote_api_shell_history')
56 DEFAULT_PATH
= '/_ah/remote_api'
57 BANNER
= """App Engine remote_api shell
59 The db, ndb, users, urlfetch, and memcache modules are imported.\
64 return (raw_input('Email: '), getpass
.getpass('Password: '))
67 def remote_api_shell(servername
, appid
, path
, secure
, rpc_server_factory
):
68 """Actually run the remote_api_shell."""
71 remote_api_stub
.ConfigureRemoteApi(appid
, path
, auth_func
,
72 servername
=servername
,
73 save_cookies
=True, secure
=secure
,
74 rpc_server_factory
=rpc_server_factory
)
75 remote_api_stub
.MaybeInvokeAuthentication()
78 os
.environ
['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'
82 appid
= os
.environ
['APPLICATION_ID']
83 sys
.ps1
= '%s> ' % appid
84 if readline
is not None:
86 readline
.parse_and_bind('tab: complete')
87 atexit
.register(lambda: readline
.write_history_file(HISTORY_PATH
))
88 if os
.path
.exists(HISTORY_PATH
):
89 readline
.read_history_file(HISTORY_PATH
)
92 if '' not in sys
.path
:
93 sys
.path
.insert(0, '')
95 preimported_locals
= {
104 code
.interact(banner
=BANNER
, local
=preimported_locals
)
108 """Parse arguments and run shell."""
109 parser
= optparse
.OptionParser(usage
=__doc__
)
110 parser
.add_option('-s', '--server', dest
='server',
111 help='The hostname your app is deployed on. '
112 'Defaults to <app_id>.appspot.com.')
113 parser
.add_option('-p', '--path', dest
='path',
114 help='The path on the server to the remote_api handler. '
115 'Defaults to %s.' % DEFAULT_PATH
)
116 parser
.add_option('--secure', dest
='secure', action
="store_true",
117 default
=False, help='Use HTTPS when communicating '
119 (options
, args
) = parser
.parse_args()
122 if ((not options
.server
and not args
) or len(args
) > 2
123 or (options
.path
and len(args
) > 1)):
124 parser
.print_usage(sys
.stderr
)
126 print >> sys
.stderr
, 'Unexpected arguments: %s' % args
[2:]
127 elif options
.path
and len(args
) > 1:
128 print >> sys
.stderr
, 'Path specified twice.'
132 servername
= options
.server
134 path
= options
.path
or DEFAULT_PATH
141 servername
= '%s.appspot.com' % args
[0]
145 remote_api_shell(servername
, appid
, path
, options
.secure
,
146 appengine_rpc
.HttpRpcServer
)
149 if __name__
== '__main__':