Skip to content

Commit 98d6311

Browse files
committed
Updated examples to work with latest babel.
Test Plan: Ran examples Reviewers: guido
1 parent ecd2f97 commit 98d6311

File tree

4 files changed

+45
-53
lines changed

4 files changed

+45
-53
lines changed

doc/generator_ref.rst

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ conditions:
1818
``example.babelg.py``
1919

2020
2. At least one class must exist in the module that extends the
21-
``babelapi.generator.generator.CodeGenerator`` class and implements the
22-
abstract ``generate()`` method. BabelAPI automatically detects subclasses
23-
and calls the ``generate()`` method. All such subclasses will be called in
24-
ASCII order.
21+
``babelapi.generator.CodeGenerator`` class and implements the abstract
22+
``generate()`` method. BabelAPI automatically detects subclasses and calls
23+
the ``generate()`` method. All such subclasses will be called in ASCII
24+
order.
2525

2626
Getting Started
2727
===============
2828

2929
Here's a simple no-op generator::
3030

31-
from babelapi.generator.generator import CodeGenerator
31+
from babelapi.generator import CodeGenerator
3232

3333
class ExampleGenerator(CodeGenerator):
3434
def generate(self):
@@ -58,7 +58,7 @@ Here's an example generator that creates an output file for each namespace.
5858
Each file is named after a respective namespace and have a ``.cpp`` extension.
5959
Each file contains a one line C++-style comment::
6060

61-
from babelapi.generator.generator import CodeGenerator
61+
from babelapi.generator import CodeGenerator
6262

6363
class ExampleGenerator(CodeGenerator):
6464
def generate(self):
@@ -212,10 +212,10 @@ serve a different purpose.
212212
Indentation
213213
===========
214214

215-
The ``babelapi.generator.generator.CodeGenerator`` class provides a context
215+
The ``babelapi.generator.CodeGenerator`` class provides a context
216216
manager for adding incremental indentation. Here's an example::
217217

218-
from babelapi.generator.generator import CodeGenerator
218+
from babelapi.generator import CodeGenerator
219219

220220
class ExampleGenerator(CodeGenerator):
221221
def generate(self):
@@ -282,14 +282,14 @@ Example 1: List All Namespaces
282282
We'll create a generator ``ex1.babelg.py`` that generates a file called
283283
``ex1.out``. Each line in the file will be the name of a defined namespace::
284284

285-
from babelapi.generator.generator import CodeGenerator
285+
from babelapi.generator import CodeGenerator
286286

287287
class ExampleGenerator(CodeGenerator):
288288
def generate(self):
289289
"""Generates a file that lists each namespace."""
290290
with self.output_to_relative_path('ex1.out'):
291291
for namespace in self.api.namespaces.values():
292-
self.emit_line(namespace.name)
292+
self.emit(namespace.name)
293293

294294
We use ``output_to_relative_path()`` a member of ``CodeGenerator`` to specify
295295
where the output of our ``emit*()`` calls go (See more emit_methods_).
@@ -311,7 +311,7 @@ Example 2: A Python module for each Namespace
311311
Now we'll create a Python module for each namespace. Each module will define
312312
a ``noop()`` function::
313313

314-
from babelapi.generator.generator import CodeGenerator
314+
from babelapi.generator import CodeGenerator
315315

316316
class ExamplePythonGenerator(CodeGenerator):
317317
def generate(self):
@@ -323,9 +323,9 @@ a ``noop()`` function::
323323
self._generate_namespace_module(namespace)
324324

325325
def _generate_namespace_module(self, namespace):
326-
self.emit_line('def noop():')
326+
self.emit('def noop():')
327327
with self.indent():
328-
self.emit_line('pass')
328+
self.emit('pass')
329329

330330
Note how we used the ``self.indent()`` context manager to increase the
331331
indentation level by a default 4 spaces. If you want to use tabs instead,
@@ -354,8 +354,8 @@ for each struct in our specification. We'll extend from
354354
``MonolingualCodeGenerator``, which enforces that a ``lang`` class variable is
355355
declared::
356356

357-
from babelapi.data_type import Struct
358-
from babelapi.generator.generator import CodeGeneratorMonolingual
357+
from babelapi.data_type import is_struct_type
358+
from babelapi.generator import CodeGeneratorMonolingual
359359
from babelapi.lang.python import PythonTargetLanguage
360360

361361
class ExamplePythonGenerator(CodeGeneratorMonolingual):
@@ -375,43 +375,37 @@ declared::
375375

376376
def _generate_namespace_module(self, namespace):
377377
for data_type in namespace.linearize_data_types():
378-
if not isinstance(data_type, Struct):
379-
# Do not handle Union types
378+
if not is_struct_type(data_type):
379+
# Only handle user-defined structs (avoid unions and primitives)
380380
continue
381381

