Skip to content

Commit 91cece4

Browse files
committed
CLI is more sane. $ babelapi <generator> <specs...> <output>
babelg is inner extension for generator files now, as opposed to babelt.
1 parent bb59f88 commit 91cece4

File tree

20 files changed

+94
-1840
lines changed

20 files changed

+94
-1840
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parsetab.py
77
/.idea
88
/build
99
/dist
10+
/output
1011
/MANIFEST
1112
/babelapi.egg-info
1213
/setuptools-3.1-py2.7.egg

README.rst

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ you can replace ``babelapi`` with ``python -m babelapi.cli`` as follows::
7070
Simple Example
7171
--------------
7272

73-
You can compile an example spec describing the Dropbox API and apply it to a
74-
Python code generator::
73+
You can compile an example spec, ``users.babel`` describing the Dropbox API and
74+
generate Python code using ``base_namespace.babelg.py``::
7575

76-
$ babelapi example/api/dbx-core/users.babel example/generator/dropbox-python-sdk/
76+
$ babelapi example/generator/dropbox-python-sdk/dbx_python_json.babelg.py example/api/dbx-core/users.babel output/
7777

7878
You can view the generated code at::
7979

80-
$ example/generator/dropbox-python-sdk/base_users.py
80+
$ output/base_users.py
8181

8282
File Types
8383
==========
@@ -95,7 +95,7 @@ Header (.babelh extension)
9595
Headers define only data types available in an API. Headers can be included
9696
in spec files so that common data types can be re-used.
9797

98-
Code Generator (.babelt.py extension)
98+
Code Generator (.babelg.py extension)
9999
--------------------------------------
100100

101101
Code generators are Python modules that implement the abstract
@@ -455,8 +455,8 @@ Defining a Code Generator
455455
A code generator is a Python class which will generate code for a target language
456456
given an API description. A code generator must satisfy the following conditions:
457457

458-
1. The filename must have ``.babelt.py`` as its extension. For example,
459-
``example.babelt.py``
458+
1. The filename must have ``.babelg.py`` as its extension. For example,
459+
``example.babelg.py``
460460

461461
2. A class must exist in the file that extends the
462462
``babelapi.generator.generator.CodeGenerator`` class and implements the
@@ -480,7 +480,7 @@ folder.
480480
Example 1: List All Namespaces
481481
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
482482

483-
We'll create a generator ``ex1.babelt.py`` that generates a file called
483+
We'll create a generator ``ex1.babelg.py`` that generates a file called
484484
``ex1.out``. Each line in the file will be the name of a defined namespace::
485485

486486
from babelapi.generator.generator import CodeGenerator
@@ -499,6 +499,7 @@ Run the generator from the root of the BabelAPI folder using the example specs
499499
we've provided::
500500

501501
$ babelapi example/api/dbx-core*.babel example/generator/ex1
502+
$ babelapi example/generator/ex1/ex1.babelg.py example/api/dbx-core/*.babel output/ex1
502503

503504
Now examine the contents of the output::
504505

@@ -566,14 +567,14 @@ class to ``True``.
566567
Run the generator from the root of the BabelAPI folder using the example specs
567568
we've provided::
568569

569-
$ babelapi example/api/dbx-core*.babel example/generator/ex2
570+
$ babelapi example/generator/ex2/ex2.babelg.py example/api/dbx-core/*.babel output/ex2
570571

571572
Now examine the contents of the output::
572573

573-
$ cat example/generator/ex2/files.py
574+
$ cat output/ex2/files.py
574575
def noop():
575576
pass
576-
$ cat example/generator/ex2/users.py
577+
$ cat output/ex2/users.py
577578
def noop():
578579
pass
579580

babelapi/babel/tower.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(self, paths, version='0.1b1', debug=False):
7070
"""Creates a new tower of babel."""
7171

7272
self._debug = debug
73-
self._logger = logging.getLogger('babelapi.dsl.tower')
73+
self._logger = logging.getLogger('babelapi.idl')
7474

7575
self.api = Api(version=version)
7676

@@ -86,6 +86,7 @@ def __init__(self, paths, version='0.1b1', debug=False):
8686
def parse(self):
8787
"""Parses each Babel file and returns an API description."""
8888
for path, scripture in self._scriptures:
89+
self._logger.info('Parsing spec %s', path)
8990
res = self.parse_scripture(scripture)
9091
if res:
9192
self.add_to_api(path, res)

babelapi/cli.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@ def main():
1919
'-v',
2020
'--verbose',
2121
action='store_true',
22-
help='Print debugging statements.'
22+
help='Print debugging statements.',
2323
)
2424
cmdline_parser.add_argument(
25-
'-t',
26-
'--target-path',
25+
'generator',
2726
type=str,
28-
help='The path to save compiled source files to.'
27+
help='The path to the generator (*.py).',
2928
)
30-
cmdline_parser.add_argument('api', nargs='+', type=str, help='Babel files describing the API.')
3129
cmdline_parser.add_argument(
32-
'path',
33-
help='The path to the template SDK for the target language.',
30+
'spec',
31+
nargs='+',
32+
type=str,
33+
help='Path to API specifications (*.babel).',
3434
)
35+
cmdline_parser.add_argument(
36+
'output',
37+
type=str,
38+
help='The folder to save generated files to.',
39+
)
40+
3541
cmdline_parser.add_argument(
3642
'--clean-build',
3743
action='store_true',
@@ -46,30 +52,22 @@ def main():
4652
logging_level = logging.INFO
4753
logging.basicConfig(level=logging_level)
4854

49-
logging.info('Analyzing these API specifications: %r', args.api)
50-
51-
if args.api[0].endswith('.py'):
52-
# Special case if the API description file ends in .py
53-
# Assume it's an internal representation used for testing.
55+
if args.spec[0].startswith('+') and args.spec[0].endswith('.py'):
56+
# Hack: Special case for defining a spec in Python for testing purposes
5457
try:
5558
api = imp.load_source('api', args.api[0]).api
5659
except ImportError as e:
5760
print >> sys.stderr, 'Could not import API description due to:', e
5861
sys.exit(1)
5962
else:
6063
# TODO: Needs version
61-
tower = TowerOfBabel(args.api, debug=debug)
64+
tower = TowerOfBabel(args.spec, debug=debug)
6265
api = tower.parse()
6366

64-
if args.target_path:
65-
build_path = os.path.join(args.target_path, os.path.basename(args.path))
66-
else:
67-
build_path = None
68-
6967
c = Compiler(
7068
api,
71-
args.path,
72-
build_path,
69+
args.generator,
70+
args.output,
7371
clean_build=args.clean_build,
7472
)
7573
c.build()

0 commit comments

Comments
 (0)