Skip to content

Commit a93ceb2

Browse files
committed
Remove type=timestamp usages
This change removes the UNIXEpoch and ISO8601 Format classes from openstack.format as well as all of their usages in Resource classes and their tests. Additionally, this removes the need to depend on oslo.utils and iso8601. Change-Id: I617fb9f0739490a3401f14a654e30b33a476ca59
1 parent 7934570 commit a93ceb2

70 files changed

Lines changed: 130 additions & 578 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

openstack/block_store/v2/snapshot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ class Snapshot(resource.Resource):
3939
#: Description of snapshot. Default is None.
4040
description = resource.prop("description")
4141
#: The timestamp of this snapshot creation.
42-
#: *Type: datetime object parsed from ISO 8601 formatted string*
43-
created_at = resource.prop("created_at", type=format.ISO8601)
42+
created_at = resource.prop("created_at")
4443
#: Metadata associated with this snapshot.
4544
metadata = resource.prop("metadata", type=dict)
4645
#: The ID of the volume this snapshot was taken of.

openstack/block_store/v2/volume.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class Volume(resource.Resource):
6868
#: TODO(briancurtin): This is currently undocumented in the API.
6969
attachments = resource.prop("attachments")
7070
#: The timestamp of this volume creation.
71-
#: *Type: datetime object parsed from ISO 8601 formatted string*
72-
created_at = resource.prop("created_at", type=format.ISO8601)
71+
created_at = resource.prop("created_at")
7372

7473

7574
class VolumeDetail(Volume):

openstack/cluster/v1/action.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313

1414
from openstack.cluster import cluster_service
15-
from openstack import format
1615
from openstack import resource
1716

1817

@@ -40,11 +39,9 @@ class Action(resource.Resource):
4039
#: Interval in seconds between two consecutive executions.
4140
interval = resource.prop('interval')
4241
#: The time the action was started.
43-
#: *Type: datetime object parsed from a UNIX epoch*
44-
start_at = resource.prop('start_time', type=format.UNIXEpoch)
42+
start_at = resource.prop('start_time')
4543
#: The time the action completed execution.
46-
#: *Type: datetime object parsed from a UNIX epoch*
47-
end_at = resource.prop('end_time', type=format.UNIXEpoch)
44+
end_at = resource.prop('end_time')
4845
#: The timeout in seconds.
4946
timeout = resource.prop('timeout')
5047
#: Current status of the action.
@@ -61,8 +58,6 @@ class Action(resource.Resource):
6158
#: A list of actions that can start only after this action has finished.
6259
depended_by = resource.prop('depended_by', type=list)
6360
#: Timestamp when the action is created.
64-
#: *Type: datetime object parsed from ISO 8601 formatted string*
65-
created_at = resource.prop('created_at', type=format.ISO8601)
61+
created_at = resource.prop('created_at')
6662
#: Timestamp when the action was last updated.
67-
#: *Type: datetime object parsed from ISO 8601 formatted string*
68-
updated_at = resource.prop('updated_at', type=format.ISO8601)
63+
updated_at = resource.prop('updated_at')

openstack/cluster/v1/policy.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# under the License.
1212

1313
from openstack.cluster import cluster_service
14-
from openstack import format
1514
from openstack import resource
1615

1716

@@ -36,11 +35,9 @@ class Policy(resource.Resource):
3635
#: The type name of the policy.
3736
type = resource.prop('type')
3837
#: The timestamp when the policy is created.
39-
#: *Type: datetime object parsed from ISO 8601 formatted string*
40-
created_at = resource.prop('created_at', type=format.ISO8601)
38+
created_at = resource.prop('created_at')
4139
#: The timestamp when the policy was last updated.
42-
#: *Type: datetime object parsed from ISO 8601 formatted string*
43-
updated_at = resource.prop('updated_at', type=format.ISO8601)
40+
updated_at = resource.prop('updated_at')
4441
#: The specification of the policy.
4542
spec = resource.prop('spec', type=dict)
4643
#: A dictionary containing runtime data of the policy.

openstack/cluster/v1/profile.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# under the License.
1212

1313
from openstack.cluster import cluster_service
14-
from openstack import format
1514
from openstack import resource
1615

