Fixed #21716 -- Only passed arguments supported by ogrinspect

Thanks Marco Badan for the report.
This commit is contained in:
Claude Paroz 2013-12-31 14:36:45 +01:00
parent 75220d3b5d
commit d0eeddd6fc
2 changed files with 15 additions and 12 deletions

View File

@ -1,4 +1,6 @@
import inspect
from optparse import make_option from optparse import make_option
from django.contrib.gis import gdal from django.contrib.gis import gdal
from django.core.management.base import LabelCommand, CommandError from django.core.management.base import LabelCommand, CommandError
@ -83,27 +85,21 @@ class Command(LabelCommand):
if not gdal.HAS_GDAL: if not gdal.HAS_GDAL:
raise CommandError('GDAL is required to inspect geospatial data sources.') 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. # Getting the OGR DataSource from the string parameter.
try: try:
ds = gdal.DataSource(data_source) ds = gdal.DataSource(data_source)
except gdal.OGRException as msg: except gdal.OGRException as msg:
raise CommandError(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 # Returning the output of ogrinspect with the given arguments
# and options. # and options.
from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping
output = [s for s in _ogrinspect(ds, model_name, **options)] # Filter options to params accepted by `_ogrinspect`
if show_mapping: 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 # Constructing the keyword arguments for `mapping`, and
# calling it on the data source. # calling it on the data source.
kwargs = {'geom_name': options['geom_name'], kwargs = {'geom_name': options['geom_name'],

View File

@ -115,6 +115,13 @@ class OGRInspectTest(TestCase):
' objects = models.GeoManager()' ' 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(): def get_ogr_db_string():
""" """