Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 772d926

Browse files
committed
Merge pull request GoogleCloudPlatform#260 from pcostell/shell
Create a demo shell.
2 parents bc68b65 + 0c2f6b9 commit 772d926

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

demo/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ different handlers.
1111
* main.py: home page and user handling including login/logout.
1212
* fibo.py: calculate the fibonacci sequence, taking advantage of ndb tasklets.
1313

14-
## Setup
14+
This folder also has a demo for using ndb from outside of App Engine. You can use `shell.py` to interact with your Datastore using ndb through the Cloud Datastore API.
15+
16+
## Setup (demo application)
1517

1618
1. Copy ndb (the entire folder) into the demo subdirectory.
1719
2. Deploy the demo application using the command:
@@ -21,3 +23,19 @@ different handlers.
2123
This will deploy the app to your default project, which you can configure using gcloud:
2224

2325
gcloud config set project <project-id>
26+
27+
## Shell
28+
29+
1. First, ensure your environment is setup.
30+
31+
export DATASTORE_PROJECT_ID=<project-id>
32+
export DATASTORE_USE_PROJECT_ID_AS_APP_ID=true
33+
34+
2. Then, make sure you have authenticated using gcloud.
35+
36+
gcloud auth login
37+
38+
3. Finally, run the shell. From here you can write ndb code to interact with Datastore. Don't forget, this shell is interacting with the production Datastore of your <project-id>.
39+
40+
python demo/shell.py
41+

demo/shell.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#
2+
# Copyright 2015 The ndb Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import atexit
17+
import code
18+
import readline
19+
import os
20+
import sys
21+
22+
import ndb
23+
from ndb import tasklets
24+
from ndb.google_imports import datastore_pbs
25+
26+
HISTORY_PATH = os.path.expanduser('~/.ndb_shell_history')
27+
28+
def shell():
29+
30+
if (not os.environ.get('DATASTORE_APP_ID', None)
31+
and not os.environ.get('DATASTORE_PROJECT_ID', None)):
32+
raise ValueError('Must set either DATASTORE_APP_ID or DATASTORE_PROJECT_ID'
33+
' environment variable.')
34+
35+
ndb.get_context().set_memcache_policy(False)
36+
ndb.get_context().set_cache_policy(False)
37+
38+
# ndb will set the application ID.
39+
application_id = os.environ['APPLICATION_ID']
40+
id_resolver = datastore_pbs.IdResolver((application_id,))
41+
project_id = id_resolver.resolve_project_id(application_id)
42+
43+
banner = """ndb shell
44+
Python %s
45+
Project: %s
46+
The ndb module is already imported.
47+
""" % (sys.version, project_id)
48+
49+
imports = {
50+
'ndb': ndb,
51+
}
52+
53+
# set up the environment
54+
os.environ['SERVER_SOFTWARE'] = 'Development (ndb_shell)/0.1'
55+
56+
sys.ps1 = '%s> ' % project_id
57+
if readline is not None:
58+
# set up readline
59+
readline.parse_and_bind('tab: complete')
60+
atexit.register(lambda: readline.write_history_file(HISTORY_PATH))
61+
if os.path.exists(HISTORY_PATH):
62+
readline.read_history_file(HISTORY_PATH)
63+
64+
code.interact(banner=banner, local=imports)
65+
66+
if __name__ == '__main__':
67+
shell()

0 commit comments

Comments
 (0)