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

Returning native datetime objects for Bucket/Blob time properties. #807

Merged
merged 1 commit into from
Apr 9, 2015
Merged
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
33 changes: 17 additions & 16 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"""Create / interact with Google Cloud Storage blobs."""

import copy
import datetime
from io import BytesIO
import json
import mimetypes
import os
import time
import datetime
from io import BytesIO

import six
from six.moves.urllib.parse import quote # pylint: disable=F0401
Expand All @@ -37,6 +37,7 @@


_API_ACCESS_ENDPOINT = 'https://storage.googleapis.com'
_GOOGLE_TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'


class Blob(_PropertyMixin):
Expand Down Expand Up @@ -248,11 +249,7 @@ def download_to_filename(self, filename):
with open(filename, 'wb') as file_obj:
self.download_to_file(file_obj)

mtime = time.mktime(
datetime.datetime.strptime(
self._properties['updated'],
'%Y-%m-%dT%H:%M:%S.%fz').timetuple()
)
mtime = time.mktime(self.updated.timetuple())
os.utime(file_obj.name, (mtime, mtime))

def download_as_string(self):
Expand Down Expand Up @@ -660,24 +657,28 @@ def time_deleted(self):

See: https://cloud.google.com/storage/docs/json_api/v1/objects

:rtype: string or ``NoneType``
:returns: RFC3339 valid timestamp, or ``None`` if the property is not
set locally. If the blob has not been deleted, this will
never be set.
:rtype: :class:`datetime.datetime` or ``NoneType``
:returns: Datetime object parsed from RFC3339 valid timestamp, or
``None`` if the property is not set locally. If the blob has
not been deleted, this will never be set.
"""
return self._properties.get('timeDeleted')
value = self._properties.get('timeDeleted')
if value is not None:
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)

@property
def updated(self):
"""Retrieve the timestamp at which the object was updated.

See: https://cloud.google.com/storage/docs/json_api/v1/objects

:rtype: string or ``NoneType``
:returns: RFC3339 valid timestamp, or ``None`` if the property is not
set locally.
:rtype: :class:`datetime.datetime` or ``NoneType``
:returns: Datetime object parsed from RFC3339 valid timestamp, or
``None`` if the property is not set locally.
"""
return self._properties.get('updated')
value = self._properties.get('updated')
if value is not None:
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)


class _UploadConfig(object):
Expand Down
12 changes: 8 additions & 4 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
... print blob
"""

import datetime
import copy
import os
import six
Expand All @@ -45,6 +46,7 @@
from gcloud.storage.acl import DefaultObjectACL
from gcloud.storage.iterator import Iterator
from gcloud.storage.blob import Blob
from gcloud.storage.blob import _GOOGLE_TIMESTAMP_FORMAT


class _BlobIterator(Iterator):
Expand Down Expand Up @@ -684,11 +686,13 @@ def time_created(self):

See: https://cloud.google.com/storage/docs/json_api/v1/buckets

