Пример #1
0
    def execute_actions(self, clear=True, testing=False):
        """Execute the configuration actions.

        This calls the action callables after resolving conflicts.
        """
        try:
            for action in resolveConflicts(self.actions):
                callable = action['callable']
                if callable is None:
                    continue
                args = action['args']
                kw = action['kw']
                info = action['info']
                try:
                    callable(*args, **kw)
                except (KeyboardInterrupt, SystemExit):  #pragma NO COVER
                    raise
                except:
                    if testing:
                        raise
                    t, v, tb = sys.exc_info()
                    try:
                        reraise(ConfigurationExecutionError(t, v, info), None,
                                tb)
                    finally:
                        del t, v, tb

        finally:
            if clear:
                del self.actions[:]
Пример #2
0
    def endElementNS(self, name, qname):
        # If ignore_depth is set, this element will be ignored, even
        # if this this decrements ignore_depth to 0.
        if self.ignore_depth:
            self.ignore_depth -= 1
            return

        info = self.context.getInfo()
        info.end(
            self.locator.getLineNumber(),
            self.locator.getColumnNumber(),
        )

        try:
            self.context.end()
        except (KeyboardInterrupt, SystemExit):  #pragma NO COVER
            raise
        except:
            if self.testing:
                raise
            reraise(
                ZopeXMLConfigurationError(info,
                                          sys.exc_info()[0],
                                          sys.exc_info()[1]), None,
                sys.exc_info()[2])
Пример #3
0
    def _handle_exception(self, ex, info):
        if self.testing:
            raise
        if isinstance(ex, ConfigurationError):
            ex.add_details(repr(info))
            raise

        exc = ZopeXMLConfigurationError(info, ex)
        reraise(exc, None, sys.exc_info()[2])
    def _handle_exception(self, ex, info):
        if self.testing:
            raise
        if isinstance(ex, ConfigurationError):
            ex.add_details(repr(info))
            raise

        exc = ZopeXMLConfigurationError(info, ex)
        reraise(exc, None, sys.exc_info()[2])
Пример #5
0
def toargs(context, schema, data):
    """Marshal data to an argument dictionary using a schema

    Names that are python keywords have an underscore added as a
    suffix in the schema and in the argument list, but are used
    without the underscore in the data.

    The fields in the schema must all implement IFromUnicode.

    All of the items in the data must have corresponding fields in the
    schema unless the schema has a true tagged value named
    'keyword_arguments'.
    """
    data = dict(data)
    args = {}
    for name, field in schema.namesAndDescriptions(True):
        field = field.bind(context)
        n = name
        if n.endswith('_') and iskeyword(n[:-1]):
            n = n[:-1]

        s = data.get(n, data)
        if s is not data:
            s = text_type(s)
            del data[n]

            try:
                args[str(name)] = field.fromUnicode(s)
            except ValidationError as v:
                reraise(ConfigurationError("Invalid value for", n, str(v)),
                        None,
                        sys.exc_info()[2])
        elif field.required:
            # if the default is valid, we can use that:
            default = field.default
            try:
                field.validate(default)
            except ValidationError:
                raise ConfigurationError("Missing parameter:", n)
            args[str(name)] = default

    if data:
        # we had data left over
        try:
            keyword_arguments = schema.getTaggedValue('keyword_arguments')
        except KeyError:
            keyword_arguments = False
        if not keyword_arguments:
            raise ConfigurationError("Unrecognized parameters:", *data)

        for name in data:
            args[str(name)] = data[name]

    return args
Пример #6
0
def processxmlfile(file, context, testing=False):
    """Process a configuration file

    See examples in tests/text_xmlconfig.py
    """
    src = InputSource(getattr(file, 'name', '<string>'))
    src.setByteStream(file)
    parser = make_parser()
    parser.setContentHandler(ConfigurationHandler(context, testing=testing))
    parser.setFeature(feature_namespaces, True)
    try:
        parser.parse(src)
    except SAXParseException:
        reraise(ZopeSAXParseException(sys.exc_info()[1]),
                None, sys.exc_info()[2])
