From d9bcba9b29725a3b717846db651bf917630610ff Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 24 Apr 2015 17:24:07 +0200 Subject: [PATCH] Removed many HAS_GEOS conditional imports --- django/contrib/gis/db/models/__init__.py | 10 ------ django/contrib/gis/geos/__init__.py | 31 ++++++------------- django/contrib/gis/geos/base.py | 10 ------ django/contrib/gis/geos/geometry.py | 8 ++--- tests/gis_tests/distapp/tests.py | 12 +++---- tests/gis_tests/geo3d/tests.py | 17 +++++----- tests/gis_tests/geoadmin/tests.py | 11 +++---- tests/gis_tests/geoapp/test_feeds.py | 4 +-- tests/gis_tests/geoapp/test_functions.py | 15 ++++----- tests/gis_tests/geoapp/test_regress.py | 7 ++--- tests/gis_tests/geoapp/test_serializers.py | 6 ++-- tests/gis_tests/geoapp/test_sitemaps.py | 4 +-- tests/gis_tests/geoapp/tests.py | 16 +++++----- tests/gis_tests/geogapp/tests.py | 5 +-- tests/gis_tests/geos_tests/test_geos.py | 16 +++++----- .../geos_tests/test_geos_mutation.py | 16 ++++------ tests/gis_tests/geos_tests/test_io.py | 9 ++---- tests/gis_tests/relatedapp/tests.py | 14 ++++----- tests/gis_tests/test_geoforms.py | 7 ++--- tests/gis_tests/test_geoip.py | 5 +-- 20 files changed, 78 insertions(+), 145 deletions(-) diff --git a/django/contrib/gis/db/models/__init__.py b/django/contrib/gis/db/models/__init__.py index 0ab75a1f05..835e907526 100644 --- a/django/contrib/gis/db/models/__init__.py +++ b/django/contrib/gis/db/models/__init__.py @@ -1,15 +1,5 @@ -from django.core.exceptions import ImproperlyConfigured - # Want to get everything from the 'normal' models package. from django.db.models import * # NOQA -from django.utils.version import get_docs_version - -from django.contrib.gis.geos import HAS_GEOS - -if not HAS_GEOS: - raise ImproperlyConfigured( - "GEOS is required and has not been detected. Are you sure it is installed? " - "See also https://docs.djangoproject.com/en/%s/ref/contrib/gis/install/geolibs/" % get_docs_version()) # Geographic aggregate functions from django.contrib.gis.db.models.aggregates import * # NOQA diff --git a/django/contrib/gis/geos/__init__.py b/django/contrib/gis/geos/__init__.py index d5fe479dae..48b321c6de 100644 --- a/django/contrib/gis/geos/__init__.py +++ b/django/contrib/gis/geos/__init__.py @@ -3,29 +3,18 @@ The GeoDjango GEOS module. Please consult the GeoDjango documentation for more details: http://geodjango.org/docs/geos.html """ -__all__ = ['HAS_GEOS'] +from .collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon # NOQA +from .error import GEOSException, GEOSIndexError # NOQA +from .factory import fromfile, fromstr # NOQA +from .geometry import GEOSGeometry, wkt_regex, hex_regex # NOQA +from .io import WKTReader, WKTWriter, WKBReader, WKBWriter # NOQA +from .libgeos import geos_version, geos_version_info # NOQA +from .linestring import LineString, LinearRing # NOQA +from .point import Point # NOQA +from .polygon import Polygon # NOQA try: - from .libgeos import geos_version, geos_version_info # NOQA: flake8 detects only the last __all__ + geos_version_info() HAS_GEOS = True - __all__ += ['geos_version', 'geos_version_info'] except ImportError: HAS_GEOS = False - -if HAS_GEOS: - from .geometry import GEOSGeometry, wkt_regex, hex_regex - from .point import Point - from .linestring import LineString, LinearRing - from .polygon import Polygon - from .collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon - from .error import GEOSException, GEOSIndexError - from .io import WKTReader, WKTWriter, WKBReader, WKBWriter - from .factory import fromfile, fromstr - - __all__ += [ - 'GEOSGeometry', 'wkt_regex', 'hex_regex', 'Point', 'LineString', - 'LinearRing', 'Polygon', 'GeometryCollection', 'MultiPoint', - 'MultiLineString', 'MultiPolygon', 'GEOSException', 'GEOSIndexError', - 'WKTReader', 'WKTWriter', 'WKBReader', 'WKBWriter', 'fromfile', - 'fromstr', - ] diff --git a/django/contrib/gis/geos/base.py b/django/contrib/gis/geos/base.py index 634ed7d3d1..9487845026 100644 --- a/django/contrib/gis/geos/base.py +++ b/django/contrib/gis/geos/base.py @@ -2,16 +2,6 @@ from ctypes import c_void_p from django.contrib.gis.geos.error import GEOSException -# Trying to import GDAL libraries, if available. Have to place in -# try/except since this package may be used outside GeoDjango. -try: - from django.contrib.gis import gdal -except ImportError: - # A 'dummy' gdal module. - class GDALInfo(object): - HAS_GDAL = False - gdal = GDALInfo() - class GEOSBase(object): """ diff --git a/django/contrib/gis/geos/geometry.py b/django/contrib/gis/geos/geometry.py index 98ccda1057..42cecc59c8 100644 --- a/django/contrib/gis/geos/geometry.py +++ b/django/contrib/gis/geos/geometry.py @@ -6,10 +6,10 @@ from __future__ import unicode_literals from ctypes import addressof, byref, c_double -from django.contrib.gis.gdal.error import SRSException +from django.contrib.gis import gdal from django.contrib.gis.geometry.regex import hex_regex, json_regex, wkt_regex from django.contrib.gis.geos import prototypes as capi -from django.contrib.gis.geos.base import GEOSBase, gdal +from django.contrib.gis.geos.base import GEOSBase from django.contrib.gis.geos.coordseq import GEOSCoordSeq from django.contrib.gis.geos.error import GEOSException, GEOSIndexError from django.contrib.gis.geos.libgeos import GEOM_PTR @@ -449,7 +449,7 @@ class GEOSGeometry(GEOSBase, ListMixin): if self.srid: try: return gdal.OGRGeometry(self.wkb, self.srid) - except SRSException: + except gdal.SRSException: pass return gdal.OGRGeometry(self.wkb) @@ -461,7 +461,7 @@ class GEOSGeometry(GEOSBase, ListMixin): if self.srid: try: return gdal.SpatialReference(self.srid) - except SRSException: + except gdal.SRSException: pass return None diff --git a/tests/gis_tests/distapp/tests.py b/tests/gis_tests/distapp/tests.py index 6638b9ed00..c18763cad4 100644 --- a/tests/gis_tests/distapp/tests.py +++ b/tests/gis_tests/distapp/tests.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals from django.contrib.gis.db.models.functions import ( Area, Distance, Length, Perimeter, Transform, ) -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geos import GEOSGeometry, LineString, Point from django.contrib.gis.measure import D # alias for Distance from django.db import connection from django.db.models import Q @@ -11,12 +11,10 @@ from django.test import TestCase, ignore_warnings, skipUnlessDBFeature from django.utils.deprecation import RemovedInDjango21Warning from ..utils import no_oracle, oracle, postgis, spatialite - -if HAS_GEOS: - from django.contrib.gis.geos import GEOSGeometry, LineString, Point - - from .models import (AustraliaCity, Interstate, SouthTexasInterstate, - SouthTexasCity, SouthTexasCityFt, CensusZipcode, SouthTexasZipcode) +from .models import ( + AustraliaCity, CensusZipcode, Interstate, SouthTexasCity, SouthTexasCityFt, + SouthTexasInterstate, SouthTexasZipcode, +) @skipUnlessDBFeature("gis_enabled") diff --git a/tests/gis_tests/geo3d/tests.py b/tests/gis_tests/geo3d/tests.py index 93c2158f6d..2feecaa417 100644 --- a/tests/gis_tests/geo3d/tests.py +++ b/tests/gis_tests/geo3d/tests.py @@ -4,26 +4,25 @@ import os import re from unittest import skipUnless +from django.contrib.gis.db.models import Extent3D, Union from django.contrib.gis.db.models.functions import ( AsGeoJSON, AsKML, Length, Perimeter, Scale, Translate, ) from django.contrib.gis.gdal import HAS_GDAL -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geos import GEOSGeometry, LineString, Point, Polygon from django.test import TestCase, ignore_warnings, skipUnlessDBFeature from django.utils._os import upath from django.utils.deprecation import ( RemovedInDjango20Warning, RemovedInDjango21Warning, ) -if HAS_GEOS: - from django.contrib.gis.db.models import Union, Extent3D - from django.contrib.gis.geos import GEOSGeometry, LineString, Point, Polygon +from .models import ( + City3D, Interstate2D, Interstate3D, InterstateProj2D, InterstateProj3D, + MultiPoint3D, Point2D, Point3D, Polygon2D, Polygon3D, +) - from .models import (City3D, Interstate2D, Interstate3D, InterstateProj2D, - InterstateProj3D, Point2D, Point3D, MultiPoint3D, Polygon2D, Polygon3D) - - if HAS_GDAL: - from django.contrib.gis.utils import LayerMapping, LayerMapError +if HAS_GDAL: + from django.contrib.gis.utils import LayerMapping, LayerMapError data_path = os.path.realpath(os.path.join(os.path.dirname(upath(__file__)), '..', 'data')) diff --git a/tests/gis_tests/geoadmin/tests.py b/tests/gis_tests/geoadmin/tests.py index 07af5a0d64..fc6c031dc0 100644 --- a/tests/gis_tests/geoadmin/tests.py +++ b/tests/gis_tests/geoadmin/tests.py @@ -1,14 +1,11 @@ from __future__ import unicode_literals -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis import admin +from django.contrib.gis.geos import Point from django.test import TestCase, override_settings, skipUnlessDBFeature -if HAS_GEOS: - from django.contrib.gis import admin - from django.contrib.gis.geos import Point - - from .admin import UnmodifiableAdmin - from .models import site, City +from .admin import UnmodifiableAdmin +from .models import City, site @skipUnlessDBFeature("gis_enabled") diff --git a/tests/gis_tests/geoapp/test_feeds.py b/tests/gis_tests/geoapp/test_feeds.py index baf2051364..55c19c01c9 100644 --- a/tests/gis_tests/geoapp/test_feeds.py +++ b/tests/gis_tests/geoapp/test_feeds.py @@ -3,14 +3,12 @@ from __future__ import unicode_literals from xml.dom import minidom from django.conf import settings -from django.contrib.gis.geos import HAS_GEOS from django.contrib.sites.models import Site from django.test import ( TestCase, modify_settings, override_settings, skipUnlessDBFeature, ) -if HAS_GEOS: - from .models import City +from .models import City @modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'}) diff --git a/tests/gis_tests/geoapp/test_functions.py b/tests/gis_tests/geoapp/test_functions.py index edef090c5b..bcd9a1ccc6 100644 --- a/tests/gis_tests/geoapp/test_functions.py +++ b/tests/gis_tests/geoapp/test_functions.py @@ -2,21 +2,17 @@ from __future__ import unicode_literals import re from decimal import Decimal -from unittest import skipUnless from django.contrib.gis.db.models import functions -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geos import ( + LineString, Point, Polygon, fromstr, geos_version_info, +) from django.db import connection from django.test import TestCase, skipUnlessDBFeature from django.utils import six from ..utils import mysql, oracle, postgis, spatialite - -if HAS_GEOS: - from django.contrib.gis.geos import ( - LineString, Point, Polygon, fromstr, geos_version_info, - ) - from .models import Country, City, State, Track +from .models import City, Country, State, Track @skipUnlessDBFeature("gis_enabled") @@ -385,8 +381,9 @@ class GISFunctionsTests(TestCase): ) @skipUnlessDBFeature("has_SymDifference_function") - @skipUnless(HAS_GEOS and geos_version_info()['version'] >= '3.3.0', "GEOS >= 3.3 required") def test_sym_difference(self): + if geos_version_info()['version'] < '3.3.0': + self.skipTest("GEOS >= 3.3 required") geom = Point(5, 23, srid=4326) qs = Country.objects.annotate(sym_difference=functions.SymDifference('mpoly', geom)) for country in qs: diff --git a/tests/gis_tests/geoapp/test_regress.py b/tests/gis_tests/geoapp/test_regress.py index e3f5437991..155126a862 100644 --- a/tests/gis_tests/geoapp/test_regress.py +++ b/tests/gis_tests/geoapp/test_regress.py @@ -3,16 +3,13 @@ from __future__ import unicode_literals from datetime import datetime -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.db.models import Extent from django.contrib.gis.shortcuts import render_to_kmz from django.db.models import Count, Min from django.test import TestCase, skipUnlessDBFeature from ..utils import no_oracle - -if HAS_GEOS: - from django.contrib.gis.db.models import Extent - from .models import City, PennsylvaniaCity, State, Truth +from .models import City, PennsylvaniaCity, State, Truth @skipUnlessDBFeature("gis_enabled") diff --git a/tests/gis_tests/geoapp/test_serializers.py b/tests/gis_tests/geoapp/test_serializers.py index 5656ea6f4e..09f837417b 100644 --- a/tests/gis_tests/geoapp/test_serializers.py +++ b/tests/gis_tests/geoapp/test_serializers.py @@ -2,13 +2,11 @@ from __future__ import unicode_literals import json -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geos import LinearRing, Point, Polygon from django.core import serializers from django.test import TestCase, skipUnlessDBFeature -if HAS_GEOS: - from django.contrib.gis.geos import LinearRing, Point, Polygon - from .models import City, MultiFields, PennsylvaniaCity +from .models import City, MultiFields, PennsylvaniaCity @skipUnlessDBFeature("gis_enabled") diff --git a/tests/gis_tests/geoapp/test_sitemaps.py b/tests/gis_tests/geoapp/test_sitemaps.py index 416166b198..4bd8b8744e 100644 --- a/tests/gis_tests/geoapp/test_sitemaps.py +++ b/tests/gis_tests/geoapp/test_sitemaps.py @@ -5,7 +5,6 @@ from io import BytesIO from xml.dom import minidom from django.conf import settings -from django.contrib.gis.geos import HAS_GEOS from django.contrib.sites.models import Site from django.test import ( TestCase, ignore_warnings, modify_settings, override_settings, @@ -13,8 +12,7 @@ from django.test import ( ) from django.utils.deprecation import RemovedInDjango20Warning -if HAS_GEOS: - from .models import City, Country +from .models import City, Country @modify_settings(INSTALLED_APPS={'append': ['django.contrib.sites', 'django.contrib.sitemaps']}) diff --git a/tests/gis_tests/geoapp/tests.py b/tests/gis_tests/geoapp/tests.py index 6ea20c0011..fe681f706d 100644 --- a/tests/gis_tests/geoapp/tests.py +++ b/tests/gis_tests/geoapp/tests.py @@ -4,7 +4,11 @@ import re import tempfile from django.contrib.gis import gdal -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.db.models import Extent, MakeLine, Union +from django.contrib.gis.geos import ( + GeometryCollection, GEOSGeometry, LinearRing, LineString, Point, Polygon, + fromstr, +) from django.core.management import call_command from django.db import connection from django.test import TestCase, ignore_warnings, skipUnlessDBFeature @@ -14,12 +18,10 @@ from django.utils.deprecation import ( ) from ..utils import no_oracle, oracle, postgis, spatialite - -if HAS_GEOS: - from django.contrib.gis.db.models import Extent, MakeLine, Union - from django.contrib.gis.geos import (fromstr, GEOSGeometry, - Point, LineString, LinearRing, Polygon, GeometryCollection) - from .models import Country, City, PennsylvaniaCity, State, Track, NonConcreteModel, Feature, MinusOneSRID +from .models import ( + City, Country, Feature, MinusOneSRID, NonConcreteModel, PennsylvaniaCity, + State, Track, +) def postgis_bug_version(): diff --git a/tests/gis_tests/geogapp/tests.py b/tests/gis_tests/geogapp/tests.py index d4f8ac3843..ec70e98d70 100644 --- a/tests/gis_tests/geogapp/tests.py +++ b/tests/gis_tests/geogapp/tests.py @@ -8,16 +8,13 @@ from unittest import skipUnless from django.contrib.gis.db.models.functions import Area, Distance from django.contrib.gis.gdal import HAS_GDAL -from django.contrib.gis.geos import HAS_GEOS from django.contrib.gis.measure import D from django.test import TestCase, ignore_warnings, skipUnlessDBFeature from django.utils._os import upath from django.utils.deprecation import RemovedInDjango21Warning from ..utils import oracle, postgis - -if HAS_GEOS: - from .models import City, County, Zipcode +from .models import City, County, Zipcode @skipUnlessDBFeature("gis_enabled") diff --git a/tests/gis_tests/geos_tests/test_geos.py b/tests/gis_tests/geos_tests/test_geos.py index 9430a9b4bb..c14a2782dc 100644 --- a/tests/gis_tests/geos_tests/test_geos.py +++ b/tests/gis_tests/geos_tests/test_geos.py @@ -8,8 +8,14 @@ from binascii import a2b_hex, b2a_hex from io import BytesIO from unittest import skipUnless +from django.contrib.gis import gdal from django.contrib.gis.gdal import HAS_GDAL -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geos import ( + HAS_GEOS, GeometryCollection, GEOSException, GEOSGeometry, GEOSIndexError, + LinearRing, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, + Polygon, fromfile, fromstr, geos_version_info, +) +from django.contrib.gis.geos.base import GEOSBase from django.contrib.gis.shortcuts import numpy from django.utils import six from django.utils.encoding import force_bytes @@ -17,14 +23,6 @@ from django.utils.six.moves import range from ..test_data import TestDataMixin -if HAS_GEOS: - from django.contrib.gis.geos import ( - GEOSException, GEOSIndexError, GEOSGeometry, GeometryCollection, Point, - MultiPoint, Polygon, MultiPolygon, LinearRing, LineString, - MultiLineString, fromfile, fromstr, geos_version_info, - ) - from django.contrib.gis.geos.base import gdal, GEOSBase - @skipUnless(HAS_GEOS, "Geos is required.") class GEOSTest(unittest.TestCase, TestDataMixin): diff --git a/tests/gis_tests/geos_tests/test_geos_mutation.py b/tests/gis_tests/geos_tests/test_geos_mutation.py index e88339775b..51b19db9e4 100644 --- a/tests/gis_tests/geos_tests/test_geos_mutation.py +++ b/tests/gis_tests/geos_tests/test_geos_mutation.py @@ -5,18 +5,14 @@ import unittest from unittest import skipUnless -from django.contrib.gis.geos import HAS_GEOS - -if HAS_GEOS: - from django.contrib.gis.geos import ( - fromstr, LinearRing, LineString, MultiPoint, Point, Polygon, - ) - from django.contrib.gis.geos.error import GEOSIndexError +from django.contrib.gis.geos import ( + HAS_GEOS, LinearRing, LineString, MultiPoint, Point, Polygon, fromstr, +) +from django.contrib.gis.geos.error import GEOSIndexError -if HAS_GEOS: - def api_get_distance(x): - return x.distance(Point(-200, -200)) +def api_get_distance(x): + return x.distance(Point(-200, -200)) def api_get_buffer(x): diff --git a/tests/gis_tests/geos_tests/test_io.py b/tests/gis_tests/geos_tests/test_io.py index 95f84bbb4f..59cb54b6b4 100644 --- a/tests/gis_tests/geos_tests/test_io.py +++ b/tests/gis_tests/geos_tests/test_io.py @@ -4,14 +4,11 @@ import binascii import unittest from unittest import skipUnless -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geos import ( + HAS_GEOS, GEOSGeometry, WKBReader, WKBWriter, WKTReader, WKTWriter, +) from django.utils.six import memoryview -if HAS_GEOS: - from django.contrib.gis.geos import ( - GEOSGeometry, WKTReader, WKTWriter, WKBReader, WKBWriter, - ) - @skipUnless(HAS_GEOS, "Geos is required.") class GEOSIOTest(unittest.TestCase): diff --git a/tests/gis_tests/relatedapp/tests.py b/tests/gis_tests/relatedapp/tests.py index f8e0e26105..5422dd48a7 100644 --- a/tests/gis_tests/relatedapp/tests.py +++ b/tests/gis_tests/relatedapp/tests.py @@ -1,6 +1,8 @@ from __future__ import unicode_literals -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.db.models import F, Collect, Count, Extent, Union +from django.contrib.gis.geometry.backend import Geometry +from django.contrib.gis.geos import GEOSGeometry, MultiPoint, Point from django.db import connection from django.test import TestCase, ignore_warnings, skipUnlessDBFeature from django.test.utils import override_settings @@ -8,13 +10,9 @@ from django.utils import timezone from django.utils.deprecation import RemovedInDjango20Warning from ..utils import no_oracle - -if HAS_GEOS: - from django.contrib.gis.db.models import Collect, Count, Extent, F, Union - from django.contrib.gis.geometry.backend import Geometry - from django.contrib.gis.geos import GEOSGeometry, Point, MultiPoint - - from .models import City, Location, DirectoryEntry, Parcel, Book, Author, Article, Event +from .models import ( + Article, Author, Book, City, DirectoryEntry, Event, Location, Parcel, +) @skipUnlessDBFeature("gis_enabled") diff --git a/tests/gis_tests/test_geoforms.py b/tests/gis_tests/test_geoforms.py index a7388d0353..836c720c03 100644 --- a/tests/gis_tests/test_geoforms.py +++ b/tests/gis_tests/test_geoforms.py @@ -1,16 +1,13 @@ from unittest import skipUnless +from django.contrib.gis import forms from django.contrib.gis.gdal import HAS_GDAL -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geos import GEOSGeometry from django.forms import ValidationError from django.test import SimpleTestCase, skipUnlessDBFeature from django.utils import six from django.utils.html import escape -if HAS_GEOS and HAS_GDAL: - from django.contrib.gis import forms - from django.contrib.gis.geos import GEOSGeometry - @skipUnless(HAS_GDAL, "GeometryFieldTest needs GDAL support") @skipUnlessDBFeature("gis_enabled") diff --git a/tests/gis_tests/test_geoip.py b/tests/gis_tests/test_geoip.py index 91a84d42af..57647314b3 100644 --- a/tests/gis_tests/test_geoip.py +++ b/tests/gis_tests/test_geoip.py @@ -7,15 +7,12 @@ from unittest import skipUnless from django.conf import settings from django.contrib.gis.geoip import HAS_GEOIP -from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geos import HAS_GEOS, GEOSGeometry from django.utils import six if HAS_GEOIP: from django.contrib.gis.geoip import GeoIP, GeoIPException -if HAS_GEOS: - from django.contrib.gis.geos import GEOSGeometry - # Note: Requires use of both the GeoIP country and city datasets. # The GEOIP_DATA path should be the only setting set (the directory