15.2.179. camcops_server.cc_modules.cc_xml¶
camcops_server/cc_modules/cc_xml.py
Copyright (C) 2012, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
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 <https://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: Optional[Any] = None, datatype: Optional[str] = None, comment: Optional[str] = None, literal: Optional[str] = None)[source]¶
Represents XML data in a tree.
- __init__(name: str, value: Optional[Any] = None, datatype: Optional[str] = None, comment: Optional[str] = None, literal: Optional[str] = None) None [source]¶
- Parameters
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.
- __init__(literal: str) None [source]¶
- Parameters
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
- camcops_server.cc_modules.cc_xml.get_xml_blob_element(name: str, blobdata: Optional[bytes], comment: Optional[str] = None) camcops_server.cc_modules.cc_xml.XmlElement [source]¶
Returns an XmlElement representing a base-64-encoded BLOB.
- Parameters
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) Optional[str] [source]¶
Returns the XML schema datatype from an SQLAlchemy Column, such as
Integer()
. Compareget_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
. Compareget_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
.- Parameters
root – root
camcops_server.cc_modules.cc_xml.XmlElement
indent_spaces – number of spaces to indent formatted XML
eol – end-of-line string
include_comments – include comments describing each field?
- 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, List[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.- Parameters
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:Comments:
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 https://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.
- Parameters
req – the
camcops_server.cc_modules.cc_request.CamcopsRequest
obj – the SQLAlchemy ORM object
skip_fields – database column names to skip
- Returns
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: Optional[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.- Parameters
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[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.- Parameters
summaries – list of
SummaryElement
objectsskip_fields – summary element names to skip
sort_by_name – sort branches by element name?