14.2.72. camcops_server.cc_modules.cc_string

camcops_server/cc_modules/cc_string.py


Copyright (C) 2012-2019 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/>.


Manage the “extra strings” that the server reads from XML files. The server uses these for displaying tasks, and provides them to client devices.

camcops_server.cc_modules.cc_string.all_extra_strings_as_dicts(config_filename: 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 whose keys are tasknames,
  • and whose values are each a dictionary
    • whose keys are string names
    • and whose values are strings.

For example, result['phq9']['q5'] == "5. Poor appetite or overeating". There is also a top-level dictionary with the key APPSTRING_TASKNAME.

The extra string files look like this:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <task name="TASK_1">
        <string name="NAME_1">VALUE</string>
        <string name="NAME_2">VALUE WITH\nNEWLINE</string>
        <!-- ... -->
    </task>
    <!-- ... -->
</resources>
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 read
  • plain – remove all HTML/XML tags?
  • strip – strip leading/trailing whitespace?
Returns:

the text contents of the element