diff --git a/django/contrib/gis/management/commands/ogrinspect.py b/django/contrib/gis/management/commands/ogrinspect.py index c7b1389574f..78c610635aa 100644 --- a/django/contrib/gis/management/commands/ogrinspect.py +++ b/django/contrib/gis/management/commands/ogrinspect.py @@ -1,4 +1,6 @@ +import inspect from optparse import make_option + from django.contrib.gis import gdal from django.core.management.base import LabelCommand, CommandError @@ -83,27 +85,21 @@ class Command(LabelCommand): if not gdal.HAS_GDAL: raise CommandError('GDAL is required to inspect geospatial data sources.') - # Removing options with `None` values. - options = dict((k, v) for k, v in options.items() if not v is None) - # Getting the OGR DataSource from the string parameter. try: ds = gdal.DataSource(data_source) except gdal.OGRException as msg: raise CommandError(msg) - # Whether the user wants to generate the LayerMapping dictionary as well. - show_mapping = options.pop('mapping', False) - - # Getting rid of settings that `_ogrinspect` doesn't like. - options.pop('verbosity', False) - options.pop('settings', False) - # Returning the output of ogrinspect with the given arguments # and options. from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping - output = [s for s in _ogrinspect(ds, model_name, **options)] - if show_mapping: + # Filter options to params accepted by `_ogrinspect` + ogr_options = dict((k, v) for k, v in options.items() + if k in inspect.getargspec(_ogrinspect).args and v is not None) + output = [s for s in _ogrinspect(ds, model_name, **ogr_options)] + + if options['mapping']: # Constructing the keyword arguments for `mapping`, and # calling it on the data source. kwargs = {'geom_name': options['geom_name'], diff --git a/django/contrib/gis/tests/inspectapp/tests.py b/django/contrib/gis/tests/inspectapp/tests.py index 17642ea8df3..2690dc484d0 100644 --- a/django/contrib/gis/tests/inspectapp/tests.py +++ b/django/contrib/gis/tests/inspectapp/tests.py @@ -115,6 +115,13 @@ class OGRInspectTest(TestCase): ' objects = models.GeoManager()' )) + def test_management_command(self): + shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp') + out = StringIO() + call_command('ogrinspect', shp_file, 'City', stdout=out) + output = out.getvalue() + self.assertIn('class City(models.Model):', output) + def get_ogr_db_string(): """