44from distutils .version import StrictVersion
55
66from babelapi .data_type import (
7+ CompositeType ,
8+ ForeignRef ,
79 doc_unwrap ,
810 is_composite_type ,
911 is_list_type ,
@@ -40,6 +42,7 @@ def __init__(self, name):
4042 self .route_by_name = {}
4143 self .data_types = []
4244 self .data_type_by_name = {}
45+ self .referenced_namespaces = []
4346
4447 def add_route (self , route ):
4548 self .routes .append (route )
@@ -49,6 +52,19 @@ def add_data_type(self, data_type):
4952 self .data_types .append (data_type )
5053 self .data_type_by_name [data_type .name ] = data_type
5154
55+ def add_referenced_namespace (self , namespace ):
56+ """
57+ For a namespace to be considered "referenced," it must be imported by
58+ this namespace. Also, at least one data type from the namespace must be
59+ referenced by this one. The caller of this method is responsible for
60+ verifying these requirements.
61+ """
62+ assert self .name != namespace .name , \
63+ 'Namespace cannot reference itself.'
64+ if namespace .name not in self .referenced_namespaces :
65+ self .referenced_namespaces .append (namespace )
66+ self .referenced_namespaces .sort (key = lambda n : n .name )
67+
5268 def linearize_data_types (self ):
5369 """
5470 Returns a list of all data types used in the namespace. Because the
@@ -63,10 +79,11 @@ def linearize_data_types(self):
6379 def add_data_type (data_type ):
6480 if data_type in seen_data_types :
6581 return
66- if hasattr (data_type , 'supertype' ) and data_type .supertype :
67- add_data_type (data_type .supertype )
68- elif hasattr (data_type , 'subtype' ) and data_type .subtype :
69- add_data_type (data_type .subtype )
82+ elif isinstance (data_type , ForeignRef ):
83+ # We're only concerned with types defined in this namespace.
84+ return
85+ if isinstance (data_type , CompositeType ) and data_type .parent_type :
86+ add_data_type (data_type .parent_type )
7087 linearized_data_types .append (data_type )
7188 seen_data_types .add (data_type )
7289
@@ -102,30 +119,32 @@ class ApiRoute(object):
102119
103120 def __init__ (self ,
104121 name ,
105- doc ,
106- request_data_type ,
107- response_data_type ,
108- error_data_type ,
109- attrs ,
110122 token ):
111123 """
112124 :param str name: Designated name of the endpoint.
125+ :param token: Raw route definition from the parser.
126+ :type token: babelapi.babel.parser.BabelRouteDef
127+ """
128+ self .name = name
129+ self ._token = token
130+
131+ def set_attributes (self , doc , request_data_type , response_data_type ,
132+ error_data_type , attrs ):
133+ """
134+ Converts a forward reference definition of a route into a full
135+ definition.
136+
113137 :param str doc: Description of the endpoint.
114138 :type request_data_type: :class:`babelapi.data_type.DataType`
115139 :type response_data_type: :class:`babelapi.data_type.DataType`
116140 :type error_data_type: :class:`babelapi.data_type.DataType`
117141 :param dict attrs: Map of string keys to values that are either int,
118142 float, bool, str, or None. These are the route attributes assigned
119143 in the spec.
120- :param token: Raw route definition from the parser.
121- :type token: babelapi.babel.parser.BabelRouteDef
122144 """
123-
124- self .name = name
125145 self .raw_doc = doc
126146 self .doc = doc_unwrap (doc )
127147 self .request_data_type = request_data_type
128148 self .response_data_type = response_data_type
129149 self .error_data_type = error_data_type
130150 self .attrs = attrs
131- self ._token = token
0 commit comments