summaryrefslogtreecommitdiff
path: root/BaseTools/Source/Python/Ecc/Exception.py
blob: b0882afa6289933bb0f79bd6e83f02db5644ded5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
## @file
# This file is used to parse exception items found by ECC tool
#
# Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution.  The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#

##
# Import Modules
#
from Xml.XmlRoutines import *
import Common.LongFilePathOs as os

# ExceptionXml to parse Exception Node of XML file
class ExceptionXml(object):
    def __init__(self):
        self.KeyWord = ''
        self.ErrorID = ''
        self.FilePath = ''
        
    def FromXml(self, Item, Key):
        self.KeyWord = XmlElement(Item, '%s/KeyWord' % Key)
        self.ErrorID = XmlElement(Item, '%s/ErrorID' % Key)
        self.FilePath = os.path.normpath(XmlElement(Item, '%s/FilePath' % Key))
        
    def __str__(self):
        return 'ErrorID = %s KeyWord = %s FilePath = %s' %(self.ErrorID, self.KeyWord, self.FilePath)

# ExceptionListXml to parse Exception Node List of XML file
class ExceptionListXml(object):
    def __init__(self):
        self.List = []
    
    def FromXmlFile(self, FilePath):
        XmlContent = XmlParseFile(FilePath)
        for Item in XmlList(XmlContent, '/ExceptionList/Exception'):
            Exp = ExceptionXml()
            Exp.FromXml(Item, 'Exception')
            self.List.append(Exp)
    
    def ToList(self):
        RtnList = []
        for Item in self.List:
            #RtnList.append((Item.ErrorID, Item.KeyWord, Item.FilePath))
            RtnList.append((Item.ErrorID, Item.KeyWord))
    
        return RtnList
        
    def __str__(self):
        RtnStr = ''
        if self.List:
            for Item in self.List:
                RtnStr = RtnStr + str(Item) + '\n'
        return RtnStr

# A class to check exception
class ExceptionCheck(object):
    def __init__(self, FilePath = None):
        self.ExceptionList = []
        self.ExceptionListXml = ExceptionListXml()
        self.LoadExceptionListXml(FilePath)

    def LoadExceptionListXml(self, FilePath):
        if FilePath and os.path.isfile(FilePath):
            self.ExceptionListXml.FromXmlFile(FilePath)
            self.ExceptionList = self.ExceptionListXml.ToList()
    
    def IsException(self, ErrorID, KeyWord, FileID=-1):
        if (str(ErrorID), KeyWord.replace('\r\n', '\n')) in self.ExceptionList:
            return True
        else:
            return False

##
#
# This acts like the main() function for the script, unless it is 'import'ed into another
# script.
#
if __name__ == '__main__':
    El = ExceptionCheck('C:\\Hess\\Project\\BuildTool\\src\\Ecc\\exception.xml')
    print El.ExceptionList