Navigation Menu

Skip to content

Commit

Permalink
Reorganized XMLSchema instatiation.
Browse files Browse the repository at this point in the history
* Only instantiate an XMLSchema object for the official SLD spec
when validating a document.
* Store a local copy of the official SLD spec as a local cache.
  • Loading branch information
David Zwarg committed Aug 31, 2012
1 parent a255e77 commit 39011b3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -3,7 +3,7 @@
License
=======
Copyright 2011 David Zwarg <dzwarg@azavea.com>
Copyright 2011-2012 David Zwarg <dzwarg@azavea.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,7 +46,7 @@ def finalize_options(self):

setup(
name = "python-sld",
version = "1.0.8",
version = "1.0.9",
author = "David Zwarg",
author_email = "dzwarg@azavea.com",
description = ("A simple python library that enables dynamic SLD creation and manipulation."),
Expand Down
46 changes: 31 additions & 15 deletions sld/__init__.py
Expand Up @@ -11,7 +11,7 @@
License
=======
Copyright 2011 David Zwarg <U{dzwarg@azavea.com}>
Copyright 2011-2012 David Zwarg <U{dzwarg@azavea.com}>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -27,9 +27,9 @@
@author: David Zwarg
@contact: dzwarg@azavea.com
@copyright: 2011, Azavea
@copyright: 2011-2012, Azavea
@license: Apache 2.0
@version: 1.0.8
@version: 1.0.9
@newfield prop: Property, Properties
"""
from lxml.etree import parse, Element, XMLSchema, XMLSyntaxError, tostring
Expand Down Expand Up @@ -1382,7 +1382,7 @@ class StyledLayerDescriptor(SLDNode):
"""

_cached_schema = None
"""A cached schema document, to prevent multiple requests from occurring."""
"""A cached schema document, to prevent repeated web requests for the schema document."""

def __init__(self, sld_file=None):
"""
Expand All @@ -1399,32 +1399,45 @@ def __init__(self, sld_file=None):
logging.debug('Storing new schema into cache.')

localschema = NamedTemporaryFile(delete=False)
schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd'
resp = urllib2.urlopen(schema_url)
localschema.write(resp.read())
resp.close()

localschema_backup_path = './StyledLayerDescriptor-backup.xsd'
try:
logging.debug('Cache hit for backup schema document.')
localschema_backup = open(localschema_backup_path, 'r')
except IOError:
logging.debug('Cache miss for backup schema document.')
localschema_backup = open(localschema_backup_path, 'w')

schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd'
resp = urllib2.urlopen(schema_url)
localschema_backup.write(resp.read())
resp.close()
localschema_backup.close()
localschema_backup = open(localschema_backup_path, 'r')

localschema.write(localschema_backup.read())
localschema.seek(0)
localschema_backup.close()

theschema = parse(localschema)
self._schemadoc = parse(localschema)
localschema.close()

StyledLayerDescriptor._cached_schema = localschema.name
else:
logging.debug('Fetching schema from cache.')

localschema = open(StyledLayerDescriptor._cached_schema, 'r')
theschema = parse(localschema)
self._schemadoc = parse(localschema)
localschema.close()

self._schema = XMLSchema(theschema)

if not sld_file is None:
self._node = parse(sld_file)

self._schema = XMLSchema(self._schemadoc)
if not self._schema.validate(self._node):
logging.warn('SLD File "%s" does not validate against the SLD schema.', sld_file)
else:
self._node = Element("{%s}StyledLayerDescriptor" % SLDNode._nsmap['sld'], version="1.0.0", nsmap=SLDNode._nsmap)
self._schema = None

setattr(self.__class__, 'NamedLayer', SLDNode.makeproperty('sld', cls=NamedLayer,
docstring="The named layer of the SLD."))
Expand Down Expand Up @@ -1469,10 +1482,13 @@ def validate(self):
"""
self.normalize()

if self._node is None or self._schema is None:
logging.debug('The node or schema is empty, and cannot be validated.')
if self._node is None:
logging.debug('The node is empty, and cannot be validated.')
return False

if self._schema is None:
self._schema = XMLSchema(self._schemadoc)

is_valid = self._schema.validate(self._node)

for msg in self._schema.error_log:
Expand Down
6 changes: 3 additions & 3 deletions sld/run_tests.py
Expand Up @@ -4,7 +4,7 @@
License
=======
Copyright 2011 David Zwarg <U{dzwarg@azavea.com}>
Copyright 2011-2012 David Zwarg <U{dzwarg@azavea.com}>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -20,9 +20,9 @@
@author: David Zwarg
@contact: dzwarg@azavea.com
@copyright: 2011, Azavea
@copyright: 2011-2012, Azavea
@license: Apache 2.0
@version: 1.0.7
@version: 1.0.9
"""
import unittest, sys, logging
from optparse import OptionParser
Expand Down
6 changes: 3 additions & 3 deletions sld/test/__init__.py
Expand Up @@ -3,7 +3,7 @@
License
=======
Copyright 2011 David Zwarg <U{dzwarg@azavea.com}>
Copyright 2011-2012 David Zwarg <U{dzwarg@azavea.com}>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,9 @@
@author: David Zwarg
@contact: dzwarg@azavea.com
@copyright: 2011, Azavea
@copyright: 2011-2012, Azavea
@license: Apache 2.0
@version: 1.0.8
@version: 1.0.9
"""
from sld import *
import unittest, copy
Expand Down

0 comments on commit 39011b3

Please sign in to comment.