15.2.155. camcops_server.cc_modules.cc_string¶
camcops_server/cc_modules/cc_string.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/>.
Manage the “extra strings” that the server reads from XML files. The server uses these for displaying tasks, and provides them to client devices.
- class camcops_server.cc_modules.cc_string.AS[source]¶
List of appstrings present in
camcops.xml
.Should match
appstrings.cpp
in the client, and of coursecamcops.xml
itself.
- camcops_server.cc_modules.cc_string.all_extra_strings_as_dicts(config_filename: str) Dict[str, Dict[str, Dict[str, str]]] [source]¶
Returns strings from the all the extra XML string files.
The result is cached (via a proper cache).
- Parameters
config_filename – a CamCOPS config filename
Returns: a dictionary like
{ "task1": { "stringname1": { "en-GB": "a string in British English", "da-DK": "a string in Danish", }, "stringname2": { ... }, }, "task2": { ... }, ... }
… in other words a
Dict[taskname: str, Dict[stringname: str, Dict[locale: str, stringvalue: str]]]
. For example,result["phq9"]["q5"][locale] == "5. Poor appetite or overeating"
There is also a top-level dictionary with the key
APPSTRING_TASKNAME
.XML format
The extra string files should look like this:
<?xml version="1.0" encoding="UTF-8"?> <resources> <task name="TASK_1" locale="en_GB"> <string name="NAME_1">VALUE</string> <string name="NAME_2">VALUE WITH\nNEWLINE</string> <!-- ... --> </task> <!-- ... --> </resources>
If the
locale
attribute is not specified, a locale (language) tag of""
is used internally, and will be the fallback position if nothing else is found.
- camcops_server.cc_modules.cc_string.text_contents(e: xml.etree.ElementTree.Element, plain: bool = False, strip: bool = True) str [source]¶
Extract the exact text contents of an XML element, including any XML/HTML tags within it.
A normal string looks like
<string name="stringname">words words words</string>
and we extract its contents (“words words words”) with
e.text
However, for this:
<string name="stringname">words <b>bold words</b> words</string>
we want to extract
words <b>bold words</b> words
and that”s a little trickier. This function does that.- Parameters
e – the
Element
to readplain – remove all HTML/XML tags?
strip – strip leading/trailing whitespace?
- Returns
the text contents of the element