382382
# Define a class for each struct
383383
class_def = 'class {}(object):'.format(self.lang.format_class(data_type.name))
384-
self.emit_line(class_def)
384+
self.emit(class_def)
385385

386386
with self.indent():
387387
if data_type.doc:
388-
self.emit_line('"""')
389-
self.emit_wrapped_lines(data_type.doc)
390-
self.emit_line('"""')
388+
self.emit('"""')
389+
self.emit_wrapped_text(data_type.doc)
390+
self.emit('"""')
391391

392-
self.emit_empty_line()
392+
self.emit()
393393

394394
# Define constructor to take each field
395-
self.emit_line('def __init__', trailing_newline=False)
396395
args = ['self']
397396
for field in data_type.fields:
398397
args.append(self.lang.format_variable(field.name))
399-
self._generate_func_arg_list(args)
400-
self.emit(':')
401-
self.emit_empty_line()
398+
self.generate_multiline_list(args, 'def __init__', ':')
402399

403400
with self.indent():
404401
if data_type.fields:
402+
self.emit()
405403
# Body of init should assign all init vars
406404
for field in data_type.fields:
407405
if field.doc:
408-
self.emit_wrapped_lines(field.doc, prefix='# ')
406+
self.emit_wrapped_text(field.doc, '# ', '# ')
409407
member_name = self.lang.format_variable(field.name)
410-
self.emit_line('self.{0} = {0}'.format(member_name))
408+
self.emit('self.{0} = {0}'.format(member_name))
411409
else:
412-
self.emit_line('pass')
413-
self.emit_empty_line()
414-
415-
One new method of ``CodeGenerator`` that was used is ``generate_func_arg_list(args)``.
416-
It helps you generate a list of arguments in a function declaration or invocation
417-
enclosed by parentheses.
410+
self.emit('pass')
411+
self.emit()

example/generator/ex1/ex1.babelg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ def generate(self):
55
"""Generates a file that lists each namespace."""
66
with self.output_to_relative_path('ex1.out'):
77
for namespace in self.api.namespaces.values():
8-
self.emit_line(namespace.name)
8+
self.emit(namespace.name)

example/generator/ex2/ex2.babelg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ def generate(self):
1010
self._generate_namespace_module(namespace)
1111

1212
def _generate_namespace_module(self, namespace):
13-
self.emit_line('def noop():')
13+
self.emit('def noop():')
1414
with self.indent():
15-
self.emit_line('pass')
15+
self.emit('pass')
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from babelapi.data_type import Struct
1+
from babelapi.data_type import is_struct_type
22
from babelapi.generator import CodeGeneratorMonolingual
33
from babelapi.lang.python import PythonTargetLanguage
44

@@ -19,39 +19,37 @@ def generate(self):
1919

2020
def _generate_namespace_module(self, namespace):
2121
for data_type in namespace.linearize_data_types():
22-
if not isinstance(data_type, Struct):
23-
# Do not handle Union types
22+
if not is_struct_type(data_type):
23+
# Only handle user-defined structs (avoid unions and primitives)
2424
continue
2525

2626
# Define a class for each struct
2727
class_def = 'class {}(object):'.format(self.lang.format_class(data_type.name))
28-
self.emit_line(class_def)
28+
self.emit(class_def)
2929

3030
with self.indent():
3131
if data_type.doc:
32-
self.emit_line('"""')
33-
self.emit_wrapped_lines(data_type.doc)
34-
self.emit_line('"""')
32+
self.emit('"""')
33+
self.emit_wrapped_text(data_type.doc)
34+
self.emit('"""')
3535

36-
self.emit_empty_line()
36+
self.emit()
3737

3838
# Define constructor to take each field
39-
self.emit_line('def __init__', trailing_newline=False)
4039
args = ['self']
4140
for field in data_type.fields:
4241
args.append(self.lang.format_variable(field.name))
43-
self._generate_func_arg_list(args)
44-
self.emit(':')
45-
self.emit_empty_line()
42+
self.generate_multiline_list(args, 'def __init__', ':')
4643

4744
with self.indent():
4845
if data_type.fields:
46+
self.emit()
4947
# Body of init should assign all init vars
5048
for field in data_type.fields:
5149
if field.doc:
52-
self.emit_wrapped_lines(field.doc, prefix='# ')
50+
self.emit_wrapped_text(field.doc, '# ', '# ')
5351
member_name = self.lang.format_variable(field.name)
54-
self.emit_line('self.{0} = {0}'.format(member_name))
52+
self.emit('self.{0} = {0}'.format(member_name))
5553
else:
56-
self.emit_line('pass')
57-
self.emit_empty_line()
54+
self.emit('pass')
55+
self.emit()

0 commit comments

Comments
 (0)