-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
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.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
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: | ||
|
||
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) |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.