MCPcopy
hub / github.com/django/django / handle

Method handle

django/contrib/gis/management/commands/ogrinspect.py:111–164  ·  view source on GitHub ↗
(self, *args, **options)

Source from the content-addressed store, hash-verified

109 )
110
111 def handle(self, *args, **options):
112 data_source, model_name = options.pop("data_source"), options.pop("model_name")
113
114 # Getting the OGR DataSource from the string parameter.
115 try:
116 ds = gdal.DataSource(data_source)
117 except gdal.GDALException as msg:
118 raise CommandError(msg)
119
120 # Returning the output of ogrinspect with the given arguments
121 # and options.
122 from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping
123
124 # Filter options to params accepted by `_ogrinspect`
125 ogr_options = {
126 k: v
127 for k, v in options.items()
128 if k in get_func_args(_ogrinspect) and v is not None
129 }
130 output = [s for s in _ogrinspect(ds, model_name, **ogr_options)]
131
132 if options["mapping"]:
133 # Constructing the keyword arguments for `mapping`, and
134 # calling it on the data source.
135 kwargs = {
136 "geom_name": options["geom_name"],
137 "layer_key": options["layer_key"],
138 "multi_geom": options["multi_geom"],
139 }
140 mapping_dict = mapping(ds, **kwargs)
141 # This extra legwork is so that the dictionary definition comes
142 # out in the same order as the fields in the model definition.
143 rev_mapping = {v: k for k, v in mapping_dict.items()}
144 output.extend(
145 [
146 "",
147 "",
148 "# Auto-generated `LayerMapping` dictionary for %s model"
149 % model_name,
150 "%s_mapping = {" % model_name.lower(),
151 ]
152 )
153 output.extend(
154 " '%s': '%s'," % (rev_mapping[ogr_fld], ogr_fld)
155 for ogr_fld in ds[options["layer_key"]].fields
156 )
157 output.extend(
158 [
159 " '%s': '%s',"
160 % (options["geom_name"], mapping_dict[options["geom_name"]]),
161 "}",
162 ]
163 )
164 return "\n".join(output)

Callers

nothing calls this directly

Calls 8

CommandErrorClass · 0.90
get_func_argsFunction · 0.90
_ogrinspectFunction · 0.90
mappingFunction · 0.90
extendMethod · 0.80
popMethod · 0.45
itemsMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected