Projet

Général

Profil

0001-misc-use-helper-function-to-get-xml-node-text-39240.patch

Frédéric Péters, 24 janvier 2020 14:26

Télécharger (15,9 ko)

Voir les différences:

Subject: [PATCH] misc: use helper function to get xml node text (#39240)

 wcs/fields.py             | 22 ++++++++--------
 wcs/formdef.py            | 18 ++++++-------
 wcs/qommon/misc.py        |  6 +++++
 wcs/qommon/xml_storage.py |  4 +--
 wcs/workflows.py          | 55 ++++++++++++++++++---------------------
 5 files changed, 54 insertions(+), 51 deletions(-)
wcs/fields.py
38 38
from .qommon import _, force_str
39 39
from .qommon import evalutils
40 40
from .qommon.form import *
41
from .qommon.misc import localstrftime, strftime, date_format, ellipsize, can_thumbnail
41
from .qommon.misc import localstrftime, strftime, date_format, ellipsize, can_thumbnail, xml_node_text
42 42
from .qommon.template import Template, TemplateError
43 43
from .qommon import get_cfg, get_logger
44 44

  
......
272 272
                continue
273 273
            if list(el):
274 274
                if type(getattr(self, attribute)) is list:
275
                    v = [force_str(x.text) for x in el]
275
                    v = [xml_node_text(x) for x in el]
276 276
                elif type(getattr(self, attribute)) is dict:
277 277
                    v = {}
278 278
                    for e in el:
279
                        v[e.tag] = force_str(e.text)
279
                        v[e.tag] = xml_node_text(e)
280 280
                else:
281 281
                    print('currently:', self.__dict__)
282 282
                    print('  attribute:', attribute)
......
291 291
                elif type(getattr(self, attribute)) is int:
292 292
                    setattr(self, attribute, int(el.text))
293 293
                else:
294
                    setattr(self, attribute, force_str(el.text))
294
                    setattr(self, attribute, xml_node_text(el))
295 295
        if include_id:
296 296
            try:
297
                self.id = force_str(elem.find('id').text)
297
                self.id = xml_node_text(elem.find('id'))
298 298
            except:
299 299
                pass
300 300

  
......
304 304
            return
305 305
        if node.findall('type'):
306 306
            self.condition = {
307
                'type': force_str(node.find('type').text),
308
                'value': force_str(node.find('value').text),
307
                'type': xml_node_text(node.find('type')),
308
                'value': xml_node_text(node.find('value')),
309 309
            }
310 310
        elif node.text:
311 311
            self.condition = {'type': 'python', 'value': force_str(node.text).strip()}
......
1788 1788
        for post_condition_node in node.findall('post_condition'):
1789 1789
            if post_condition_node.findall('condition/type'):
1790 1790
                condition = {
1791
                    'type': force_str(post_condition_node.find('condition/type').text),
1792
                    'value': force_str(post_condition_node.find('condition/value').text),
1791
                    'type': xml_node_text(post_condition_node.find('condition/type')),
1792
                    'value': xml_node_text(post_condition_node.find('condition/value')),
1793 1793
                }
1794 1794
            elif post_condition_node.find('condition').text:
1795 1795
                condition = {
1796 1796
                    'type': 'python',
1797
                    'value': force_str(post_condition_node.find('condition').text),
1797
                    'value': xml_node_text(post_condition_node.find('condition')),
1798 1798
                }
1799 1799
            else:
1800 1800
                continue
1801 1801
            self.post_conditions.append({
1802 1802
                'condition': condition,
1803
                'error_message': force_str(post_condition_node.find('error_message').text),
1803
                'error_message': xml_node_text(post_condition_node.find('error_message')),
1804 1804
            })
1805 1805

  
1806 1806
    def post_conditions_export_to_xml(self, node, charset, include_id=False):
wcs/formdef.py
35 35
from .qommon.storage import StorableObject, fix_key
36 36
from .qommon.cron import CronJob
37 37
from .qommon.form import *
38
from .qommon.misc import simplify, get_as_datetime
38
from .qommon.misc import simplify, get_as_datetime, xml_node_text
39 39
from .qommon import get_cfg
40 40
from .qommon.substitution import Substitutions
41 41
from .qommon.publisher import get_publisher_class
......
1061 1061
            value = tree.find(text_attribute)
1062 1062
            if value is None or value.text is None:
1063 1063
                continue
1064
            setattr(formdef, text_attribute, force_str(value.text))
1064
            setattr(formdef, text_attribute, xml_node_text(value))
1065 1065

  
1066 1066
        for boolean_attribute in cls.BOOLEAN_ATTRIBUTES:
1067 1067
            value = tree.find(boolean_attribute)
......
1095 1095
            if option.attrib.get('type') == 'date':
1096 1096
                option_value = time.strptime(option.text, '%Y-%m-%d')
1097 1097
            elif option.text:
1098
                option_value = force_str(option.text)
1098
                option_value = xml_node_text(option)
1099 1099
            elif option.findall('filename'):
1100
                filename = option.find('filename').text
1101
                upload = Upload(filename, content_type=option.find('content_type').text)
1100
                filename = xml_node_text(option.find('filename'))
1101
                upload = Upload(filename, content_type=xml_node_text(option.find('content_type')))
1102 1102
                option_value = UploadedFile(get_publisher().app_dir, filename, upload)
1103 1103
                option_value.set_content(base64.decodestring(force_bytes(option.find('content').text)))
1104 1104
            formdef.workflow_options[option.attrib.get('varname')] = option_value
......
1116 1116
                if Category.has_key(category_id):
1117 1117
                    formdef.category_id = category_id
1118 1118
            else:
1119
                category = force_str(category_node.text)
1119
                category = xml_node_text(category_node)
1120 1120
                for c in Category.select():
1121 1121
                    if c.name == category:
1122 1122
                        formdef.category_id = c.id
......
1130 1130
                if Workflow.has_key(workflow_id):
1131 1131
                    formdef.workflow_id = workflow_id
1132 1132
            else:
1133
                workflow = force_str(workflow_node.text)
1133
                workflow = xml_node_text(workflow_node)
1134 1134
                for w in Workflow.select():
1135 1135
                    if w.name == workflow:
1136 1136
                        formdef.workflow_id = w.id
......
1138 1138

  
1139 1139
        def get_role_by_node(role_node):
1140 1140
            role_id = None
1141
            value = force_str(role_node.text)
1141
            value = xml_node_text(role_node)
1142 1142
            if value.startswith('_') or value == 'logged-users':
1143 1143
                role_id = value
1144 1144
            elif include_id:
......
1183 1183
            formdef.geolocations = {}
1184 1184
            for child in geolocations_node:
1185 1185
                geoloc_key = child.attrib['key']
1186
                geoloc_value = force_str(child.text)
1186
                geoloc_value = xml_node_text(child)
1187 1187
                formdef.geolocations[geoloc_key] = geoloc_value
1188 1188

  
1189 1189
        if tree.find('required_authentication_contexts') is not None:
wcs/qommon/misc.py
495 495
    return elem
496 496

  
497 497

  
498
def xml_node_text(node):
499
    if node is None or node.text is None:
500
        return None
501
    return force_str(node.text)
502

  
503

  
498 504
def preprocess_struct_time(obj):
499 505
    if isinstance(obj, time.struct_time):
500 506
        dt = datetime.datetime(*obj[:6])
wcs/qommon/xml_storage.py
21 21
from quixote import get_publisher
22 22

  
23 23
from . import force_str
24
from .misc import indent_xml
24
from .misc import indent_xml, xml_node_text
25 25
from .storage import StorableObject
26 26

  
27 27

  
......
106 106
        return obj
107 107

  
108 108
    def import_str_from_xml(self, element, charset):
109
        return force_str(element.text)
109
        return xml_node_text(element)
110 110

  
111 111
    def import_int_from_xml(self, element, charset):
112 112
        return int(element.text)
wcs/workflows.py
31 31
from quixote import get_request, get_response, redirect
32 32

  
33 33
from .qommon import _, force_str
34
from .qommon.misc import C_, get_as_datetime, file_digest, get_foreground_colour
34
from .qommon.misc import C_, get_as_datetime, file_digest, get_foreground_colour, xml_node_text
35 35
from .qommon.storage import StorableObject, atomic_write, NotEqual, Contains, Null, pickle_2to3_conversion
36 36
from .qommon.form import *
37 37
from .qommon.humantime import seconds2humanduration
......
600 600
        if include_id and tree.attrib.get('id'):
601 601
            workflow.id = tree.attrib.get('id')
602 602

  
603
        workflow.name = force_str(tree.find('name').text)
603
        workflow.name = xml_node_text(tree.find('name'))
604 604

  
605 605
        if tree.find('roles') is not None:
606 606
            workflow.roles = {}
607 607
            for role_node in tree.findall('roles/role'):
608
                workflow.roles[role_node.attrib['id']] = force_str(role_node.text)
608
                workflow.roles[role_node.attrib['id']] = xml_node_text(role_node)
609 609

  
610 610
        if tree.find('last_modification') is not None:
611 611
            node = tree.find('last_modification')
......
877 877
                continue
878 878
            if list(el):
879 879
                if type(getattr(self, attribute)) is list:
880
                    v = [force_str(x.text) if x.text is not None else '' for x in el]
880
                    v = [xml_node_text(x) or '' for x in el]
881 881
                elif type(getattr(self, attribute)) is dict:
882 882
                    v = {}
883 883
                    for e in el:
884
                        v[e.tag] = force_str(e.text)
884
                        v[e.tag] = xml_node_text(e)
885 885
                else:
886 886
                    # ???
887 887
                    raise AssertionError
......
894 894
                elif type(getattr(self, attribute)) is int:
895 895
                    setattr(self, attribute, int(el.text))
896 896
                else:
897
                    setattr(self, attribute, force_str(el.text))
897
                    setattr(self, attribute, xml_node_text(el))
898 898

  
899 899
    def _roles_export_to_xml(self, attribute, item, charset, include_id=False):
900 900
        if not hasattr(self, attribute) or not getattr(self, attribute):
......
945 945
        if elem is None:
946 946
            return None
947 947

  
948
        value = force_str(elem.text)
948
        value = xml_node_text(elem) or ''
949 949

  
950 950
        # look for known static values
951 951
        if value.startswith('_') or value == 'logged-users':
......
1362 1362
        return status
1363 1363

  
1364 1364
    def init_with_xml(self, elem, charset, include_id=False):
1365
        self.id = force_str(elem.find('id').text)
1366
        self.name = force_str(elem.find('name').text)
1365
        self.id = xml_node_text(elem.find('id'))
1366
        self.name = xml_node_text(elem.find('name'))
1367 1367
        if elem.find('backoffice_info_text') is not None:
1368
            self.backoffice_info_text = force_str(elem.find('backoffice_info_text').text)
1368
            self.backoffice_info_text = xml_node_text(elem.find('backoffice_info_text'))
1369 1369

  
1370 1370
        self.items = []
1371 1371
        for item in elem.find('items'):
......
1403 1403
        return level
1404 1404

  
1405 1405
    def init_with_xml(self, elem, charset, include_id=False):
1406
        self.id = force_str(elem.find('id').text)
1407
        self.name = force_str(elem.find('name').text)
1406
        self.id = xml_node_text(elem.find('id'))
1407
        self.name = xml_node_text(elem.find('name'))
1408 1408
        if elem.find('colour') is not None:
1409
            self.colour = force_str(elem.find('colour').text)
1409
            self.colour = xml_node_text(elem.find('colour'))
1410 1410

  
1411 1411

  
1412 1412
class WorkflowStatus(object):
......
1652 1652
        return status
1653 1653

  
1654 1654
    def init_with_xml(self, elem, charset, include_id=False):
1655
        self.id = force_str(elem.find('id').text)
1656
        self.name = force_str(elem.find('name').text)
1655
        self.id = xml_node_text(elem.find('id'))
1656
        self.name = xml_node_text(elem.find('name'))
1657 1657
        if elem.find('colour') is not None:
1658
            self.colour = force_str(elem.find('colour').text)
1658
            self.colour = xml_node_text(elem.find('colour'))
1659 1659
        if elem.find('extra_css_class') is not None:
1660
            self.extra_css_class = force_str(elem.find('extra_css_class').text)
1660
            self.extra_css_class = xml_node_text(elem.find('extra_css_class'))
1661 1661
        if elem.find('forced_endpoint') is not None:
1662 1662
            self.forced_endpoint = (elem.find('forced_endpoint').text == 'true')
1663 1663
        if elem.find('backoffice_info_text') is not None:
1664
            self.backoffice_info_text = force_str(elem.find('backoffice_info_text').text)
1664
            self.backoffice_info_text = xml_node_text(elem.find('backoffice_info_text'))
1665 1665

  
1666 1666
        self.visibility = []
1667 1667
        for visibility_role in elem.findall('visibility/role'):
......
2015 2015
            return
2016 2016
        if node.findall('type'):
2017 2017
            self.condition = {
2018
                'type': force_str(node.find('type').text),
2019
                'value': force_str(node.find('value').text),
2018
                'type': xml_node_text(node.find('type')),
2019
                'value': xml_node_text(node.find('value')),
2020 2020
            }
2021 2021
        elif node.text:
2022 2022
            # backward compatibility
2023
            self.condition = {'type': 'python', 'value': force_str(node.text)}
2023
            self.condition = {'type': 'python', 'value': xml_node_text(node)}
2024 2024

  
2025 2025
    def q_admin_lookup(self, workflow, status, component, html_top):
2026 2026
        return None
......
2035 2035
        if elem is None:
2036 2036
            self.attachments = None
2037 2037
        else:
2038
            self.attachments = [force_str(item.text) for item in elem.findall('attachment')]
2038
            self.attachments = [xml_node_text(item) for item in elem.findall('attachment')]
2039 2039

  
2040 2040
    def get_attachments_options(self):
2041 2041
        attachments_options = [(None, '---', None)]
......
2303 2303
    def button_label_init_with_xml(self, element, charset, include_id=False):
2304 2304
        if element is None:
2305 2305
            return
2306
        if element.text is None:
2307
            # this means element is self-closing, <button_label />, which maps
2308
            # to None, meaning "no button".
2309
            self.button_label = None
2310
        else:
2311
            self.button_label = force_str(element.text)
2306
        # this can be None if element is self-closing, <button_label />, which
2307
        # then maps to None, meaning "no button".
2308
        self.button_label = xml_node_text(element)
2312 2309

  
2313 2310
register_item_class(CommentableWorkflowStatusItem)
2314 2311

  
......
2461 2458
        # override to allow for destination set with computed values.
2462 2459
        if elem is None:
2463 2460
            return None
2464
        value = force_str(elem.text)
2461
        value = xml_node_text(elem)
2465 2462

  
2466 2463
        if self.get_expression(value)['type'] != 'text' or '@' in value:
2467 2464
            return value
2468
-