PageRenderTime 72ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 22ms app.codeStats 1ms

/django/contrib/gis/geometry/test_data.py

https://code.google.com/p/mango-py/
Python | 105 lines | 54 code | 23 blank | 28 comment | 15 complexity | c4fca32d21e9fc20079fd1b867cd5819 MD5 | raw file
  1"""
  2This module has the mock object definitions used to hold reference geometry
  3for the GEOS and GDAL tests.
  4"""
  5import gzip
  6import os
  7
  8from django.contrib import gis
  9from django.utils import simplejson
 10
 11
 12# This global used to store reference geometry data.
 13GEOMETRIES = None
 14
 15# Path where reference test data is located.
 16TEST_DATA = os.path.join(os.path.dirname(gis.__file__), 'tests', 'data')
 17
 18
 19def tuplize(seq):
 20    "Turn all nested sequences to tuples in given sequence."
 21    if isinstance(seq, (list, tuple)):
 22        return tuple([tuplize(i) for i in seq])
 23    return seq
 24
 25
 26def strconvert(d):
 27    "Converts all keys in dictionary to str type."
 28    return dict([(str(k), v) for k, v in d.iteritems()])
 29
 30
 31def get_ds_file(name, ext):
 32    return os.path.join(TEST_DATA,
 33                        name,
 34                        name + '.%s' % ext
 35                        )
 36
 37
 38class TestObj(object):
 39    """
 40    Base testing object, turns keyword args into attributes.
 41    """
 42    def __init__(self, **kwargs):
 43        for key, value in kwargs.items():
 44            setattr(self, key, value)
 45
 46
 47class TestDS(TestObj):
 48    """
 49    Object for testing GDAL data sources.
 50    """
 51    def __init__(self, name, **kwargs):
 52        # Shapefile is default extension, unless specified otherwise.
 53        ext = kwargs.pop('ext', 'shp')
 54        self.ds = get_ds_file(name, ext)
 55        super(TestDS, self).__init__(**kwargs)
 56
 57
 58class TestGeom(TestObj):
 59    """
 60    Testing object used for wrapping reference geometry data
 61    in GEOS/GDAL tests.
 62    """
 63    def __init__(self, **kwargs):
 64        # Converting lists to tuples of certain keyword args
 65        # so coordinate test cases will match (JSON has no
 66        # concept of tuple).
 67        coords = kwargs.pop('coords', None)
 68        if coords:
 69            self.coords = tuplize(coords)
 70
 71        centroid = kwargs.pop('centroid', None)
 72        if centroid:
 73            self.centroid = tuple(centroid)
 74
 75        ext_ring_cs = kwargs.pop('ext_ring_cs', None)
 76        if ext_ring_cs:
 77            ext_ring_cs = tuplize(ext_ring_cs)
 78        self.ext_ring_cs = ext_ring_cs
 79
 80        super(TestGeom, self).__init__(**kwargs)
 81
 82
 83class TestGeomSet(object):
 84    """
 85    Each attribute of this object is a list of `TestGeom` instances.
 86    """
 87    def __init__(self, **kwargs):
 88        for key, value in kwargs.items():
 89            setattr(self, key, [TestGeom(**strconvert(kw)) for kw in value])
 90
 91
 92class TestDataMixin(object):
 93    """
 94    Mixin used for GEOS/GDAL test cases that defines a `geometries`
 95    property, which returns and/or loads the reference geometry data.
 96    """
 97    @property
 98    def geometries(self):
 99        global GEOMETRIES
100        if GEOMETRIES is None:
101            # Load up the test geometry data from fixture into global.
102            gzf = gzip.GzipFile(os.path.join(TEST_DATA, 'geometries.json.gz'))
103            geometries = simplejson.loads(gzf.read())
104            GEOMETRIES = TestGeomSet(**strconvert(geometries))
105        return GEOMETRIES