2222
2323
2424class Entity (dict ):
25- """
25+ """
2626 :type dataset: :class:`gcloud.datastore.dataset.Dataset`
2727 :param dataset: The dataset in which this entity belongs.
2828
@@ -61,26 +61,26 @@ class Entity(dict):
6161 {'age': 20, 'name': 'JJ'}
6262 """
6363
64- def __init__ (self , dataset = None , kind = None ):
65- if dataset and kind :
66- self ._key = Key (dataset = dataset ).kind (kind )
67- else :
68- self ._key = None
64+ def __init__ (self , dataset = None , kind = None ):
65+ if dataset and kind :
66+ self ._key = Key (dataset = dataset ).kind (kind )
67+ else :
68+ self ._key = None
6969
70- def dataset (self ):
71- """Get the :class:`gcloud.datastore.dataset.Dataset` in which this entity belonds.
70+ def dataset (self ):
71+ """Get the :class:`gcloud.datastore.dataset.Dataset` in which this entity belonds.
7272
7373 .. note::
7474 This is based on the :class:`gcloud.datastore.key.Key` set on the entity.
7575 That means that if you have no key set, the dataset might be `None`.
7676 It also means that if you change the key on the entity, this will refer
7777 to that key's dataset.
7878 """
79- if self .key ():
80- return self .key ().dataset ()
79+ if self .key ():
80+ return self .key ().dataset ()
8181
82- def key (self , key = None ):
83- """Get or set the :class:`gcloud.datastore.key.Key` on the current entity.
82+ def key (self , key = None ):
83+ """Get or set the :class:`gcloud.datastore.key.Key` on the current entity.
8484
8585 :type key: :class:`glcouddatastore.key.Key`
8686 :param key: The key you want to set on the entity.
@@ -93,14 +93,14 @@ def key(self, key=None):
9393 <Key[{'kind': 'OtherKeyKind', 'id': 1234}]>
9494 """
9595
96- if key :
97- self ._key = key
98- return self
99- else :
100- return self ._key
96+ if key :
97+ self ._key = key
98+ return self
99+ else :
100+ return self ._key
101101
102- def kind (self ):
103- """Get the kind of the current entity.
102+ def kind (self ):
103+ """Get the kind of the current entity.
104104
105105 .. note::
106106 This relies entirely on
@@ -111,24 +111,24 @@ def kind(self):
111111 which knows its Kind.
112112 """
113113
114- if self .key ():
115- return self .key ().kind ()
114+ if self .key ():
115+ return self .key ().kind ()
116116
117- @classmethod
118- def from_key (cls , key ):
119- """Factory method for creating an entity based on the :class:`gcloud.datastore.key.Key`.
117+ @classmethod
118+ def from_key (cls , key ):
119+ """Factory method for creating an entity based on the :class:`gcloud.datastore.key.Key`.
120120
121121 :type key: :class:`gcloud.datastore.key.Key`
122122 :param key: The key for the entity.
123123
124124 :returns: The :class:`Entity` derived from the :class:`gcloud.datastore.key.Key`.
125125 """
126126
127- return cls ().key (key )
127+ return cls ().key (key )
128128
129- @classmethod
130- def from_protobuf (cls , pb , dataset = None ):
131- """Factory method for creating an entity based on a protobuf.
129+ @classmethod
130+ def from_protobuf (cls , pb , dataset = None ):
131+ """Factory method for creating an entity based on a protobuf.
132132
133133 The protobuf should be one returned from the Cloud Datastore Protobuf API.
134134
@@ -138,20 +138,20 @@ def from_protobuf(cls, pb, dataset=None):
138138 :returns: The :class:`Entity` derived from the :class:`gcloud.datastore.datastore_v1_pb2.Entity`.
139139 """
140140
141- # This is here to avoid circular imports.
142- from gcloud .datastore import helpers
141+ # This is here to avoid circular imports.
142+ from gcloud .datastore import helpers
143143
144- key = Key .from_protobuf (pb .key , dataset = dataset )
145- entity = cls .from_key (key )
144+ key = Key .from_protobuf (pb .key , dataset = dataset )
145+ entity = cls .from_key (key )
146146
147- for property_pb in pb .property :
148- value = helpers .get_value_from_protobuf (property_pb )
149- entity [property_pb .name ] = value
147+ for property_pb in pb .property :
148+ value = helpers .get_value_from_protobuf (property_pb )
149+ entity [property_pb .name ] = value
150150
151- return entity
151+ return entity
152152
153- def reload (self ):
154- """Reloads the contents of this entity from the datastore.
153+ def reload (self ):
154+ """Reloads the contents of this entity from the datastore.
155155
156156 This method takes the :class:`gcloud.datastore.key.Key`, loads all
157157 properties from the Cloud Datastore, and sets the updated properties on
@@ -163,51 +163,48 @@ def reload(self):
163163 only locally.
164164 """
165165
166- # Note that you must have a valid key, otherwise this makes no sense.
167- entity = self .dataset ().get_entity (self .key ().to_protobuf ())
166+ # Note that you must have a valid key, otherwise this makes no sense.
167+ entity = self .dataset ().get_entity (self .key ().to_protobuf ())
168168
169- if entity :
170- self .update (entity )
171- return self
169+ if entity :
170+ self .update (entity )
171+ return self
172172
173- def save (self ):
174- """Save the entity in the Cloud Datastore.
173+ def save (self ):
174+ """Save the entity in the Cloud Datastore.
175175
176176 :rtype: :class:`gcloud.datastore.entity.Entity`
177177 :returns: The entity with a possibly updated Key.
178178 """
179- key_pb = self .dataset ().connection ().save_entity (
180- dataset_id = self .dataset ().id (), key_pb = self .key ().to_protobuf (),
181- properties = dict (self ))
179+ key_pb = self .dataset ().connection ().save_entity (dataset_id = self .dataset ().id (), key_pb = self .key ().to_protobuf (), properties = dict (self ))
182180
183- # If we are in a transaction and the current entity needs an
184- # automatically assigned ID, tell the transaction where to put that.
185- transaction = self .dataset ().connection ().transaction ()
186- if transaction and self .key ().is_partial ():
187- transaction .add_auto_id_entity (self )
181+ # If we are in a transaction and the current entity needs an
182+ # automatically assigned ID, tell the transaction where to put that.
183+ transaction = self .dataset ().connection ().transaction ()
184+ if transaction and self .key ().is_partial ():
185+ transaction .add_auto_id_entity (self )
188186
189- if isinstance (key_pb , datastore_pb .Key ):
190- updated_key = Key .from_protobuf (key_pb )
191- # Update the path (which may have been altered).
192- key = self .key ().path (updated_key .path ())
193- self .key (key )
187+ if isinstance (key_pb , datastore_pb .Key ):
188+ updated_key = Key .from_protobuf (key_pb )
189+ # Update the path (which may have been altered).
190+ key = self .key ().path (updated_key .path ())
191+ self .key (key )
194192
195- return self
193+ return self
196194
197- def delete (self ):
198- """Delete the entity in the Cloud Datastore.
195+ def delete (self ):
196+ """Delete the entity in the Cloud Datastore.
199197
200198 .. note::
201199 This is based entirely off of the :class:`gcloud.datastore.key.Key` set
202200 on the entity. Whatever is stored remotely using the key on the entity
203201 will be deleted.
204202 """
205- self .dataset ().connection ().delete_entity (
206- dataset_id = self .dataset ().id (), key_pb = self .key ().to_protobuf ())
207-
208- def __repr__ (self ): #pragma NO COVER
209- # An entity should have a key all the time (even if it's partial).
210- if self .key ():
211- return '<Entity%s %s>' % (self .key ().path (), super (Entity , self ).__repr__ ())
212- else :
213- return '<Entity %s>' % (super (Entity , self ).__repr__ ())
203+ self .dataset ().connection ().delete_entity (dataset_id = self .dataset ().id (), key_pb = self .key ().to_protobuf ())
204+
205+ def __repr__ (self ): # pragma NO COVER
206+ # An entity should have a key all the time (even if it's partial).
207+ if self .key ():
208+ return '<Entity%s %s>' % (self .key ().path (), super (Entity , self ).__repr__ ())
209+ else :
210+ return '<Entity %s>' % (super (Entity , self ).__repr__ ())
0 commit comments