|
1 | 1 | import importlib
|
2 | 2 | import json
|
| 3 | +from distutils.version import StrictVersion |
| 4 | +from optparse import make_option |
3 | 5 |
|
| 6 | +from django import get_version as get_django_version |
4 | 7 | from django.core.management.base import BaseCommand, CommandError
|
5 | 8 |
|
| 9 | +LT_DJANGO_1_8 = StrictVersion(get_django_version()) < StrictVersion('1.8') |
6 | 10 |
|
7 |
| -class Command(BaseCommand): |
| 11 | +if LT_DJANGO_1_8: |
| 12 | + class CommandArguments(BaseCommand): |
| 13 | + option_list = BaseCommand.option_list + ( |
| 14 | + make_option( |
| 15 | + '--schema', |
| 16 | + type=str, |
| 17 | + dest='schema', |
| 18 | + default='', |
| 19 | + help='Django app containing schema to dump, e.g. myproject.core.schema', |
| 20 | + ), |
| 21 | + make_option( |
| 22 | + '--out', |
| 23 | + type=str, |
| 24 | + dest='out', |
| 25 | + default='', |
| 26 | + help='Output file (default: schema.json)' |
| 27 | + ), |
| 28 | + ) |
| 29 | +else: |
| 30 | + class CommandArguments(BaseCommand): |
| 31 | + |
| 32 | + def add_arguments(self, parser): |
| 33 | + from django.conf import settings |
| 34 | + parser.add_argument( |
| 35 | + '--schema', |
| 36 | + type=str, |
| 37 | + dest='schema', |
| 38 | + default=getattr(settings, 'GRAPHENE_SCHEMA', ''), |
| 39 | + help='Django app containing schema to dump, e.g. myproject.core.schema') |
| 40 | + |
| 41 | + parser.add_argument( |
| 42 | + '--out', |
| 43 | + type=str, |
| 44 | + dest='out', |
| 45 | + default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'), |
| 46 | + help='Output file (default: schema.json)') |
| 47 | + |
| 48 | + |
| 49 | +class Command(CommandArguments): |
8 | 50 | help = 'Dump Graphene schema JSON to file'
|
9 | 51 | can_import_settings = True
|
10 | 52 |
|
11 |
| - def add_arguments(self, parser): |
12 |
| - from django.conf import settings |
13 |
| - parser.add_argument( |
14 |
| - '--schema', |
15 |
| - type=str, |
16 |
| - dest='schema', |
17 |
| - default=getattr(settings, 'GRAPHENE_SCHEMA', ''), |
18 |
| - help='Django app containing schema to dump, e.g. myproject.core.schema') |
19 |
| - |
20 |
| - parser.add_argument( |
21 |
| - '--out', |
22 |
| - type=str, |
23 |
| - dest='out', |
24 |
| - default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'), |
25 |
| - help='Output file (default: schema.json)') |
| 53 | + def save_file(self, out, schema_dict): |
| 54 | + with open(out, 'w') as outfile: |
| 55 | + json.dump(schema_dict, outfile) |
26 | 56 |
|
27 | 57 | def handle(self, *args, **options):
|
28 |
| - schema_module = options['schema'] |
29 |
| - if schema_module == '': |
| 58 | + from django.conf import settings |
| 59 | + schema = options.get('schema') or getattr(settings, 'GRAPHENE_SCHEMA', '') |
| 60 | + out = options.get('out') or getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json') |
| 61 | + |
| 62 | + if schema == '': |
30 | 63 | raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema')
|
31 |
| - i = importlib.import_module(schema_module) |
| 64 | + i = importlib.import_module(schema) |
32 | 65 |
|
33 | 66 | schema_dict = {'data': i.schema.introspect()}
|
| 67 | + self.save_file(out, schema_dict) |
34 | 68 |
|
35 |
| - with open(options['out'], 'w') as outfile: |
36 |
| - json.dump(schema_dict, outfile) |
| 69 | + style = getattr(self, 'style', None) |
| 70 | + SUCCESS = getattr(style, 'SUCCESS', lambda x: x) |
37 | 71 |
|
38 |
| - self.stdout.write(self.style.SUCCESS('Successfully dumped GraphQL schema to %s' % options['out'])) |
| 72 | + self.stdout.write(SUCCESS('Successfully dumped GraphQL schema to %s' % out)) |
0 commit comments