Next / Previous / Contents / TCC Help System / NM Tech homepage

15.3. rnc_validate.py: Prologue

The actual code for this module starts here, with the customary documentation string, which points back to this documentation. The block also contains Cleanroom intended function notation for the interface described above.

rnc_validate.py
'''rnc_validate.py: An XML validator for Relax NG schemas.
  For documentation, see:
    http://www.nmt.edu/tcc/help/pubs/pylxml/

  Exports:
    class RelaxException(Exception)
    class RelaxValidator
      RelaxValidator(schemaPath):
        [ schemaPath is a string ->
            if schemaPath names a readable, valid .rng schema ->
              return a RelaxValidator that validates against that schema
            else if (schemaPath, with .rnc appended if there is no
            extension, names a readable, valid .rnc schema) ->
              if the corresponding .rng schema is readable, valid, and
              newer than the .rnc schema ->
                return a RelaxValidator that validates against the
                .rng schema
              else if (we have write access to the corresponding .rng
              schema) and (trang is locally installed) ->
                corresponding .rng schema  :=  trang's translation of
                    the .rnc schema into .rng
                return a RelaxValidator that validates against the
                translated schema
              else -> raise ValueError ]
      .validate(tree):
        [ tree is an etree.ElementTree ->
            if tree validates against self -> I
            else -> raise RelaxException ]
'''

Next come module imports. We need the standard Python os and stat modules to check file modification times.

rnc_validate.py
# - - - - -   I m p o r t s

import os
import stat

We import the lxml module's etree implementation but call it et.

rnc_validate.py
from lxml import etree as et

The pexpect module is a third-party library for spawning and controlling subprocesses. We need it to run trang.

rnc_validate.py
import pexpect

We'll need two constants for the characteristic file suffixes.

rnc_validate.py
# - - - - -   M a n i f e s t   c o n s t a n t s

RNC_SUFFIX = '.rnc'
RNG_SUFFIX = '.rng'