def testLxmlElementUntail(self):
        lxmlNode = parse(StringIO('<myns:root xmlns:myns="http://myns.org/" xmlns="http://myns.org/"><a>b</a></myns:root>'))
        element = lxmlNode.xpath('/myns:root/myns:a', namespaces={'myns': 'http://myns.org/'})[0]
        self.assertEquals(None, element.tail)
        newElement = lxmlElementUntail(element)
        self.assertTrue(newElement is element)

        lxmlNode = parse(StringIO('<myns:root xmlns:myns="http://myns.org/" xmlns="http://myns.org/"><a><b>c</b>\n\n</a>\n\n\n\n</myns:root>'))
        element = lxmlNode.xpath('/myns:root/myns:a', namespaces={'myns': 'http://myns.org/'})[0]
        self.assertEquals('\n\n\n\n', element.tail)
        newElement = lxmlElementUntail(element)
        self.assertFalse(newElement is element)
        self.assertEquals(None, newElement.tail)
        self.assertEquals('<a xmlns="http://myns.org/"><b>c</b>\n\n</a>', lxmltostring(newElement))
    def handleRequest(self, Body="", **kwargs):
        yield okXml
        if not Body:
            yield self._respond(
                diagnosticUri='info:srw/diagnostic/12/9',
                details='Update request lacks a record in its body.',
                message='Missing mandatory element:  record rejected')
            return

        localLogCollector = dict()
        try:
            try:
                lxmlNode = parse(StringIO(Body))
            except XMLSyntaxError, e:
                self._log(Body, localLogCollector=localLogCollector)
                raise
            updateRequest = xpathFirst(lxmlNode,
                                       '/*[local-name()="updateRequest"]')
            recordId = xpathFirst(updateRequest, 'ucp:recordIdentifier/text()')
            if recordId is None or recordId.strip() == '':
                raise ValueError("recordIdentifier is mandatory.")
            recordId = str(recordId)
            action = xpathFirst(updateRequest, 'ucp:action/text()')
            action = action.partition("info:srw/action/1/")[-1]
            if action in ['create', 'replace']:
                record = xpathFirst(updateRequest, 'srw:record')
                lxmlNode = record
                if self._sendRecordData:
                    lxmlNode = xpathFirst(record, 'srw:recordData/child::*')
                recordSchema = xpathFirst(record, 'srw:recordSchema/text()')
                localLogCollector['add'] = recordId
                yield self.all.add(
                    identifier=recordId,
                    partname=recordSchema,
                    lxmlNode=ElementTree(lxmlElementUntail(lxmlNode)),
                )
            elif action == 'delete':
                localLogCollector['delete'] = recordId
                yield self.all.delete(identifier=recordId)
            else:
                raise ValueError(
                    "action value should refer to either 'create', 'replace' or 'delete'."
                )
            yield self._respond()
    def handleRequest(self, Body="", **kwargs):
        yield okXml
        if not Body:
            yield self._respond(
                diagnosticUri='info:srw/diagnostic/12/9',
                details='Update request lacks a record in its body.',
                message='Missing mandatory element:  record rejected')
            return

        localLogCollector = dict()
        try:
            try:
                lxmlNode = parse(StringIO(Body))
            except XMLSyntaxError, e:
                self._log(Body, localLogCollector=localLogCollector)
                raise
            updateRequest = xpathFirst(lxmlNode, '/*[local-name()="updateRequest"]')
            recordId = xpathFirst(updateRequest, 'ucp:recordIdentifier/text()')
            if recordId is None or recordId.strip() == '':
                raise ValueError("recordIdentifier is mandatory.")
            recordId = str(recordId)
            action = xpathFirst(updateRequest, 'ucp:action/text()')
            action = action.partition("info:srw/action/1/")[-1]
            if action in ['create', 'replace']:
                record = xpathFirst(updateRequest, 'srw:record')
                lxmlNode = record
                if self._sendRecordData:
                    lxmlNode = xpathFirst(record, 'srw:recordData/child::*')
                recordSchema = xpathFirst(record, 'srw:recordSchema/text()')
                localLogCollector['add'] = recordId
                yield self.all.add(
                        identifier=recordId,
                        partname=recordSchema,
                        lxmlNode=ElementTree(lxmlElementUntail(lxmlNode)),
                    )
            elif action == 'delete':
                localLogCollector['delete'] = recordId
                if self._supportDeleteRecord:
                    yield self.all.deleteRecord(identifier=recordId, record=xpathFirst(updateRequest, 'srw:record'))
                else:
                    yield self.all.delete(identifier=recordId)
            else:
                raise ValueError("action value should refer to either 'create', 'replace' or 'delete'.")
            yield self._respond()
