44from itertools import izip
55
66from gcloud .datastore import datastore_v1_pb2 as datastore_pb
7- from gcloud .datastore .dataset import Dataset
87
98
109class Key (object ):
@@ -13,22 +12,23 @@ class Key(object):
1312 .. automethod:: __init__
1413 """
1514
16- def __init__ (self , dataset = None , namespace = None , path = None ):
15+ def __init__ (self , path = None , namespace = None , dataset_id = None ):
1716 """Constructor / initializer for a key.
1817
19- :type dataset: :class:`gcloud.datastore.dataset.Dataset`
20- :param dataset: A dataset instance for the key.
21-
2218 :type namespace: :class:`str`
2319 :param namespace: A namespace identifier for the key.
2420
2521 :type path: sequence of dicts
2622 :param path: Each dict must have keys 'kind' (a string) and optionally
2723 'name' (a string) or 'id' (an integer).
24+
25+ :type dataset_id: string
26+ :param dataset: The dataset ID assigned by back-end for the key.
27+ Leave as None for newly-created keys.
2828 """
29- self ._dataset = dataset
30- self ._namespace = namespace
3129 self ._path = path or [{'kind' : '' }]
30+ self ._namespace = namespace
31+ self ._dataset_id = dataset_id
3232
3333 def _clone (self ):
3434 """Duplicates the Key.
@@ -40,12 +40,10 @@ def _clone(self):
4040 :rtype: :class:`gcloud.datastore.key.Key`
4141 :returns: a new `Key` instance
4242 """
43- clone = copy .deepcopy (self )
44- clone ._dataset = self ._dataset # Make a shallow copy of the Dataset.
45- return clone
43+ return copy .deepcopy (self )
4644
4745 @classmethod
48- def from_protobuf (cls , pb , dataset = None ):
46+ def from_protobuf (cls , pb ):
4947 """Factory method for creating a key based on a protobuf.
5048
5149 The protobuf should be one returned from the Cloud Datastore
@@ -54,10 +52,6 @@ def from_protobuf(cls, pb, dataset=None):
5452 :type pb: :class:`gcloud.datastore.datastore_v1_pb2.Key`
5553 :param pb: The Protobuf representing the key.
5654
57- :type dataset: :class:`gcloud.datastore.dataset.Dataset`
58- :param dataset: A dataset instance. If not passed, defaults to an
59- instance whose ID is derived from pb.
60-
6155 :rtype: :class:`gcloud.datastore.key.Key`
6256 :returns: a new `Key` instance
6357 """
@@ -75,13 +69,10 @@ def from_protobuf(cls, pb, dataset=None):
7569
7670 path .append (element_dict )
7771
78- if not dataset :
79- dataset = Dataset (id = pb .partition_id .dataset_id )
80- namespace = pb .partition_id .namespace
81- else :
82- namespace = None
72+ dataset_id = pb .partition_id .dataset_id or None
73+ namespace = pb .partition_id .namespace
8374
84- return cls (dataset , namespace , path )
75+ return cls (path , namespace , dataset_id )
8576
8677 def to_protobuf (self ):
8778 """Return a protobuf corresponding to the key.
@@ -91,18 +82,8 @@ def to_protobuf(self):
9182 """
9283 key = datastore_pb .Key ()
9384
94- # Technically a dataset is required to do anything with the key,
95- # but we shouldn't throw a cryptic error if one isn't provided
96- # in the initializer.
97- if self .dataset ():
98- # Apparently 's~' is a prefix for High-Replication and is necessary
99- # here. Another valid preflix is 'e~' indicating EU datacenters.
100- dataset_id = self .dataset ().id ()
101- if dataset_id :
102- if dataset_id [:2 ] not in ['s~' , 'e~' ]:
103- dataset_id = 's~' + dataset_id
104-
105- key .partition_id .dataset_id = dataset_id
85+ if self ._dataset_id is not None :
86+ key .partition_id .dataset_id = self ._dataset_id
10687
10788 if self ._namespace :
10889 key .partition_id .namespace = self ._namespace
@@ -161,24 +142,6 @@ def is_partial(self):
161142 """
162143 return self .id_or_name () is None
163144
164- def dataset (self , dataset = None ):
165- """Dataset setter / getter.
166-
167- :type dataset: :class:`gcloud.datastore.dataset.Dataset`
168- :param dataset: A dataset instance for the key.
169-
170- :rtype: :class:`Key` (for setter); or
171- :class:`gcloud.datastore.dataset.Dataset` (for getter)
172- :returns: a new key, cloned from self., with the given dataset
173- (setter); or self's dataset (getter).
174- """
175- if dataset :
176- clone = self ._clone ()
177- clone ._dataset = dataset
178- return clone
179- else :
180- return self ._dataset
181-
182145 def namespace (self , namespace = None ):
183146 """Namespace setter / getter.
184147
0 commit comments