# Copyright 2012 Google Inc. All Rights Reserved.


"""A sample app that uses GCS client to operate on bucket and file."""

import os
import cloudstorage as gcs
import webapp2

"""
Retry can help overcome transient urlfetch or GCS issues, such as timeouts.
"""
my_default_retry_params = gcs.RetryParams(initial_delay=0.2,
                                          max_delay=5.0,
                                          backoff_factor=2,
                                          max_retry_period=15)
"""
All requests to GCS using the GCS client within current GAE request and
current thread will use this retry params as default. If a default is not
set via this mechanism, the library's built-in default will be used.
Any GCS client function can also be given a more specific retry params
that overrides the default.
"""
gcs.set_default_retry_params(my_default_retry_params)


class MainPage(webapp2.RequestHandler):

  def get(self):
    bucket = '/yey-cloud-storage-trial'
    filename = bucket + '/demo-testfile'

    self.response.headers['Content-Type'] = 'text/plain'
    self.tmp_filenames_to_clean_up = []

    self.create_file(filename)
    self.response.write('\n\n')

    self.read_file(filename)
    self.response.write('\n\n')

    self.stat_file(filename)
    self.response.write('\n\n')

    self.list_bucket(bucket)
    self.response.write('\n\n')

    self.delete_files()

  def create_file(self, filename):
    """Create a file.

    The retry_params specified in the open call will override the default
    retry params for this particular file handle.

    Args:
      filename: filename.
    """
    self.response.write('Creating file %s\n' % filename)

    write_retry_params = gcs.RetryParams(backoff_factor=1.1)
    gcs_file = gcs.open(filename,
                        'w',
                        content_type='text/plain',
                        options={'x-goog-meta-foo': 'foo',
                                 'x-goog-meta-bar': 'bar'},
                        retry_params=write_retry_params)
    gcs_file.write('abcde\n')
    gcs_file.write('f'*1024*1024 + '\n')
    gcs_file.close()
    self.tmp_filenames_to_clean_up.append(filename)

  def read_file(self, filename):
    self.response.write('Truncated file content:\n')

    gcs_file = gcs.open(filename)
    self.response.write(gcs_file.readline())
    gcs_file.seek(-1024, os.SEEK_END)
    self.response.write(gcs_file.read())
    gcs_file.close()

  def stat_file(self, filename):
    self.response.write('File stat:\n')

    stat = gcs.stat(filename)
    self.response.write(repr(stat))

  def list_bucket(self, bucket):
    """Create several files and paginate through them.

    Production apps should set page_size to a practical value.

    Args:
      bucket: bucket.
    """
    self.response.write('Creating more files for listbucket...\n')
    self.create_file(bucket + '/foo1')
    self.create_file(bucket + '/foo2')
    self.response.write('\nListbucket result:\n')

    page_size = 1
    stats = gcs.listbucket(bucket, max_keys=page_size)
    while True:
      count = 0
      for stat in stats:
        count += 1
        self.response.write(repr(stat))
        self.response.write('\n')

      if count != page_size or count == 0:
        break
      last_filename = stat.filename[len(bucket)+1:]
      stats = gcs.listbucket(bucket, max_keys=page_size, marker=last_filename)

  def delete_files(self):
    self.response.write('Deleting files...\n')
    for filename in self.tmp_filenames_to_clean_up:
      try:
        gcs.delete(filename)
      except gcs.NotFoundError:
        pass


app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

@@ -174,92 +174,8 @@
 p2%0A%0A
-%22%22%22%0ARetry can help overcome transient urlfetch or GCS issues, such as timeouts.%0A%22%22%22%0A
 my_d
@@ -415,330 +415,8 @@
 15)%0A
-%22%22%22%0AAll requests to GCS using the GCS client within current GAE request and%0Acurrent thread will use this retry params as default. If a default is not%0Aset via this mechanism, the library's built-in default will be used.%0AAny GCS client function can also be given a more specific retry params%0Athat overrides the default.%0A%22%22%22%0A
 gcs.

Add doc tags to sample app