App Engine Python SDK version 1.8.9
[gae.git] / python / google / appengine / ext / analytics / entity.py
blob62e78aae2c9d596f22ad57bdbc982d4eaf04e0c7
1 #!/usr/bin/env python
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."""
24 def EntityKind(key):
25 """Given entity primary key as Reference Proto, returns entity kind.
27 Args:
28 key: primary key of entity in ReferenceProto form.
30 Returns:
31 Kind of entity in string format. Returns '' if
32 kind cannot be determined in some unexpected scenario.
33 """
34 if key.path().element_list():
35 return key.path().element_list()[-1].type()
36 else:
37 return ''
40 def EntityGroupKind(key):
41 """Given entity primary key as Reference Proto, returns kind of entity group.
43 Args:
44 key: primary key of entity in ReferenceProto form.
46 Returns:
47 Kind of entity group that entity belongs to in string format.
48 """
49 return key.path().element(0).type()
52 def EntityListKind(keylist):
53 """Given list of entity keys, return entity kind.
55 Args:
56 keylist: list of primary keys of entities in ReferenceProto form.
58 Returns:
59 Kind of entity. Returns 'None' if list is empty and 'Multi' if
60 entities in the list are of different kinds.
61 """
62 kinds = map(EntityKind, keylist)
63 unique_kinds = set(kinds)
64 numkinds = len(unique_kinds)
65 if numkinds > 1:
66 return 'Multi'
67 elif numkinds == 1:
68 return unique_kinds.pop()
69 else:
70 return 'None'
73 def EntityGroupName(entity):
74 """Given entity primary key as Reference Proto, returns entity group.
76 Args:
77 entity: primary key of entity in ReferenceProto form
79 Returns:
80 Name of entitygroup in string format.
81 """
82 element = entity.path().element(0)
83 if element.has_id():
84 return str(element.id())
85 elif element.has_name():
86 return element.name()
87 else:
89 return 'None'
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.
98 Args:
99 entity: primary key of entity in ReferenceProto form
101 Returns:
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.
106 names = []
107 for element in entity.path().element_list():
108 if element.has_id():
109 name = '%s:%s' %(element.type(), str(element.id()))
110 elif element.has_name():
111 name = '%s:%s' %(element.type(), str(element.name()))
112 else:
114 name = '%s:None' %(element.type())
115 names.append(name)
116 fullname = '.'.join(names)
117 return fullname