Example #4
0
 def _nodeOrText2ElementTree(self, nodeOrText):
     if type(nodeOrText) == _Element:
         return ElementTree(lxmlElementUntail(nodeOrText))
     return parse(StringIO(nodeOrText))
Example #5
0
 def _nodeOrText2ElementTree(self, nodeOrText):
     if type(nodeOrText) == _Element:
         return ElementTree(lxmlElementUntail(nodeOrText))
     return ElementTree(XML(nodeOrText))
 def _nodeOrText2ElementTree(self, nodeOrText):
     if type(nodeOrText) == _Element:
         return ElementTree(lxmlElementUntail(nodeOrText))
     return parse(StringIO(nodeOrText))
    def handleRequest(self, Body="", **kwargs):
        if type(Body) is str:
            Body = bytes(Body, encoding="utf-8")

        yield okXml
        if not Body:
            yield self._respond(
                diagnosticUri='info:srw/diagnostic/12/9',
                details='Update request lacks a record in its body.',
                message='Missing mandatory element:  record rejected')
            return

        localLogCollector = dict()
        try:
            try:
                lxmlNode = parse(BytesIO(Body))
            except XMLSyntaxError as e:
                self._log(Body, localLogCollector=localLogCollector)
                raise
            updateRequest = xpathFirst(lxmlNode,
                                       '/*[local-name()="updateRequest"]')
            recordId = xpathFirst(updateRequest, 'ucp:recordIdentifier/text()')
            if recordId is None or recordId.strip() == '':
                raise ValueError("recordIdentifier is mandatory.")
            recordId = str(recordId)
            action = xpathFirst(updateRequest, 'ucp:action/text()')
            action = action.partition("info:srw/action/1/")[-1]
            if action in ['create', 'replace']:
                record = xpathFirst(updateRequest, 'srw:record')
                lxmlNode = record
                if self._sendRecordData:
                    lxmlNode = xpathFirst(record, 'srw:recordData/child::*')
                recordSchema = xpathFirst(record, 'srw:recordSchema/text()')
                localLogCollector['add'] = recordId
                yield self.all.add(
                    identifier=recordId,
                    partname=recordSchema,
                    lxmlNode=ElementTree(lxmlElementUntail(lxmlNode)),
                )
            elif action == 'delete':
                localLogCollector['delete'] = recordId
                if self._supportDeleteRecord:
                    yield self.all.deleteRecord(identifier=recordId,
                                                record=xpathFirst(
                                                    updateRequest,
                                                    'srw:record'))
                else:
                    yield self.all.delete(identifier=recordId)
            else:
                raise ValueError(
                    "action value should refer to either 'create', 'replace' or 'delete'."
                )
            yield self._respond()
        except ValidateException as e:
            localLogCollector['invalid'] = recordId
            self._log(Body, e, localLogCollector=localLogCollector)
            yield self._respond(diagnosticUri='info:srw/diagnostic/12/12',
                                details=escapeXml(str(e)),
                                message='Invalid data:  record rejected')
        except Exception as e:
            self._log(Body, e, localLogCollector=localLogCollector)
            yield self._respond(diagnosticUri='info:srw/diagnostic/12/1',
                                details=escapeXml(format_exc()),
                                message='Invalid component:  record rejected')
        finally:
            self._collectLogForScope(sruRecordUpdate=localLogCollector)