1716

@@ -40,8 +39,6 @@ class Profile(resource.Resource):
4039
#: A collection of key-value pairs that are attached to the profile.
4140
metadata = resource.prop('metadata', type=dict)
4241
#: Timestamp of when the profile was created.
43-
#: *Type: datetime object parsed from ISO 8601 formatted string*
44-
created_at = resource.prop('created_at', type=format.ISO8601)
42+
created_at = resource.prop('created_at')
4543
#: Timestamp of when the profile was last updated.
46-
#: *Type: datetime object parsed from ISO 8601 formatted string*
47-
updated_at = resource.prop('updated_at', type=format.ISO8601)
44+
updated_at = resource.prop('updated_at')

openstack/cluster/v1/receiver.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13-
1413
from openstack.cluster import cluster_service
15-
from openstack import format
1614
from openstack import resource
1715

1816

@@ -44,11 +42,9 @@ class Receiver(resource.Resource):
4442
#: The name of the targeted action.
4543
action = resource.prop('action')
4644
#: Timestamp of when the receiver was created.
47-
#: *Type: datetime object parsed from ISO 8601 formatted string*
48-
created_at = resource.prop('created_at', type=format.ISO8601)
45+
created_at = resource.prop('created_at')
4946
#: Timestamp of when the receiver was last updated.
50-
#: *Type: datetime object parsed from ISO 8601 formatted string*
51-
updated_at = resource.prop('updated_at', type=format.ISO8601)
47+
updated_at = resource.prop('updated_at')
5248
#: The credential of the impersonated user.
5349
actor = resource.prop('actor', type=dict)
5450
#: A dictionary containing key-value pairs that are provided to the

openstack/compute/v2/extension.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# under the License.
1212

1313
from openstack.compute import compute_service
14-
from openstack import format
1514
from openstack import resource
1615

1716

@@ -39,5 +38,4 @@ class Extension(resource.Resource):
3938
#: A URL pointing to the namespace for this extension.
4039
namespace = resource.prop('namespace')
4140
#: Timestamp when this extension was last updated.
42-
#: *Type: datetime object parsed from ISO 8601 formatted string*
43-
updated_at = resource.prop('updated', type=format.ISO8601)
41+
updated_at = resource.prop('updated')

openstack/compute/v2/image.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from openstack.compute import compute_service
1414
from openstack.compute.v2 import metadata
15-
from openstack import format
1615
from openstack import resource
1716

1817

@@ -34,8 +33,7 @@ class Image(resource.Resource, metadata.MetadataMixin):
3433
#: The name of this image.
3534
name = resource.prop('name')
3635
#: Timestamp when the image was created.
37-
#: *Type: datetime object parsed from ISO 8601 formatted string*
38-
created_at = resource.prop('created', type=format.ISO8601)
36+
created_at = resource.prop('created')
3937
#: Metadata pertaining to this image. *Type: dict*
4038
metadata = resource.prop('metadata', type=dict)
4139
#: The mimimum disk size. *Type: int*
@@ -48,8 +46,7 @@ class Image(resource.Resource, metadata.MetadataMixin):
4846
#: The status of this image.
4947
status = resource.prop('status')
5048
#: Timestamp when the image was updated.
51-
#: *Type: datetime object parsed from ISO 8601 formatted string*
52-
updated_at = resource.prop('updated', type=format.ISO8601)
49+
updated_at = resource.prop('updated')
5350
#: Size of the image in bytes. *Type: int*
5451
size = resource.prop('OS-EXT-IMG-SIZE:size', type=int)
5552

openstack/compute/v2/server.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from openstack.compute import compute_service
1414
from openstack.compute.v2 import metadata
15-
from openstack import format
1615
from openstack import resource
1716
from openstack import utils
1817