:rtype: string or ``NoneType``
:returns: RFC3339 valid timestamp, or ``None`` if the property is not
set locally.
:rtype: :class:`datetime.datetime` or ``NoneType``
:returns: Datetime object parsed from RFC3339 valid timestamp, or
``None`` if the property is not set locally.
"""
return self._properties.get('timeCreated')
value = self._properties.get('timeCreated')
if value is not None:
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)

@property
def versioning_enabled(self):
Expand Down
41 changes: 25 additions & 16 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ def test_generate_signed_url_w_default_method(self):
from gcloud.storage import blob as MUT

BLOB_NAME = 'blob-name'
EXPIRATION = '2014-10-16T20:34:37Z'
EXPIRATION = '2014-10-16T20:34:37.000Z'
connection = _Connection()
bucket = _Bucket(connection)
blob = self._makeOne(BLOB_NAME, bucket=bucket)
URI = ('http://example.com/abucket/a-blob-name?Signature=DEADBEEF'
'&Expiration=2014-10-16T20:34:37Z')
'&Expiration=2014-10-16T20:34:37.000Z')

SIGNER = _Signer()
with _Monkey(MUT, generate_signed_url=SIGNER):
Expand All @@ -151,12 +151,12 @@ def test_generate_signed_url_w_slash_in_name(self):
from gcloud.storage import blob as MUT

BLOB_NAME = 'parent/child'
EXPIRATION = '2014-10-16T20:34:37Z'
EXPIRATION = '2014-10-16T20:34:37.000Z'
connection = _Connection()
bucket = _Bucket(connection)
blob = self._makeOne(BLOB_NAME, bucket=bucket)
URI = ('http://example.com/abucket/a-blob-name?Signature=DEADBEEF'
'&Expiration=2014-10-16T20:34:37Z')
'&Expiration=2014-10-16T20:34:37.000Z')

SIGNER = _Signer()
with _Monkey(MUT, generate_signed_url=SIGNER):
Expand All @@ -176,12 +176,12 @@ def test_generate_signed_url_w_explicit_method(self):
from gcloud.storage import blob as MUT

BLOB_NAME = 'blob-name'
EXPIRATION = '2014-10-16T20:34:37Z'
EXPIRATION = '2014-10-16T20:34:37.000Z'
connection = _Connection()
bucket = _Bucket(connection)
blob = self._makeOne(BLOB_NAME, bucket=bucket)
URI = ('http://example.com/abucket/a-blob-name?Signature=DEADBEEF'
'&Expiration=2014-10-16T20:34:37Z')
'&Expiration=2014-10-16T20:34:37.000Z')

SIGNER = _Signer()
with _Monkey(MUT, generate_signed_url=SIGNER):
Expand Down Expand Up @@ -267,7 +267,6 @@ def test_download_to_file(self):
def test_download_to_filename(self):
import os
import time
import datetime
from six.moves.http_client import OK
from six.moves.http_client import PARTIAL_CONTENT
from tempfile import NamedTemporaryFile
Expand All @@ -292,11 +291,7 @@ def test_download_to_filename(self):
with open(f.name, 'rb') as g:
wrote = g.read()
mtime = os.path.getmtime(f.name)
updatedTime = time.mktime(
datetime.datetime.strptime(
blob._properties['updated'],
'%Y-%m-%dT%H:%M:%S.%fz').timetuple()
)
updatedTime = time.mktime(blob.updated.timetuple())
self.assertEqual(wrote, b'abcdef')
self.assertEqual(mtime, updatedTime)

Expand Down Expand Up @@ -990,22 +985,36 @@ def test_storage_class(self):
self.assertEqual(blob.storage_class, STORAGE_CLASS)

def test_time_deleted(self):
import datetime
BLOB_NAME = 'blob-name'
connection = _Connection()
bucket = _Bucket(connection)
TIME_DELETED = '2014-11-05T20:34:37Z'
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

TIME_DELETED = TIMESTAMP.isoformat() + '.000Z'
properties = {'timeDeleted': TIME_DELETED}
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
self.assertEqual(blob.time_deleted, TIME_DELETED)
self.assertEqual(blob.time_deleted, TIMESTAMP)

def test_time_deleted_unset(self):
BUCKET = object()
blob = self._makeOne('blob-name', bucket=BUCKET)
self.assertEqual(blob.time_deleted, None)

def test_updated(self):
import datetime
BLOB_NAME = 'blob-name'
connection = _Connection()
bucket = _Bucket(connection)
UPDATED = '2014-11-05T20:34:37Z'
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
UPDATED = TIMESTAMP.isoformat() + '.000Z'
properties = {'updated': UPDATED}
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
self.assertEqual(blob.updated, UPDATED)
self.assertEqual(blob.updated, TIMESTAMP)

def test_updated_unset(self):
BUCKET = object()
blob = self._makeOne('blob-name', bucket=BUCKET)
self.assertEqual(blob.updated, None)


class _Responder(object):
Expand Down
10 changes: 8 additions & 2 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,10 +856,16 @@ def test_storage_class(self):
self.assertEqual(bucket.storage_class, STORAGE_CLASS)

def test_time_created(self):
TIME_CREATED = '2014-11-05T20:34:37Z'
import datetime
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
TIME_CREATED = TIMESTAMP.isoformat() + '.000Z'
properties = {'timeCreated': TIME_CREATED}
bucket = self._makeOne(properties=properties)
self.assertEqual(bucket.time_created, TIME_CREATED)
self.assertEqual(bucket.time_created, TIMESTAMP)

def test_time_created_unset(self):
bucket = self._makeOne()
self.assertEqual(bucket.time_created, None)

def test_versioning_enabled_getter_missing(self):
NAME = 'name'
Expand Down