3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
21 """Helper file to manipulate entity keys and names."""
25 """Given entity primary key as Reference Proto, returns entity kind.
28 key: primary key of entity in ReferenceProto form.
31 Kind of entity in string format. Returns '' if
32 kind cannot be determined in some unexpected scenario.
34 if key
.path().element_list():
35 return key
.path().element_list()[-1].type()
40 def EntityGroupKind(key
):
41 """Given entity primary key as Reference Proto, returns kind of entity group.
44 key: primary key of entity in ReferenceProto form.
47 Kind of entity group that entity belongs to in string format.
49 return key
.path().element(0).type()
52 def EntityListKind(keylist
):
53 """Given list of entity keys, return entity kind.
56 keylist: list of primary keys of entities in ReferenceProto form.
59 Kind of entity. Returns 'None' if list is empty and 'Multi' if
60 entities in the list are of different kinds.
62 kinds
= map(EntityKind
, keylist
)
63 unique_kinds
= set(kinds
)
64 numkinds
= len(unique_kinds
)
68 return unique_kinds
.pop()
73 def EntityGroupName(entity
):
74 """Given entity primary key as Reference Proto, returns entity group.
77 entity: primary key of entity in ReferenceProto form
80 Name of entitygroup in string format.
82 element
= entity
.path().element(0)
84 return str(element
.id())
85 elif element
.has_name():
92 def EntityFullName(entity
):
93 """Given entity primary key as a Reference Proto, returns full name.
95 This is a concatenation of entity information along the entire
96 path, and includes entity kind and entity name (or id) at each level.
99 entity: primary key of entity in ReferenceProto form
102 Full name of entity in string format with dots delimiting each element in
103 the path. Each element is represented as 'entity_kind:entity_id' or
104 'entity_kind:entity_name' as applicable.
107 for element
in entity
.path().element_list():
109 name
= '%s:%s' %(element
.type(), str(element
.id()))
110 elif element
.has_name():
111 name
= '%s:%s' %(element
.type(), str(element
.name()))
114 name
= '%s:None' %(element
.type())
116 fullname
= '.'.join(names
)