plist_writer.py 3.73 KB
Newer Older
1
2
import sys

Sasa Zhang's avatar
Sasa Zhang committed
3
4

# class Plist_writer(object):
5
6
class Plist_writer:

Sasa Zhang's avatar
Sasa Zhang committed
7
    def __indent(self, iLevel, iNb_space=4):
8
9
        return " " * iNb_space * iLevel

Sasa Zhang's avatar
Sasa Zhang committed
10
    def __write_header(self):
11
        self.output.write('<?xml version="1.0" encoding="UTF-8"?>\n')
Sasa Zhang's avatar
Sasa Zhang committed
12
13
        self.output.write('<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" '
                          '"http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n')
14
15
        self.output.write('<plist version="1.0">\n')

Sasa Zhang's avatar
Sasa Zhang committed
16
17
    def __write_footer(self):
        self.output.write("</plist>\n")
18

Sasa Zhang's avatar
Sasa Zhang committed
19
    def __dispatch_value(self, value, iLevel):
20
        valueType = type(value)
Sasa Zhang's avatar
Sasa Zhang committed
21
        if valueType == dict:
22
            self.__dump_dict(value, iLevel)
Sasa Zhang's avatar
Sasa Zhang committed
23
        elif valueType == list:
24
            self.__dump_list(value, iLevel)
Sasa Zhang's avatar
Sasa Zhang committed
25
        elif valueType == float:
26
            self.__dump_float(value, iLevel)
Sasa Zhang's avatar
Sasa Zhang committed
27
        elif valueType == int:
28
            self.__dump_int(value, iLevel)
Sasa Zhang's avatar
Sasa Zhang committed
29
        elif valueType == bool:
30
            self.__dump_bool(value, iLevel)
Sasa Zhang's avatar
Sasa Zhang committed
31
        elif isinstance(value, str):
32
33
34
35
            self.__dump_string(value, iLevel)
        else:
            self.output.write("Type error, can't create config file")
            # TODOFF raise an exception
Sasa Zhang's avatar
Sasa Zhang committed
36
            print("Error when generating config file. Type error")
37
38
            sys.exit(3)

Sasa Zhang's avatar
Sasa Zhang committed
39
40
    def __dump_string(self, sString, iLevel):
        self.output.write("%s<string>%s</string>\n" % (self.__indent(iLevel), sString))
41

Sasa Zhang's avatar
Sasa Zhang committed
42
43
    def __dump_float(self, fNumber, iLevel):
        self.output.write("%s<real>%s</real>\n" % (self.__indent(iLevel), fNumber))
44

Sasa Zhang's avatar
Sasa Zhang committed
45
46
    def __dump_int(self, iNumber, iLevel):
        self.output.write("%s<integer>%s</integer>\n" % (self.__indent(iLevel), iNumber))
47

Sasa Zhang's avatar
Sasa Zhang committed
48
49
    def __dump_bool(self, bValue, iLevel):
        if bValue:
50
51
52
            sBool = "true"
        else:
            sBool = "false"
Sasa Zhang's avatar
Sasa Zhang committed
53
        self.output.write("%s<%s/>\n" % (self.__indent(iLevel), sBool))
54

Sasa Zhang's avatar
Sasa Zhang committed
55
    def __dump_list(self, lList, iLevel):
56
57
58
        # Imprime tag list
        self.output.write(self.__indent(iLevel) + "<array>\n")
        for item in lList:
Sasa Zhang's avatar
Sasa Zhang committed
59
            self.__dispatch_value(item, iLevel + 1)
60
61
        self.output.write(self.__indent(iLevel) + "</array>\n")

Sasa Zhang's avatar
Sasa Zhang committed
62
    def __dump_dict(self, dDict, iLevel):
63
        self.output.write(self.__indent(iLevel) + "<dict>\n")
Sasa Zhang's avatar
Sasa Zhang committed
64
        lKeys = list(dDict.keys())
65
66
        lKeys.sort()
        for key in lKeys:
Sasa Zhang's avatar
Sasa Zhang committed
67
            self.output.write("%s<key>%s</key>\n" % (self.__indent(iLevel + 1), key))
68
            value = dDict[key]
Sasa Zhang's avatar
Sasa Zhang committed
69
            self.__dispatch_value(value, iLevel + 1)
70
71
        self.output.write(self.__indent(iLevel) + "</dict>\n")

Sasa Zhang's avatar
Sasa Zhang committed
72
73
    def __dump_structured(self, dDict, iLevel, bDump_all):
        if bDump_all:
74
75
76
77
            iTreshold = -1
        else:
            iTreshold = 0
        self.output.write(self.__indent(iLevel) + "<dict>\n")
Sasa Zhang's avatar
Sasa Zhang committed
78
        lKeys = list(dDict.keys())
79
80
81
82
        lKeys.sort()
        for key in lKeys:
            if dDict[key]['FROM'] > iTreshold:
                sComments = dDict[key]['COMMENTS']
Sasa Zhang's avatar
Sasa Zhang committed
83
84
                self.output.write(self.__indent(iLevel + 1) + "<!-- " + sComments + " -->\n")
                self.output.write("%s<key>%s</key>\n" % (self.__indent(iLevel + 1), key))
85
                value = dDict[key]['VALUE']
Sasa Zhang's avatar
Sasa Zhang committed
86
                self.__dispatch_value(value, iLevel + 1)
87
88
89
                self.output.write("\n")
        self.output.write(self.__indent(iLevel) + "</dict>\n")

Sasa Zhang's avatar
Sasa Zhang committed
90
    def write(self, sFilename, dConfig, bStructured=False, bDump_all=False):
91
92
        try:
            self.output = open(sFilename, 'w')
Sasa Zhang's avatar
Sasa Zhang committed
93
94
        except IOError as errorInfo:
            raise IOError(errorInfo)
95
        self.__write_header()
Sasa Zhang's avatar
Sasa Zhang committed
96
        if bStructured:
97
98
99
100
101
            self.__dump_structured(dConfig, 0, bDump_all)
        else:
            self.__dump_dict(dConfig, 0)
        self.__write_footer()
        self.output.close()