Added tag v0-5-20090608 for changeset 19e9fdde919e
[Melange.git] / scripts / interactive.py
blob98cdafd8585432ae6bfc887b09db47f2914f6345
1 #!/usr/bin/python2.5
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.
19 Usage is simple:
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]
26 """
28 __authors__ = [
29 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
34 import code
35 import getpass
36 import os
37 import sys
40 def auth_func():
41 """Returns a tuple with username and password.
42 """
44 return raw_input('Username:'), getpass.getpass('Password:')
47 def deepFetch(queryGen,key=None,batchSize = 100):
48 """Iterator that yields an entity in batches.
50 Args:
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).
56 """
58 from google.appengine.ext import db
60 # AppEngine will not fetch more than 1000 results
61 batchSize = min(batchSize,1000)
63 query = None
64 done = False
65 count = 0
67 if key:
68 key = db.Key(key)
70 while not done:
71 print count
72 query = queryGen()
73 if key:
74 query.filter("__key__ > ",key)
75 results = query.fetch(batchSize)
76 for result in results:
77 count += 1
78 yield result
79 if batchSize > len(results):
80 done = True
81 else:
82 key = results[-1].key()
85 def remote(args, context=None):
86 """Starts a shell with the datastore as remote_api_stub.
88 Args:
89 args: arguments from the user
90 context: locals that should be added to the shell
91 """
93 from google.appengine.ext import db
94 from google.appengine.ext.remote_api import remote_api_stub
96 if not context:
97 context = {}
99 app_id = args[0]
101 if len(args) > 1:
102 host = args[1]
103 else:
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)
113 def setup():
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')
123 extra_paths = [here,
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'),
127 appengine_location,
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
137 def main(args):
138 """Convenience wrapper that calls setup and remote.
141 setup()
142 remote(args)
145 if __name__ == '__main__':
146 if len(sys.argv) < 2:
147 print "Usage: %s app_id [host]" % (sys.argv[0],)
148 sys.exit(1)
150 main(sys.argv[1:])