The first step is to remove the file suffix so we know
which kind of schema we're using, and then derive the
full path names of both the .rnc and .rng
(potential) versions of the schema.
# - - - R e l a x V a l i d a t o r . _ _ i n i t _ _
def __init__ ( self, schemaPath ):
'''Constructor.
'''
#-- 1 --
# [ basePath := schemaPath without its extension
# suffix := schemaPath's extension, defaulting to RNC_SUFFIX
# cName := (schemaPath without its extension)+RNC_SUFFIX
# gName := (schemaPath without its extension)+RNG_SUFFIX ]
basePath, suffix = os.path.splitext(schemaPath)
if suffix == '':
suffix = RNC_SUFFIX
gName = basePath + RNG_SUFFIX
cName = basePath + RNC_SUFFIX
If the desired schema is in .rng form, we're ready
to proceed. If it is an .rnc schema, though, we
need an .rng version that is up to date. See Section 14.8, “RelaxValidator.__makeRNG(): Find or
create an .rng file”. If the file
suffix isn't either, that's an error.
#-- 2 --
# [ if suffix == RNG_SUFFIX ->
# I
# else if (file cName is readable) and (gName names a
# readable file that is newer than cName) ->
# I
# else if (cName names a readable, valid RNC file) and
# (we have write access to path gName) and
# (trang is locally installed) ->
# file gName := trang's translation of file cName into RNG
# else -> raise ValueError ]
if suffix == RNC_SUFFIX:
self.__makeRNG ( cName, gName )
elif suffix != RNG_SUFFIX:
raise ValueError("File suffix not %s or %s: %s" %
(RNC_SUFFIX, RNG_SUFFIX, suffix) )
At this point we have a known good .rng version of
the schema. Read that, make it into a RelaxNG instance (assuming it is valid Relax
NG), and store it in self.__schema.
#-- 3 --
# if gName names a readable, valid XML file ->
# doc := an et.ElementTree representing that file
# else -> raise ValueError ]
try:
doc = et.parse ( gName )
except IOError, details:
raise ValueError("Can't open the schema file '%s': %s" %
(gName, str(details)) )
#-- 4 --
# [ if doc is a valid RNG schema ->
# self.__schema := an et.RelaxNG instance that represents
# doc
# else -> raise ValueError ]
try:
self.__schema = et.RelaxNG ( doc )
except et.RelaxNGParseError, details:
raise ValueError("Schema file '%s' is not valid: %s" %
(gName, str(details)) )