Skip to content

Commit 6d2e110

Browse files
committed
Python and Swift generators now live directly in Stone package.
We've now returned to the model where there are some generators that are built-in. Instead of specifying the full generator path which can be confusing and difficult (Deploying data files reliably isn't easy...), the CLI now accepts "python_types", "python/swift_client".
1 parent 352cae3 commit 6d2e110

17 files changed

+159
-144
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
zip_safe=False,
3030
packages=['stone',
3131
'stone.lang',
32-
'stone.target'],
32+
'stone.target',
33+
'stone.target.python_rsrc'],
3334
long_description=open('README.rst').read(),
3435
platforms=['CPython 2.6', 'CPython 2.7'],
3536
entry_points={

stone/cli.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
from .lang.exception import InvalidSpec
2020
from .lang.tower import TowerOfStone
2121

22+
# These generators come by default
23+
_builtin_generators = (
24+
'python_types',
25+
'python_client',
26+
'swift_client',
27+
)
28+
2229
# The parser for command line arguments
2330
_cmdline_parser = argparse.ArgumentParser(description='StoneAPI')
2431
_cmdline_parser.add_argument(
@@ -27,10 +34,14 @@
2734
action='count',
2835
help='Print debugging statements.',
2936
)
37+
_generator_help = (
38+
'Either the name of a built-in generator or the path to a generator '
39+
'module. Paths to generator modules must end with a .stoneg.py extension. '
40+
'The following generators are built-in: ' + ', '.join(_builtin_generators))
3041
_cmdline_parser.add_argument(
3142
'generator',
3243
type=six.text_type,
33-
help='Specify the path to a generator. It must have a .stoneg.py extension.',
44+
help=_generator_help,
3445
)
3546
_cmdline_parser.add_argument(
3647
'output',
@@ -223,7 +234,10 @@ def main():
223234
del namespace.route_by_name[route.name]
224235
namespace.routes = filtered_routes
225236

226-
if not os.path.exists(args.generator):
237+
if args.generator in _builtin_generators:
238+
generator_module = __import__(
239+
'stone.target.%s' % args.generator, fromlist=[''])
240+
elif not os.path.exists(args.generator):
227241
print("error: Generator '%s' cannot be found." % args.generator,
228242
file=sys.stderr)
229243
sys.exit(1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Make this a package so that the Python generator tests can import these.
File renamed without changes.
File renamed without changes.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
unwrap_nullable,
2929
)
3030
from stone.generator import CodeGenerator
31-
from stone.target.python import (
31+
from stone.target.python_helpers import (
3232
fmt_class,
3333
fmt_func,
3434
fmt_obj,
@@ -87,15 +87,15 @@ def generate(self, api):
8787
Each namespace will have Python classes to represent data types and
8888
routes in the Stone spec.
8989
"""
90-
cur_folder = os.path.dirname(__file__)
90+
rsrc_folder = os.path.join(os.path.dirname(__file__), 'python_rsrc')
9191
self.logger.info('Copying stone_validators.py to output folder')
92-
shutil.copy(os.path.join(cur_folder, 'stone_validators.py'),
92+
shutil.copy(os.path.join(rsrc_folder, 'stone_validators.py'),
9393
self.target_folder_path)
9494
self.logger.info('Copying stone_serializers.py to output folder')
95-
shutil.copy(os.path.join(cur_folder, 'stone_serializers.py'),
95+
shutil.copy(os.path.join(rsrc_folder, 'stone_serializers.py'),
9696
self.target_folder_path)
9797
self.logger.info('Copying stone_base.py to output folder')
98-
shutil.copy(os.path.join(cur_folder, 'stone_base.py'),
98+
shutil.copy(os.path.join(rsrc_folder, 'stone_base.py'),
9999
self.target_folder_path)
100100
for namespace in api.namespaces.values():
101101
with self.output_to_relative_path('{}.py'.format(namespace.name)):
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
is_void_type,
3030
)
3131
from stone.generator import CodeGenerator
32-
from stone.target.swift import (
32+
from stone.target.swift_helpers import (
3333
fmt_class,
3434
fmt_func,
3535
fmt_obj,
@@ -63,20 +63,20 @@ def _docf(self, tag, val):
6363
return val
6464

6565
def generate(self, api):
66-
cur_folder = os.path.dirname(__file__)
66+
rsrc_folder = os.path.join(os.path.dirname(__file__), 'swift_rsrc')
6767
self.logger.info('Copying StoneSerializers.swift to output folder')
68-
shutil.copy(os.path.join(cur_folder, 'StoneSerializers.swift'),
68+
shutil.copy(os.path.join(rsrc_folder, 'StoneSerializers.swift'),
6969
os.path.join(self.target_folder_path, 'Source'))
7070

7171
self.logger.info('Copying StoneValidators.swift to output folder')
72-
shutil.copy(os.path.join(cur_folder, 'StoneValidators.swift'),
72+
shutil.copy(os.path.join(rsrc_folder, 'StoneValidators.swift'),
7373
os.path.join(self.target_folder_path, 'Source'))
7474

7575
self.logger.info('Copying Client.swift to output folder')
76-
shutil.copy(os.path.join(cur_folder, 'Client.swift'),
76+
shutil.copy(os.path.join(rsrc_folder, 'Client.swift'),
7777
os.path.join(self.target_folder_path, 'Source'))
7878

79-
jazzy_cfg_path = os.path.join(cur_folder, 'jazzy.json')
79+
jazzy_cfg_path = os.path.join(rsrc_folder, 'jazzy.json')
8080
with open(jazzy_cfg_path) as jazzy_file:
8181
jazzy_cfg = json.load(jazzy_file)
8282

@@ -98,7 +98,7 @@ def generate(self, api):
9898
self.emit_raw(json.dumps(jazzy_cfg, indent=2)+'\n')
9999

100100
client_path = os.path.join('Source', 'DropboxClient.swift')
101-
dropbox_raw_path = os.path.join(cur_folder, 'DropboxClient-raw.swift')
101+
dropbox_raw_path = os.path.join(rsrc_folder, 'DropboxClient-raw.swift')
102102

103103
with open(dropbox_raw_path) as raw_client, self.output_to_relative_path(client_path):
104104
self._generate_bound_client(api, raw_client)

0 commit comments

Comments
 (0)