metro_validate.py 3.98 KB
Newer Older
1
2
# METRo : Model of the Environment and Temperature of Roads
# METRo is Free and is proudly provided by the Government of Canada
Miguel Tremblay's avatar
Miguel Tremblay committed
3
# Copyright (C) Her Majesty The Queen in Right of Canada, Environment Canada, 2006
Sasa Zhang's avatar
Sasa Zhang committed
4
#
5
#  Questions or bugs report: metro@ec.gc.ca
6
#  METRo repository: https://framagit.org/metroprojects/metro
7
#  Documentation: https://framagit.org/metroprojects/metro/wikis/home
8
9
10
11
12
13
14
15
#
#
# Code contributed by:
#  Miguel Tremblay - Canadian meteorological center
#  Francois Fortin - Canadian meteorological center
#
#  $LastChangedDate$
#  $LastChangedRevision$
Sasa Zhang's avatar
Sasa Zhang committed
16
###################################################################################
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#  This program 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 2 of the License, or
#  (at your option) any later version.
#
#  This program 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 this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

31
32

"""
Sasa Zhang's avatar
Sasa Zhang committed
33
    Name:          metro_validate.py
34

Sasa Zhang's avatar
Sasa Zhang committed
35
    Description:   Module that is used to validate the well-formation of the XML files.
36

Sasa Zhang's avatar
Sasa Zhang committed
37
    Author:        Francois Fortin
38

Sasa Zhang's avatar
Sasa Zhang committed
39
40
41
42
43
    Date:          2004
"""


from executable_module.metro_module import Metro_module
44
import metro_logger
Miguel Tremblay's avatar
   
Miguel Tremblay committed
45
import metro_error
46
47
48
from toolbox import metro_xml
from toolbox import metro_util

Sasa Zhang's avatar
Sasa Zhang committed
49

50
51
_ = metro_util.init_translation('metro_validate')

Sasa Zhang's avatar
Sasa Zhang committed
52

53
54
55
56
class Metro_validate(Metro_module):

    def __init__(self):
        if self.__class__ is Metro_validate:
Sasa Zhang's avatar
Sasa Zhang committed
57
            sMessage = _("class %s is a virtual class") % str(self.__class__)
58
59
60
            raise NotImplementedError(sMessage)

    # Redefined method
Sasa Zhang's avatar
Sasa Zhang committed
61
    def get_receive_type(self):
62
63
        return Metro_module.DATATYPE_INPUT

Sasa Zhang's avatar
Sasa Zhang committed
64
    def get_send_type(self):
65
66
67
        return Metro_module.DATATYPE_INPUT

    # Protected method
Sasa Zhang's avatar
Sasa Zhang committed
68
    def _validate(self, sInfdata_tag):
69
70
71
72
        if self.infdata_exist(sInfdata_tag):
            pInfdata = self.get_infdata_reference(sInfdata_tag)
            sXml_content = pInfdata.get_input_information()
            if sXml_content != "":
Sasa Zhang's avatar
Sasa Zhang committed
73
                try:
74
                    self.__validate_xml_string(sXml_content)
Sasa Zhang's avatar
Sasa Zhang committed
75
76
77
78
                except metro_error.Metro_xml_error as inst:
                    sMessage = _("Fatal Error when validating %s ") % sInfdata_tag + \
                               _("XML string.\nThe error is:\n%s") % (str(inst))
                    metro_logger.print_message(metro_logger.LOGGER_MSG_STOP, sMessage)
79
                else:
Sasa Zhang's avatar
Sasa Zhang committed
80
81
                    sMessage = _("%s XML string has been validated") % sInfdata_tag
                    metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE, sMessage)
82
            else:
Sasa Zhang's avatar
Sasa Zhang committed
83
84
                sMessage = _("Fatal Error, %s XML string is empty") % sInfdata_tag
                metro_logger.print_message(metro_logger.LOGGER_MSG_STOP, sMessage)
85
        else:
Sasa Zhang's avatar
Sasa Zhang committed
86
87
            sMessage = _("Fatal Error, no %s XML string to validate.") % sInfdata_tag
            metro_logger.print_message(metro_logger.LOGGER_MSG_STOP, sMessage)
88
89

    # Private method
Sasa Zhang's avatar
Sasa Zhang committed
90
91
    def __validate_xml_string(self, sXml_content):
        if sXml_content is not None:
92
93
            try:
                return metro_xml.validate_string(sXml_content)
Sasa Zhang's avatar
Sasa Zhang committed
94
            except metro_error.Metro_xml_error as inst:
Sasa Zhang's avatar
Sasa Zhang committed
95
96
                metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,
                                           _("Error when validating ") + _("XML string."))
Miguel Tremblay's avatar
   
Miguel Tremblay committed
97
                raise metro_error.Metro_xml_error(str(inst))
98
99
        else:
            sMessage = _("No XML string to validate")
Sasa Zhang's avatar
Sasa Zhang committed
100
            metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage)
Miguel Tremblay's avatar
   
Miguel Tremblay committed
101
            raise metro_error.Metro_xml_error(sMessage)