Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reorganized XMLSchema instatiation.

* 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...
commit 39011b3784d90ccc6c9b01345f88f325bc180856 1 parent a255e77
David Zwarg authored
View
4 setup.py
@@ -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.
@@ -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."),
View
46 sld/__init__.py
@@ -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.
@@ -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
@@ -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):
"""
@@ -1399,13 +1399,27 @@ 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
@@ -1413,18 +1427,17 @@ def __init__(self, sld_file=None):
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."))
@@ -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:
View
6 sld/run_tests.py
@@ -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.
@@ -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
View
6 sld/test/__init__.py
@@ -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.
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.