File: option.py

package info (click to toggle)
convertall 0.7.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,724 kB
  • sloc: python: 2,289; makefile: 8
file content (182 lines) | stat: -rw-r--r-- 7,223 bytes parent folder | download | duplicates (5)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#****************************************************************************
# option.py, provides classes to read and set user preferences
#
# Copyright (C) 2014, Douglas W. Bell
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License, either Version 2 or any later
# version.  This program is distributed in the hope that it will be useful,
# but WITTHOUT ANY WARRANTY.  See the included LICENSE file for details.
#*****************************************************************************

import sys
import os.path

class Option:
    """Stores and retrieves string options.
    """
    def __init__(self, baseFileName, keySpaces=20):
        self.path = ''
        if baseFileName:
            if sys.platform.startswith('win'):
                fileName = '{0}.ini'.format(baseFileName)
                userPath = os.environ.get('APPDATA', '')
                if userPath:
                    userPath = os.path.join(userPath, 'bellz', baseFileName)
            else:
                fileName = '.{0}'.format(baseFileName)
                userPath = os.environ.get('HOME', '')
            self.path = os.path.join(userPath, fileName)
            if not os.path.exists(self.path):
                modPath = os.path.abspath(sys.path[0])
                if modPath.endswith('.zip') or modPath.endswith('.exe'):
                    modPath = os.path.dirname(modPath)  # for py2exe/cx_freeze
                self.path = os.path.join(modPath, fileName)
                if not os.access(self.path, os.W_OK):
                    self.path = os.path.join(userPath, fileName)
                    if not os.path.exists(userPath):
                        try:
                            os.makedirs(userPath)
                        except OSError:
                            print('Error - could not write to config dir')
                            self.path = ''
        self.keySpaces = keySpaces
        self.dfltDict = {}
        self.userDict = {}
        self.dictList = (self.userDict, self.dfltDict)
        self.chgList = []

    def loadAll(self, defaultList):
        """Reads defaultList & file, writes file if required
           return true if file read.
        """
        self.loadSet(defaultList, self.dfltDict)
        if self.path:
            try:
                with open(self.path, 'r', encoding='utf-8') as f:
                    self.loadSet(f.readlines(), self.userDict)
                    return True
            except IOError:
                try:
                    with open(self.path, 'w', encoding='utf-8') as f:
                        f.writelines([line + '\n' for line in defaultList])
                except IOError:
                    print('Error - could not write to config file', self.path)
                    self.path = ''
                return False

    def loadSet(self, list, data):
        """Reads settings from list into dict.
        """
        for line in list:
            line = line.split('#', 1)[0].strip()
            if line:
                item = line.split(None, 1) + ['']   # add value if blank
                data[item[0]] = item[1].strip()

    def addData(self, key, strData, storeChange=0):
        """Add new entry, add to write list if storeChange.
        """
        self.userDict[key] = strData
        if storeChange:
            self.chgList.append(key)

    def boolData(self, key):
        """Returns true or false from yes or no in option data.
        """
        for data in self.dictList:
            val = data.get(key)
            if val and val[0] in ('y', 'Y'):
                return True
            if val and val[0] in ('n', 'N'):
                return False
        print('Option error - bool key', key, 'is not valid')
        return False

    def numData(self, key, min=None, max=None):
        """Return float from option data.
        """
        for data in self.dictList:
            val = data.get(key)
            if val:
                try:
                    num = float(val)
                    if (min == None or num >= min) and \
                       (max == None or num <= max):
                        return num
                except ValueError:
                    pass
        print('Option error - float key', key, 'is not valid')
        return 0

    def intData(self, key, min=None, max=None):
        """Return int from option data.
        """
        for data in self.dictList:
            val = data.get(key)
            if val:
                try:
                    num = int(val)
                    if (min == None or num >= min) and \
                       (max == None or num <= max):
                        return num
                except ValueError:
                    pass
        print('Option error - int key', key, 'is not valid')
        return 0

    def strData(self, key, emptyOk=0):
        """Return string from option data.
        """
        for data in self.dictList:
            val = data.get(key)
            if val != None:
                if val or emptyOk:
                    return val
        print('Option error - string key', key, 'is not valid')
        return ''

    def changeData(self, key, strData, storeChange):
        """Change entry, add to write list if storeChange
           Return true if changed.
        """
        for data in self.dictList:
            val = data.get(key)
            if val != None:
                if strData == val:  # no change reqd
                    return False
                self.userDict[key] = strData
                if storeChange:
                    self.chgList.append(key)
                return True
        print('Option error - key', key, 'is not valid')
        return False

    def writeChanges(self):
        """Write any stored changes to the option file - rtn true on success.
        """
        if self.path and self.chgList:
            try:
                with open(self.path, 'r', encoding='utf-8') as f:
                    fileList = f.readlines()
                for key in self.chgList[:]:
                    hitList = [line for line in fileList if
                               line.strip().split(None, 1)[:1] == [key]]
                    if not hitList:
                        hitList = [line for line in fileList if
                                   line.replace('#', ' ', 1).strip().
                                   split(None, 1)[:1] == [key]]
                    if hitList:
                        fileList[fileList.index(hitList[-1])] = '{0}{1}\n'.\
                                format(key.ljust(self.keySpaces),
                                       self.userDict[key])
                        self.chgList.remove(key)
                for key in self.chgList:
                    fileList.append('{0}{1}\n'.format(key.ljust(self.keySpaces),
                                                      self.userDict[key]))
                with open(self.path, 'w', encoding='utf-8') as f:
                    f.writelines([line for line in fileList])
                return True
            except IOError:
                print('Error - could not write to config file', self.path)
        return False