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>',
41 """Returns a tuple with username and password.
44 return raw_input('Username:'), getpass
.getpass('Password:')
47 def deepFetch(queryGen
,key
=None,batchSize
= 100):
48 """Iterator that yields an entity in batches.
51 queryGen: should return a Query object
52 key: used to .filter() for __key__
53 batchSize: how many entities to retrieve in one datastore call
55 Retrieved from http://tinyurl.com/d887ll (AppEngine cookbook).
58 from google
.appengine
.ext
import db
60 # AppEngine will not fetch more than 1000 results
61 batchSize
= min(batchSize
,1000)
74 query
.filter("__key__ > ",key
)
75 results
= query
.fetch(batchSize
)
76 for result
in results
:
79 if batchSize
> len(results
):
82 key
= results
[-1].key()
85 def remote(args
, context
=None):
86 """Starts a shell with the datastore as remote_api_stub.
89 args: arguments from the user
90 context: locals that should be added to the shell
93 from google
.appengine
.ext
import db
94 from google
.appengine
.ext
.remote_api
import remote_api_stub
104 host
= '%s.appspot.com' % app_id
106 remote_api_stub
.ConfigureRemoteDatastore(app_id
, '/remote_api', auth_func
, host
)
108 context
['deepFetch'] = deepFetch
110 code
.interact('App Engine interactive console for %s' % (app_id
,), None, context
)
114 """Sets up the sys.path and environment for development.
117 here
= os
.path
.abspath(__file__
)
118 here
= os
.path
.join(os
.path
.dirname(here
), '..')
119 here
= os
.path
.normpath(here
)
121 appengine_location
= os
.path
.join(here
, 'thirdparty', 'google_appengine')
124 os
.path
.join(appengine_location
, 'lib', 'django'),
125 os
.path
.join(appengine_location
, 'lib', 'webob'),
126 os
.path
.join(appengine_location
, 'lib', 'yaml', 'lib'),
128 os
.path
.join(here
, 'app'),
131 sys
.path
= extra_paths
+ sys
.path
133 os
.environ
['SERVER_SOFTWARE'] = 'Development Interactive Shell'
135 import main
as app_main
138 """Convenience wrapper that calls setup and remote.
145 if __name__
== '__main__':
146 if len(sys
.argv
) < 2:
147 print "Usage: %s app_id [host]" % (sys
.argv
[0],)