Skip to content

Commit 5d139a9

Browse files
committed
Added import statement.
Summary: - Added support for forward references. - Changed spec path reporting in errors to account for references in other namespaces. - supertype -> parent_type, subtype -> parent_type Test Plan: Added tests and tested on metaserver. Reviewed By: guido
1 parent 5989a71 commit 5d139a9

File tree

12 files changed

+1344
-624
lines changed

12 files changed

+1344
-624
lines changed

babelapi/api.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from distutils.version import StrictVersion
55

66
from 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

babelapi/babel/lexer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test(self, data):
146146
'error',
147147
'extends',
148148
'attrs',
149-
'include',
149+
'import',
150150
'namespace',
151151
'pass',
152152
'request',
@@ -160,7 +160,7 @@ def test(self, data):
160160
'deprecated': 'DEPRECATED',
161161
'extends': 'EXTENDS',
162162
'attrs': 'ATTRS',
163-
'include': 'INCLUDE',
163+
'import': 'IMPORT',
164164
'pass': 'PASS',
165165
'route': 'ROUTE',
166166
'struct': 'STRUCT',

0 commit comments

Comments
 (0)