Пример #7
0
def processxmlfile(file, context, testing=False):
    """Process a configuration file

    See examples in tests/test_xmlconfig.py
    """
    src = InputSource(getattr(file, 'name', '<string>'))
    src.setByteStream(file)
    parser = make_parser()
    parser.setContentHandler(ConfigurationHandler(context, testing=testing))
    parser.setFeature(feature_namespaces, True)
    try:
        parser.parse(src)
    except SAXParseException:
        reraise(ZopeSAXParseException(sys.exc_info()[1]), None,
                sys.exc_info()[2])
Пример #8
0
    def startElementNS(self, name, qname, attrs):
        if self.ignore_depth:
            self.ignore_depth += 1
            return

        data = {}
        for (ns, aname), value in attrs.items():
            # NB: even though on CPython, 'ns' will be ``None`` always,
            # do not change the below to "if ns is None" because Jython's
            # sax parser generates attrs that have empty strings for
            # the namepace instead of ``None``.
            if not ns:
                aname = str(aname)
                data[aname] = value
            if (ns, aname) == ZCML_CONDITION:
                # need to process the expression to determine if we
                # use this element and it's descendents
                use = self.evaluateCondition(value)
                if not use:
                    self.ignore_depth = 1
                    return

        info = ParserInfo(
            self.locator.getSystemId(),
            self.locator.getLineNumber(),
            self.locator.getColumnNumber(),
        )

        try:
            self.context.begin(name, data, info)
        except (KeyboardInterrupt, SystemExit):  #pragma NO COVER
            raise
        except:
            if self.testing:
                raise
            reraise(
                ZopeXMLConfigurationError(info,
                                          sys.exc_info()[0],
                                          sys.exc_info()[1]), None,
                sys.exc_info()[2])

        self.context.setInfo(info)
Пример #9
0
    def startElementNS(self, name, qname, attrs):
        if self.ignore_depth:
            self.ignore_depth += 1
            return

        data = {}
        for (ns, aname), value in attrs.items():
            # NB: even though on CPython, 'ns' will be ``None`` always,
            # do not change the below to "if ns is None" because Jython's
            # sax parser generates attrs that have empty strings for
            # the namepace instead of ``None``.
            if not ns:
                aname = str(aname)
                data[aname] = value
            if (ns, aname) == ZCML_CONDITION:
                # need to process the expression to determine if we
                # use this element and it's descendents
                use = self.evaluateCondition(value)
                if not use:
                    self.ignore_depth = 1
                    return

        info = ParserInfo(
            self.locator.getSystemId(),
            self.locator.getLineNumber(),
            self.locator.getColumnNumber(),
            )

        try:
            self.context.begin(name, data, info)
        except (KeyboardInterrupt, SystemExit): #pragma NO COVER
            raise
        except:
            if self.testing:
                raise
            reraise(ZopeXMLConfigurationError(info,
                                              sys.exc_info()[0],
                                              sys.exc_info()[1]),
                    None, sys.exc_info()[2])

        self.context.setInfo(info)
Пример #10
0
    def endElementNS(self, name, qname):
        # If ignore_depth is set, this element will be ignored, even
        # if this this decrements ignore_depth to 0.
        if self.ignore_depth:
            self.ignore_depth -= 1
            return

        info = self.context.getInfo()
        info.end(
            self.locator.getLineNumber(),
            self.locator.getColumnNumber(),
            )

        try:
            self.context.end()
        except (KeyboardInterrupt, SystemExit): #pragma NO COVER
            raise
        except:
            if self.testing:
                raise
            reraise(ZopeXMLConfigurationError(info,
                                              sys.exc_info()[0],
                                              sys.exc_info()[1]),
                    None, sys.exc_info()[2])