14.2.119. camcops_server.cc_modules.cc_xml


Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <http://www.gnu.org/licenses/>.

XML helper functions/classes.

class camcops_server.cc_modules.cc_xml.XmlDataTypes[source]

Constants representing standard XML data types.

class camcops_server.cc_modules.cc_xml.XmlElement(name: str, value: Any = None, datatype: str = None, comment: str = None, literal: str = None)[source]

Represents XML data in a tree.

  • name – name of this XML element
  • value – value of this element: may be a raw value or a list of camcops_server.cc_modules.cc_xml.XmlElement objects (default: None)
  • datatype – data type of this element (default: None)
  • comment – description of this element (default: None)
  • literal – literal XML; overrides all other options
class camcops_server.cc_modules.cc_xml.XmlLiteral(literal: str)[source]

Represents literal XML.

camcops_server.cc_modules.cc_xml.get_xml_blob_element(name: str, blobdata: Union[bytes, NoneType], comment: str = None) → camcops_server.cc_modules.cc_xml.XmlElement[source]

Returns an XmlElement representing a base-64-encoded BLOB.

  • name – XML element name
  • blobdata – the raw binary, or None
  • comment – XML comment
camcops_server.cc_modules.cc_xml.get_xml_datatype_from_sqla_column(column: sqlalchemy.sql.schema.Column) → Union[str, NoneType][source]

Returns the XML schema datatype from an SQLAlchemy Column, such as Integer(). Compare get_xml_datatype_from_sqla_column_type().

camcops_server.cc_modules.cc_xml.get_xml_datatype_from_sqla_column_type(coltype: sqlalchemy.sql.type_api.TypeEngine) → str[source]

Returns the XML schema datatype from an SQLAlchemy column type, such as Integer. Compare get_xml_datatype_from_sqla_column().

camcops_server.cc_modules.cc_xml.get_xml_document(root: camcops_server.cc_modules.cc_xml.XmlElement, indent_spaces: int = 4, eol: str = '\n', include_comments: bool = False) → str[source]

Returns an entire XML document as text, given the root camcops_server.cc_modules.cc_xml.XmlElement.

camcops_server.cc_modules.cc_xml.get_xml_tree(element: Union[camcops_server.cc_modules.cc_xml.XmlElement, camcops_server.cc_modules.cc_simpleobjects.XmlSimpleValue, typing.List[typing.Union[camcops_server.cc_modules.cc_xml.XmlElement, camcops_server.cc_modules.cc_simpleobjects.XmlSimpleValue]]], level: int = 0, indent_spaces: int = 4, eol: str = '\n', include_comments: bool = False) → str[source]

Returns an camcops_server.cc_modules.cc_xml.XmlElement as text.

  • element – root camcops_server.cc_modules.cc_xml.XmlElement
  • level – starting level/depth (used for recursion)
  • indent_spaces – number of spaces to indent formatted XML
  • eol – end-of-line string
  • include_comments – include comments describing each field?

We will represent NULL values with xsi:nil, but this requires a namespace:


Regarding newlines:

  • We do nothing special, i.e. newlines are provided in raw format.
  • However, some browsers may fail to display them correctly (i.e. they look like they’re missing) – e.g. Firefox, Chrome – see http://stackoverflow.com/questions/2004386. Just try saving and inspecting the results with a text editor, or use the browser’s “View Source” function (which, for Chrome, shows both newlines and line numbers too).
camcops_server.cc_modules.cc_xml.make_xml_branches_from_blobs(req: CamcopsRequest, obj, skip_fields: List[str] = None) → List[camcops_server.cc_modules.cc_xml.XmlElement][source]

Return XML branches from those attributes of an SQLAlchemy ORM object (e.g. task) that represent BLOBs.


a list of camcops_server.cc_modules.cc_xml.XmlElement objects

camcops_server.cc_modules.cc_xml.make_xml_branches_from_columns(obj, skip_fields: List[str] = None) → List[camcops_server.cc_modules.cc_xml.XmlElement][source]

Returns a list of XML branches, each an camcops_server.cc_modules.cc_xml.XmlElement, from an SQLAlchemy ORM object, using the list of SQLAlchemy Column objects that define/describe its fields.

  • obj – the SQLAlchemy ORM object
  • skip_fields – database column names to skip
camcops_server.cc_modules.cc_xml.make_xml_branches_from_summaries(summaries: List[_ForwardRef('SummaryElement')], skip_fields: List[str] = None, sort_by_name: bool = True) → List[camcops_server.cc_modules.cc_xml.XmlElement][source]

Returns a list of XML branches, each an camcops_server.cc_modules.cc_xml.XmlElement, from a list of summary data provided by a task.

  • summaries – list of SummaryElement objects
  • skip_fields – summary element names to skip
  • sort_by_name – sort branches by element name?
camcops_server.cc_modules.cc_xml.xml_escape_value(value: str) → str[source]

Escape a value for XML.

camcops_server.cc_modules.cc_xml.xml_header(eol: str = '\n') → str[source]

XML declaration header.

camcops_server.cc_modules.cc_xml.xml_quote_attribute(attr: str) → str[source]

Escapes and quotes an attribute for XML.

More stringent than value escaping.