3 # Copyright 2009 the Melange authors.
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 """This module supplies an interactive shell with remote api configured.
21 App Engine interactive console
22 >>> from soc.models.user import User
23 >>> gen = lambda: User.all()
24 >>> it = deepFetch(gen)
25 >>> result = [i for i in it]
29 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
42 """Returns a tuple with username and password.
45 return raw_input('Username:'), getpass
.getpass('Password:')
48 def deepFetch(queryGen
,key
=None,batchSize
= 100):
49 """Iterator that yields an entity in batches.
52 queryGen: should return a Query object
53 key: used to .filter() for __key__
54 batchSize: how many entities to retrieve in one datastore call
56 Retrieved from http://tinyurl.com/d887ll (AppEngine cookbook).
59 # AppEngine will not fetch more than 1000 results
60 batchSize
= min(batchSize
,1000)
72 query
.filter("__key__ > ",key
)
73 results
= query
.fetch(batchSize
)
74 for result
in results
:
77 if batchSize
> len(results
):
80 key
= results
[-1].key()
84 """Starts a shell with the datastore as remote_api_stub.
87 from google
.appengine
.ext
import db
88 from google
.appengine
.ext
.remote_api
import remote_api_stub
95 host
= '%s.appspot.com' % app_id
97 remote_api_stub
.ConfigureRemoteDatastore(app_id
, '/remote_api', auth_func
, host
)
100 'deepFetch': deepFetch
,
103 code
.interact('App Engine interactive console for %s' % (app_id
,), None, context
)
107 """Sets up the sys.path and environment for development.
110 here
= os
.path
.abspath(__file__
)
111 here
= os
.path
.join(os
.path
.dirname(here
), '..')
112 here
= os
.path
.normpath(here
)
114 appengine_location
= os
.path
.join(here
, 'thirdparty', 'google_appengine')
117 os
.path
.join(appengine_location
, 'lib', 'django'),
118 os
.path
.join(appengine_location
, 'lib', 'webob'),
119 os
.path
.join(appengine_location
, 'lib', 'yaml', 'lib'),
121 os
.path
.join(here
, 'app'),
124 sys
.path
= extra_paths
+ sys
.path
126 os
.environ
['SERVER_SOFTWARE'] = 'Development Interactive Shell'
128 import main
as app_main
133 if __name__
== '__main__':
134 if len(sys
.argv
) < 2:
135 print "Usage: %s app_id [host]" % (sys
.argv
[0],)