This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

Classix

Introduction

Classix is a thin layer over lxml’s custom element classes functionality that lets you configure them declaratively. It uses Martian to accomplish this effect, but to you this should be an implementation detail.

To parse an XML document you need a parser. An XMLParser` can be configured with custom element classes that will be associated to the right elements in the XML document after parsing. This way you can enrich the content model of your XML document.

The approach

First we need to do the configuration for this package. This only needs to happen once for this package. First, we need to set up a GrokkerRegistry.

>>> import martian
>>> reg = martian.GrokkerRegistry()

Now we can grok the grokkers in this package:

>>> from classix import meta
>>> reg.grok('meta', meta)
True

Now we can start using classix. You need to create an XMLParser class that you will associate your custom element classes with:

>>> import classix
>>> class MyParser(classix.XMLParser):
...     pass

Let’s grok the parser:

>>> reg.grok('MyParser', MyParser)
True

Now you can set up classes to associate with particular elements in particular namespaces, for that parser:

>>> XMLNS = 'http://ns.example.com'
>>> class Test(classix.Element):
...    classix.namespace(XMLNS)
...    classix.parser(MyParser)
...
...    def custom_method(self):
...        return "The custom method"

We also need to grok this:

>>> reg.grok('Test', Test)
True

Now that we have it all set up, we can initialize the parser:

>>> parser = MyParser()

Let’s parse a bit of XML:

>>> xml = '''\
...   <test xmlns="http://ns.example.com" />
...   '''
>>> from lxml import etree
>>> root = etree.XML(xml, parser)
>>> root.custom_method()
'The custom method'

No namespace

Sometimes you want to associate a class with an element in no namespace at all. Do do this, you can set the namepace to None explicitly:

>>> reg = martian.GrokkerRegistry()
>>> reg.grok('meta', meta)
True

>>> class MyParser(classix.XMLParser):
...     pass
>>> reg.grok('MyParser', MyParser)
True
>>> class Test(classix.Element):
...     classix.namespace(None)
...     classix.parser(MyParser)
...     def custom_method(self):
...        return 'The custom method for no namespace'
>>> reg.grok('Test', Test)
True
>>> parser = MyParser()
>>> no_ns_xml = '''\
...   <test />
...   '''
>>> root = etree.XML(no_ns_xml, parser)
>>> root.custom_method()
'The custom method for no namespace'

When supplied with an element with a namespace, the Test class will not be associated with that element:

>>> root = etree.XML(xml, parser)
>>> root.custom_method()
Traceback (most recent call last):
  ...
AttributeError: 'lxml.etree._Element' object has no attribute 'custom_method'

Namespaces are assumed to be None if the classix.namespace directive isn’t used at all:

>>> class MyParser(classix.XMLParser):
...     pass
>>> reg.grok('MyParser', MyParser)
True
>>> class Test(classix.Element):
...     classix.parser(MyParser)
...     def custom_method(self):
...        return 'The custom method for no namespace 2'
>>> reg.grok('Test', Test)
True
>>> parser = MyParser()
>>> no_ns_xml = '''\
...   <test />
...   '''
>>> root = etree.XML(no_ns_xml, parser)
>>> root.custom_method()
'The custom method for no namespace 2'

Namespaces in parser

As a convenience, you can also configure the default namespace in the parser, as a fall-back so you don’t have to specify it in all the element classes:

>>> reg = martian.GrokkerRegistry()
>>> reg.grok('meta', meta)
True

>>> class MyParserWithNamespace(classix.XMLParser):
...    classix.namespace(XMLNS)
>>> reg.grok('MyParserWithNamespace', MyParserWithNamespace)
True

>>> class Test2(classix.Element):
...    classix.parser(MyParserWithNamespace)
...    classix.name('test') # also override name
...    def custom_method(self):
...        return "Another custom method"
>>> reg.grok('Test2', Test2)
True

>>> parser_ns = MyParserWithNamespace()
>>> root = etree.XML(xml, parser_ns)
>>> root.custom_method()
'Another custom method'

Classix changes

0.5 (2008-07-07)

  • Initial public release

Download

Release History

Release History

0.5

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
classix-0.5.tar.gz (3.9 kB) Copy SHA256 Checksum SHA256 Source Jul 7, 2008

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting