Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage/demo: add demo script parser #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
93 changes: 93 additions & 0 deletions gcloud/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from code import interact
import os.path
import sys
import time

from gcloud import storage


__all__ = ['CLIENT_EMAIL', 'PRIVATE_KEY_PATH', 'PROJECT_NAME',
'get_connection', 'main']


CLIENT_EMAIL = '606734090113-6ink7iugcv89da9sru7lii8bs3i0obqg@developer.gserviceaccount.com'

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

PRIVATE_KEY_PATH = os.path.join(os.path.dirname(__file__), 'demo.key')
PROJECT_NAME = 'gcloud-storage-demo'

extra_newline = False

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

code_globals, code_locals = globals(), locals()


def get_storage_connection():
return storage.get_connection(PROJECT_NAME, CLIENT_EMAIL, PRIVATE_KEY_PATH)


def write(*strings):
# Add an extra newline if necessary.
global extra_newline
if extra_newline:
print

for string in strings:
print string
raw_input()

# We don't need an extra newline after this.
extra_newline = False


def code(string, comment=None):
keypress_time = 0.05

print '>>> ',
for char in string:
time.sleep(keypress_time)
sys.stdout.write(char)
sys.stdout.flush()

if comment:
sys.stdout.write(' # %s' % comment)

# Wait for an enter key before continuing...
raw_input()

# Yes, this is crazy unsafe... but it's demo code.
# Globalize these so our imports hang around...
global code_globals
global code_locals
exec(string, code_globals, code_locals)

# In the next non-code piece, we need an extra newline.
global extra_newline
extra_newline = True

def is_empty(line): return not bool(line.strip())
def is_comment(line): return line.startswith('#')
def is_code(line): return not is_empty(line) and not is_comment(line)

def consume(is_a, lines):
result = []
for l in lines:
if not is_a(l):
break
result.append(l)
return (result, lines[len(result):])

def trim_until_main(lines):
main_tag = 'if __name__ == \'__main__\':'
return lines[:lines.index(main_tag)] if main_tag in lines else lines

def run(script):
with open(script) as f:
lines = trim_until_main([l.strip() for l in f])
while len(lines):
comments, lines = consume(is_comment, lines)
if comments:
write(*comments)
loc, lines = consume(is_code, lines)
for c in loc:
code(c)
_, lines = consume(is_empty, lines)

global code_locals
interact('(Hit CTRL-D to exit...)', local=code_locals)
112 changes: 0 additions & 112 deletions gcloud/storage/demo.py

This file was deleted.

Empty file added gcloud/storage/demo/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions gcloud/storage/demo/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from gcloud import demo
import os.path

demo.run(os.path.join(os.path.dirname(__file__), 'script.py'))
39 changes: 39 additions & 0 deletions gcloud/storage/demo/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Welcome to the gCloud Storage Demo! (hit enter)

# We're going to walk through some of the basics...,
# Don't worry though. You don't need to do anything, just keep hitting enter...

# Let's start by importing the demo module and getting a connection:
from gcloud import demo
connection = demo.get_storage_connection()

# OK, now let's look at all of the buckets...
print connection.get_all_buckets() # This might take a second...

# Now let's create a new bucket...
import time
bucket_name = ("bucket-%s" % time.time()).replace(".", "") # Get rid of dots...
print bucket_name
bucket = connection.create_bucket(bucket_name)
print bucket

# Let's look at all of the buckets again...
print connection.get_all_buckets()

# How about we create a new key inside this bucket.
key = bucket.new_key("my-new-file.txt")

# Now let's put some data in there.
key.set_contents_from_string("this is some data!")

# ... and we can read that data back again.
print key.get_contents_as_string()

# Now let's delete that key.
print key.delete()

# And now that we're done, let's delete that bucket...
print bucket.delete()

# Alright! That's all!
# Here's an interactive prompt for you now...