~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

Viewing changes to django/contrib/gis/geos/libgeos.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
Tags: 1.5.1-2
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 This module also houses GEOS Pointer utilities, including
7
7
 get_pointer_arr(), and GEOM_PTR.
8
8
"""
 
9
import logging
9
10
import os
10
11
import re
11
 
import sys
12
12
from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER
13
13
from ctypes.util import find_library
 
14
 
14
15
from django.contrib.gis.geos.error import GEOSException
 
16
from django.core.exceptions import ImproperlyConfigured
 
17
 
 
18
logger = logging.getLogger('django.contrib.gis')
15
19
 
16
20
# Custom library path set?
17
21
try:
18
22
    from django.conf import settings
19
23
    lib_path = settings.GEOS_LIBRARY_PATH
20
 
except (AttributeError, EnvironmentError, ImportError):
 
24
except (AttributeError, EnvironmentError,
 
25
        ImportError, ImproperlyConfigured):
21
26
    lib_path = None
22
27
 
23
28
# Setting the appropriate names for the GEOS-C library.
56
61
# Supposed to mimic the GEOS message handler (C below):
57
62
#  typedef void (*GEOSMessageHandler)(const char *fmt, ...);
58
63
NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
59
 
def notice_h(fmt, lst, output_h=sys.stdout):
 
64
def notice_h(fmt, lst):
 
65
    fmt, lst = fmt.decode(), lst.decode()
60
66
    try:
61
67
        warn_msg = fmt % lst
62
68
    except:
63
69
        warn_msg = fmt
64
 
    output_h.write('GEOS_NOTICE: %s\n' % warn_msg)
 
70
    logger.warn('GEOS_NOTICE: %s\n' % warn_msg)
65
71
notice_h = NOTICEFUNC(notice_h)
66
72
 
67
73
ERRORFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
68
 
def error_h(fmt, lst, output_h=sys.stderr):
 
74
def error_h(fmt, lst):
 
75
    fmt, lst = fmt.decode(), lst.decode()
69
76
    try:
70
77
        err_msg = fmt % lst
71
78
    except:
72
79
        err_msg = fmt
73
 
    output_h.write('GEOS_ERROR: %s\n' % err_msg)
 
80
    logger.error('GEOS_ERROR: %s\n' % err_msg)
74
81
error_h = ERRORFUNC(error_h)
75
82
 
76
83
#### GEOS Geometry C data structures, and utility functions. ####
101
108
geos_version.restype = c_char_p
102
109
 
103
110
# Regular expression should be able to parse version strings such as
104
 
# '3.0.0rc4-CAPI-1.3.3', '3.0.0-CAPI-1.4.1' or '3.4.0dev-CAPI-1.8.0'
105
 
version_regex = re.compile(r'^(?P<version>(?P<major>\d+)\.(?P<minor>\d+)\.(?P<subminor>\d+))((rc(?P<release_candidate>\d+))|dev)?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)$')
 
111
# '3.0.0rc4-CAPI-1.3.3', '3.0.0-CAPI-1.4.1', '3.4.0dev-CAPI-1.8.0' or '3.4.0dev-CAPI-1.8.0 r0'
 
112
version_regex = re.compile(
 
113
    r'^(?P<version>(?P<major>\d+)\.(?P<minor>\d+)\.(?P<subminor>\d+))'
 
114
    r'((rc(?P<release_candidate>\d+))|dev)?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)( r\d+)?$'
 
115
)
106
116
def geos_version_info():
107
117
    """
108
118
    Returns a dictionary containing the various version metadata parsed from
110
120
    is a release candidate (and what number release candidate), and the C API
111
121
    version.
112
122
    """
113
 
    ver = geos_version()
 
123
    ver = geos_version().decode()
114
124
    m = version_regex.match(ver)
115
 
    if not m: raise GEOSException('Could not parse version info string "%s"' % ver)
116
 
    return dict((key, m.group(key)) for key in ('version', 'release_candidate', 'capi_version', 'major', 'minor', 'subminor'))
 
125
    if not m:
 
126
        raise GEOSException('Could not parse version info string "%s"' % ver)
 
127
    return dict((key, m.group(key)) for key in (
 
128
        'version', 'release_candidate', 'capi_version', 'major', 'minor', 'subminor'))
117
129
 
118
130
# Version numbers and whether or not prepared geometry support is available.
119
131
_verinfo = geos_version_info()