@@ -41,8 +40,7 @@ class Server(resource.Resource, metadata.MetadataMixin):
4140
#: of the IP address. *Type: dict*
4241
addresses = resource.prop('addresses', type=dict)
4342
#: Timestamp of when the server was created.
44-
#: *Type: datetime object parsed from ISO 8601 formatted string*
45-
created_at = resource.prop('created', type=format.ISO8601)
43+
created_at = resource.prop('created')
4644
#: The flavor reference, as a ID or full URL, for the flavor to use for
4745
#: this server.
4846
flavor_id = resource.prop('flavorRef')
@@ -69,8 +67,7 @@ class Server(resource.Resource, metadata.MetadataMixin):
6967
#: ``SUSPENDED``, ``UNKNOWN``, or ``VERIFY_RESIZE``.
7068
status = resource.prop('status')
7169
#: Timestamp of when this server was last updated.
72-
#: *Type: datetime object parsed from ISO 8601 formatted string*
73-
updated_at = resource.prop('updated', type=format.ISO8601)
70+
updated_at = resource.prop('updated')
7471
#: The user ID associated with this server.
7572
user_id = resource.prop('user_id')
7673

openstack/format.py

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13-
from datetime import datetime
14-
import numbers
15-
import time
16-
17-
from iso8601 import iso8601
18-
import six
19-
20-
from oslo_utils import timeutils
21-
2213

2314
class Formatter(object):
2415

@@ -33,82 +24,6 @@ def deserialize(cls, value):
3324
raise NotImplementedError
3425

3526

36-
class ISO8601(Formatter):
37-
38-
@classmethod
39-
def serialize(cls, value):
40-
"""Convert a datetime to an ISO8601 string"""
41-
if isinstance(value, datetime):
42-
return value.isoformat()
43-
elif isinstance(value, six.string_types):
44-
# If we're already given a string, keep it as-is.
45-
# This happens when a string comes back in a response body,
46-
# as opposed to the datetime case above which happens when
47-
# a user is setting a datetime for a request.
48-
return value
49-
else:
50-
raise ValueError("Unable to serialize ISO8601: %s" % value)
51-
52-
@classmethod
53-
def deserialize(cls, value):
54-
"""Convert an ISO8601 string to a datetime object"""
55-
if isinstance(value, six.string_types):
56-
return timeutils.parse_isotime(value)
57-
else:
58-
raise ValueError("Unable to deserialize ISO8601: %s" % value)
59-
60-
61-
class UNIXEpoch(Formatter):
62-
63-
EPOCH = datetime(1970, 1, 1, tzinfo=iso8601.UTC)
64-
65-
@classmethod
66-
def serialize(cls, value):
67-
"""Convert a datetime to a UNIX epoch"""
68-
if isinstance(value, datetime):
69-
# Do not try to format using %s as it's platform dependent.
70-
return (value - cls.EPOCH).total_seconds()
71-
elif isinstance(value, numbers.Number):
72-
return value
73-
else:
74-
raise ValueError("Unable to serialize UNIX epoch: %s" % value)
75-
76-
@classmethod
77-
def deserialize(cls, value):
78-
"""Convert a UNIX epoch into a datetime object"""
79-
try:
80-
value = float(value)
81-
except ValueError:
82-
raise ValueError("Unable to deserialize UNIX epoch: %s" % value)
83-
84-
# gmtime doesn't respect microseconds so we need to parse them out
85-
# if they're there and build the datetime appropriately with the
86-
# proper precision.
87-
# NOTES:
88-
# 1. datetime.fromtimestamp sort of solves this but using localtime
89-
# instead of UTC, which we need.
90-
# 2. On Python 2 we can't just str(value) as it truncates digits
91-
# that are significant to us.
92-
parsed_value = "%000000f" % value
93-
decimal = parsed_value.find(".")
94-
95-
if decimal == -1:
96-
microsecond = 0
97-
else:
98-
# Some examples of these timestamps include less precision
99-
# than the allowable 6 digits that can represent microseconds,
100-
# so since we have a string we need to construct a real
101-
# count of microseconds instead of just converting the
102-
# stringified amount to an int.
103-
fractional_second = float(parsed_value[decimal:]) * 1e6
104-
microsecond = int(fractional_second)
105-
106-
gmt = time.gmtime(value)
107-
108-
return datetime(*gmt[:6], microsecond=microsecond,
109-
tzinfo=iso8601.UTC)
110-
111-
11227
class BoolStr(Formatter):
11328

11429
@classmethod

0 commit comments

Comments
 (0)