Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gnrfan committed Jul 6, 2014
0 parents commit 6a10e03
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Generic files
*~
*.lock
*.out

# OS specific files
.DS_Store
# Vim
.*.swp
.*.swo

# Emacs
.emacs-project
\#.#
*/.#*
\#*
.#*
*~
*.idea

# Komodo
*.komodoproject

# Python-specific
*.log
*.py[co]

# Virtualenv-specific
env/

# Django-specific
local_settings.py
doccupations/media/!.ignoreme
doccupations/static
#static/admin_tools
*.pot

# Python packages-specific
*.egg
*.egg-info
dist
build
eggs
parts
var
sdist
develop-eggs
.installed.cfg

# pip-specific
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

# SQLite-specific
data/!.turd
*.sqlite
*.sqlite3

#Redis
*.rdb

#Coverage byproducts
.coverage
htmlcov
cover
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Antonio Ognio <antonio@ognio.com>
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2012, Antonio Ognio.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1) Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2) Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3) Neither the name of django-sequence-field nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7 changes: 7 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# file GENERATED by distutils, do NOT edit
LICENSE
AUTHORS
README.md
setup.py
easyxsd/__init__.py
easyxsd/utils.py
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include LICENSE
include AUTHORS
include README.md
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
EasyXSD
=======

Easy XML Schema Definition (XSD) validation of XML documents based on ```lxml```.

Usage:

```python
>>> from easyxsd import *
>>> xsd = xsd_from_file('/path/to/files/definitions.xsd')
>>> xml = xml_from_file('/path/to/files/valid-example.xml')
>>> xsd.validate(xml)
True
>>> xml = xml_from_file('/path/to/files/invalid-example.xml')
>>> xsd.validate(xml)
False
```

The ```xml``` and ```xsd``` objects are ```lxml```'s ```lxml.etree._ElementTree```
and ```lxml.etree.XMLSchema``` objects respectively.

More information on the available API:

**validate(xml, xsd)**
Receives an lxml.etree._ElementTree object as first parameter
and an lxml.etree.XMLSchema object as second parameter and
returns True or False respectively as the XSD validation of the
XML succeeds or fails.

**validate_from_files(xmlfilepath, xsdfilepath)**
Receives a string with a file path to a valid XML document
as first parameter and another string with a file path to a valid
XSD document as second parameter and validates the first according
to the latter returning True or False respectively as the validation
succeeds or fails.

**validate_from_strings(xmlstr, xsdstr)**
Receives a string containing a valid XML document as first parameter
and another string containing a valid XSD document as second parameter
and validates the first according to the latter returning True or False
respectively as the validation succeeds or fails.

**validate_with_errors(xml, xsd)**
Returns a tuple with a boolean product of the XSD validation as
the first element and the error log object as the second element.

**validate_xml_string_from_xsd_file(xmlstr, xsdfilepath)**
Validates a string containing an XML document as the first parameter
with an XSD document contained in the file path passed as the
second parameter.

**xml_from_file(filepath)**
Returns an lxml.etree._ElementTree object from a file
containing a valid XML document.

**xml_from_string(xmlstr)**
Returns an lxml.etree._ElementTree object from a string
containing a valid XML document.

**xsd_error_as_simple_string(error)**
Returns a string based on an XSD error object with the format
LINE:COLUMN:LEVEL_NAME:DOMAIN_NAME:TYPE_NAME:MESSAGE.

**xsd_error_log_as_simple_strings(error_log)**
Returns a list of strings representing all the errors of an XSD
error log object.

(c) 2014 - Antonio Ognio <antonio@ognio.com>
7 changes: 7 additions & 0 deletions easyxsd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
try:
__version__ = \
__import__('pkg_resources').get_distribution('easyxsd').version
except Exception:
__version__ = 'unknown'

from easyxsd.utils import *
103 changes: 103 additions & 0 deletions easyxsd/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from lxml import etree

def xml_from_string(xmlstr):
"""
Returns an lxml.etree._ElementTree object from a string
containing a valid XML document.
"""
return etree.XML(xsdstr.strip())

def xml_from_file(filepath):
"""
Returns an lxml.etree._ElementTree object from a file
containing a valid XML document.
"""
return etree.parse(filepath)

def xsd_from_string(xsdstr):
"""
Returns an lxml.etree.XMLSchema object from a string
containing a valid XML document.
"""
xml = etree.XML(xsdstr.strip())
return etree.XMLSchema(xml)

def xsd_from_file(filepath):
"""
Returns an lxml.etree.XMLSchema object from a file
containing a valid XML document.
"""
xml = etree.parse(filepath)
return etree.XMLSchema(xml)

def validate(xml, xsd):
"""
Receives an lxml.etree._ElementTree object as first parameter
and an lxml.etree.XMLSchema object as second parameter and
returns True or False respectively as the XSD validation of the
XML succeeds or fails.
"""
return xsd.validate(xml)

def validate_from_strings(xmlstr, xsdstr):
"""
Receives a string containing a valid XML document as first parameter
and another string containing a valid XSD document as second parameter
and validates the first according to the latter returning True or False
respectively as the validation succeeds or fails.
"""
xml = xml_from_string(xmlstr)
xsd = xsd_from_string(xsdstr)
return validate(xml, xsd)

def validate_from_files(xmlfilepath, xsdfilepath):
"""
Receives a string with a file path to a valid XML document
as first parameter and another string with a file path to a valid
XSD document as second parameter and validates the first according
to the latter returning True or False respectively as the validation
succeeds or fails.
"""
xml = xml_from_file(xmlfilepath)
xsd = xsd_from_file(xsdfilepath)
return validate(xml, xsd)

def validate_xml_string_from_xsd_file(xmlstr, xsdfilepath):
"""
Validates a string containing an XML document as the first parameter
with an XSD document contained in the file path passed as the
second parameter.
"""
xml = xml_from_string(xmlstr)
xsd = xsd_from_file(xsdfilepath)
return validate(xml, xsd)

def validate_with_errors(xml, xsd):
"""
Returns a tuple with a boolean product of the XSD validation as
the first element and the error log object as the second element.
"""
validation = xsd.validate(xml)
return (validation, xsd.error_log, )

def xsd_error_as_simple_string(error):
"""
Returns a string based on an XSD error object with the format
LINE:COLUMN:LEVEL_NAME:DOMAIN_NAME:TYPE_NAME:MESSAGE.
"""
parts = [
error.line,
error.column,
error.level_name,
error.domain_name,
error.type_name,
error.message
]
return ':'.join([str(item) for item in parts])

def xsd_error_log_as_simple_strings(error_log):
"""
Returns a list of strings representing all the errors of an XSD
error log object.
"""
return [xsd_error_as_simple_string(e) for e in error_log]
29 changes: 29 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from setuptools import setup

description = 'Easy XML Schema Definition (XSD) validation of XML documents'

try:
with open('README.md') as f:
long_description = f.read()
except IOError:
long_description = description

setup(
name = 'easyxsd',
version = '0.1',
description = description,
author = 'Antonio Ognio',
author_email = 'antonio@ognio.com',
url = 'https://github.com/gnrfan/python-easyxsd',
long_description = long_description,
packages = ['easyxsd'],
install_requires = ['lxml >= 3.3.5'],
classifiers = [
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
],
)

0 comments on commit 6a10e03

Please sign in to comment.