def _GetCategory(self): '''Returns the category this node belongs to. ''' if self._lookup_path[-2] in GetNodeCategories(): return self._lookup_path[-2] # If lookup_path[-2] is not a valid category and lookup_path[-1] is # 'callback', then we know we have an event callback. if self._lookup_path[-1] == 'callback': return 'events' if self._lookup_path[-2] == 'parameters': # Function parameters are modelled as properties. return 'properties' if (self._lookup_path[-1].endswith('Type') and (self._lookup_path[-1][:-len('Type')] == self._lookup_path[-2] or self._lookup_path[-1][:-len('ReturnType')] == self._lookup_path[-2])): # Array elements and function return objects have 'Type' and 'ReturnType' # appended to their names, respectively, in model.py. This results in # lookup paths like # 'events > types > Rule > properties > tags > tagsType'. # These nodes are treated as properties. return 'properties' if self._lookup_path[0] == 'events': # HACK(ahernandez.miralles): This catches a few edge cases, # such as 'webviewTag > events > consolemessage > level'. return 'properties' raise AssertionError('Could not classify node %s' % self)
def _GetByNameDict(namespace): '''Returns a dictionary mapping names to named items from |namespace|. This lets us render specific API entities rather than the whole thing at once, for example {{apis.manifestTypes.byName.ExternallyConnectable}}. Includes items from namespace['types'], namespace['functions'], namespace['events'], and namespace['properties']. ''' by_name = {} for item_type in GetNodeCategories(): if item_type in namespace: old_size = len(by_name) by_name.update( (item['name'], item) for item in namespace[item_type]) assert len(by_name) == old_size + len(namespace[item_type]), ( 'Duplicate name in %r' % namespace) return by_name
def _GetParentPath(self): '''Returns the path pointing to this node's parent. ''' assert len(self._lookup_path) > 1, \ 'Tried to look up parent for the top-level node.' # lookup_path[-1] is the name of the current node. If this lookup_path # describes a regular node, then lookup_path[-2] will be a node category. # Otherwise, it's an event callback or a function parameter. if self._lookup_path[-2] not in GetNodeCategories(): if self._lookup_path[-1] == 'callback': # This is an event callback, so lookup_path[-2] is the event # node name, thus lookup_path[-3] must be 'events'. assert self._lookup_path[-3] == 'events' return self._lookup_path[:-1] # This is a function parameter. assert self._lookup_path[-2] == 'parameters' return self._lookup_path[:-2] # This is a regular node, so lookup_path[-2] should # be a node category. self._AssertIsValidCategory(self._lookup_path[-2]) return self._lookup_path[:-2]
def _AssertIsValidCategory(self, category): assert category in GetNodeCategories(), \ '%s is not a valid category. Full path: %s' % (category, str(self))