Source code for publiforge.lib.processor.validator

# $Id$
"""This module validates XML files against their Relax NG."""

import re
from os import walk
from os.path import join, exists, dirname, basename, isdir
from ConfigParser import ConfigParser

from ...i18n import _
from ...xml import load
from ...processor import load_relaxngs

# =============================================================================
[docs]class Processor(object): """Main class for Validator processor.""" # ------------------------------------------------------------------------- def __init__(self, build): """Constructor method. :param build: (:class:``) Main Build object. """ # Attributes = build # Configuration name = join(build.path, 'Processor', 'validator.ini') if not exists(name): build.stopped(_('File "validator.ini" is missing.')) return config = ConfigParser({'here': dirname(name)}) config.optionxform = str # Relax NG self._relaxngs = load_relaxngs(, config) if len(self._relaxngs) == 0: build.stopped(_('No Relax NG.')) return # File selection self._file_regex = config.has_option('Input', 'file_regex') \ and re.compile(config.get('Input', 'file_regex')) \ or re.compile(r'\.xml$') self._content_regex = config.has_option('Input', 'content_regex') \ and re.compile(config.get('Input', 'content_regex')) # -------------------------------------------------------------------------
[docs] def start(self): """Start the processor.""" if return # Get file list file_list = [] for name in['files']: fullname = join(, name) if not exists(fullname):'Unknown file "%s"' % name, 'a_warn') continue if isdir(fullname): for path, name, files in walk(fullname): for name in files: if not continue name = join(path, name) name = isinstance(name, str) \ and unicode(name.decode('utf8')) or name file_list.append(name) elif file_list.append(fullname) if not len(file_list):'nothing to do!'), 'a_error') return # Validate for count, name in enumerate(file_list): percent = 90 * (count + 1) / (len(file_list) + 1) self._validate(percent, name)
# ------------------------------------------------------------------------- def _validate(self, percent, filename): """Validate one XML file. :param percent: (integer) Percent of progress. :param filename: (string) Absolute path of the file to validate. """ with open(filename, 'r') as hdl: data = if self._content_regex and not return u'%s ............' % basename(filename), 'a_build', percent) data = load(filename, self._relaxngs, data) if not isinstance(data, basestring):'${f} is valid', {'f': basename(filename)})) return if 'values' not in['values'] = []['values'].append(data)