File: configuration.py

package info (click to toggle)
pythoncard 0.8.2-5
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 9,364 kB
  • ctags: 6,988
  • sloc: python: 56,804; makefile: 60; sh: 22
file content (289 lines) | stat: -rw-r--r-- 9,769 bytes parent folder | download | duplicates (4)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
__version__ = "$Revision: 1.23 $"
__date__ = "$Date: 2003/07/21 18:45:53 $"
"""

import os, sys
import shutil
import util

"""
Unix
os.environ['HOME']

Windows
os.environ['USERPROFILE']

On Windows 2000: \Documents and Settings\%UserName%
On Windows NT 4.0: \Winnt\Profiles\%UserName%
On Windows 98: \Windows\Profiles\%UserName%
On Windows Me: \Windows\Profiles\%UserName%
On Windows 2000 after upgrading from Windows NT 4.0:
\Winnt\Profiles\%UserName%


fallback
__file__
"""

# KEA 2003-06-04
# the config dir was named .pythoncard in release 0.7
# but this caused problems on Windows and the Mac OS X Finder
# KJP 2004-12-19 Changed for Debian back to .pythoncard to be more consistent
# PYTHONCARD_CONFIG_DIR = "pythoncard_config"
PYTHONCARD_CONFIG_DIR = ".pythoncard"

PYTHONCARD_CONFIG_FILE = 'pythoncard_config.txt'

STC_CONFIG_FILE = 'stc-styles.cfg'

# KEA 2003-06-04
# pythoncard_config.txt
# for standalones
# updated 2004-05-14
DEFAULT_CONFIG = {
   'enableLogging':False,
   'logfile':'pythoncard.log',
   'logToStdout':True,
   'showMessageWatcher':False,
   'messageWatcherPosition':(0, 0),
   'messageWatcherSize':(200, 300),
   'showPropertyEditor':False,
   'propertyEditorPosition':(0, 0),
   'propertyEditorSize':(360, 240),
   'showShell':False,
   'shellPosition':(0, 0),
   'shellSize':(500, 200),
   'showNamespace':False,
   'namespacePosition':(0, 0),
   'namespaceSize':(600, 300),
}


#print "sys.path[0]", sys.path[0]
if util.main_is_frozen():
    # running standalone
    default_homedir = sys.path[0]
    homedir = default_homedir
else:
    # KJP 2004-12-19 Changed for Debian to put shared data in a more "correct" place
    #default_homedir = os.path.dirname(os.path.abspath(__file__))
    default_homedir = "/usr/share/pythoncard"
    #print "default_homedir", default_homedir
    for evar in ('HOME', 'USERPROFILE', 'TMP'):
        try:
            path = os.environ[evar]
            if os.path.isdir(path):
                homedir = os.path.join(path, PYTHONCARD_CONFIG_DIR)
                try:
                    # create the config directory if it
                    # doesn't already exist
                    if not os.path.isdir(homedir):
                        os.mkdir(homedir)
                    # if we were able to create the dir we're done
                    break
                except:
                    print "unable to create config directory", homedir
        except:
            homedir = os.path.join(default_homedir, PYTHONCARD_CONFIG_DIR)

    #print "homedir", homedir

    # should copying the config files be within the block above?
    try:
        # make copies of the default configs
        # for the user to modify
        for file in (PYTHONCARD_CONFIG_FILE, STC_CONFIG_FILE):
            path = os.path.join(homedir, file)
            if not os.path.exists(path):
                shutil.copy2(os.path.join(default_homedir, file), path)
    except:
        print "unable to copy config files"

try:
    configDict = util.readAndEvalFile(os.path.join(homedir, PYTHONCARD_CONFIG_FILE))
except:
    try:
        configDict = util.readAndEvalFile(os.path.join(default_homedir, PYTHONCARD_CONFIG_FILE))
    except:
        configDict = DEFAULT_CONFIG


def getStyleConfigPath():
    configfile = os.path.join(homedir, STC_CONFIG_FILE)
    if not os.path.exists(configfile):
        configfile = os.path.join(default_homedir, STC_CONFIG_FILE)
    return configfile

class Configuration :
    """
    Provides access to PythonCard's runtime configuration options.
    """

    class ConfigurationImpl :

        def __init__( self ) :
            # KEA 2001-11-17
            # change to import on configs
            """
            basePath = os.path.dirname( os.path.abspath( __file__ ) )
            path = os.path.join( basePath, PYTHONCARD_USER_CONFIG_FILE )
            # KEA 2001-08-09
            # first look in the directory of the program we're going to run
            # can't do this yet, given how the loader runs right now
            # then look for a user config file, then finally use the default
            #if os.path.exists(PYTHONCARD_USER_CONFIG_FILE):
            #    self._resource = resource.ResourceFile( PYTHONCARD_USER_CONFIG_FILE ).getResource()
            if os.path.exists(path):
                #print "using", path
                self._resource = resource.ResourceFile( path ).getResource()
                #print self._resource
            else:
                path = os.path.join( basePath, PYTHONCARD_CONFIG_FILE )
                self._resource = resource.ResourceFile( path ).getResource()
            """
            #self._resource = resource.Resource(configDict)
            self._resource = configDict
        
        def setOption( self, name, value ) :
            self._resource[name] = value
            #setattr( self._resource, name, value )
            #eval( 'self._resource.' + name + ' = ' + value )

        def getOption(self, name):
            return self._resource.get(name, None)

        #def getGuiToolkitName( self ) :
        #    return self._resource.gui

        def getLogFileName( self ) :
            # KEA 2001-12-13
            # return the application path for the log file directory
            # unless the user supplied filename contains a directory
            path = os.path.split(self.getOption('logfile'))[0]
            if path == '':
                return os.path.abspath( \
                    os.path.join( \
                    os.path.dirname(sys.argv[0]), \
                    self.getOption('logfile')))
            else:
                return os.path.abspath(self.getOption('logfile'))
            #return self._resource.logfile

        # KEA 2001-09-10
        # there should probably be generic method for turning Resources back into
        # dictionaries
        def saveConfig(self):
            global homedir
            
            #listX = self._resource.__dict__.keys()
            listX = self._resource.keys()
            listX.sort()
            dictStr = "{\n"
            for k in listX:
                if isinstance(self._resource[k], str):
                    dictStr += "'%s':'%s',\n" % (k, self._resource[k])
                else:
                    dictStr += "'%s':%s,\n" % (k, self._resource[k])
                """
                if isinstance(self._resource.__dict__[k], str):
                    dictStr += "'%s':'%s',\n" % (k, self._resource.__dict__[k])
                else:
                    dictStr += "'%s':%s,\n" % (k, self._resource.__dict__[k])
                """
            dictStr += "}\n"

            try:
                path = os.path.join(homedir, PYTHONCARD_CONFIG_FILE)
                f = open(path, 'w')
                f.write(dictStr)
                f.close()
            except:
                pass
                    

    _impl = None

    def __init__( self ) :
        if Configuration._impl is None :
            Configuration._impl = self.ConfigurationImpl()

    def setOption( self, name, value ) :
        Configuration._impl.setOption( name, value )

    def getOption( self, name ) :
        return Configuration._impl.getOption( name )

    #def getGuiToolkitName( self ) :
    #    return Configuration._impl.getGuiToolkitName()

    def getLogFileName( self ) :
        return Configuration._impl.getLogFileName()

    def saveConfig(self):
        Configuration._impl.saveConfig()


config = Configuration()
def getOption(name): return config.getOption(name)
def setOption(name, value): config.setOption(name, value)
def getLogFileName(): return config.getLogFileName()
def saveConfig(): config.saveConfig()

def hasOption( aOption ) :
    for arg in sys.argv :
        if arg == aOption :
            return 1
    return 0

def configOptions():
    # KEA 2001-08-09
    # this needs to be reworked, the old implementation overwrites whatever
    # might be in the .config.py files
    # also this implementation can only turn on an option, not turn it off
    if hasOption('-p'):
        setOption('showPropertyEditor', 1)
    if hasOption('-m'):
        setOption('showMessageWatcher', 1)
    if hasOption('-l'):
        setOption('enableLogging', 1)
    if hasOption('-s'):
        setOption('showShell', 1)
    if hasOption('-n'):
        setOption('showNamespace', 1)
    if hasOption('-d'):
        setOption('showDebugMenu', 1)

def saveUserConfiguration(app):
    """
    Given an application instance, save the current settings of the runtime
    windows.
    """
    if app.mw is not None:
        setOption('messageWatcherPosition', app.mw.GetPositionTuple())
        setOption('messageWatcherSize', app.mw.GetSizeTuple())
        setOption('showMessageWatcher', False)
    if app.namespaceFrame is not None:
        setOption('namespacePosition', app.namespaceFrame.GetPositionTuple())
        setOption('namespaceSize', app.namespaceFrame.GetSizeTuple())
        setOption('showNamespace', False)
    if app.pw is not None:
        setOption('propertyEditorPosition', app.pw.GetPositionTuple())
        setOption('propertyEditorSize', app.pw.GetSizeTuple())
        setOption('showPropertyEditor', False)
    if app.shellFrame is not None:
        setOption('shellPosition', app.shellFrame.GetPositionTuple())
        setOption('shellSize', app.shellFrame.GetSizeTuple())
        setOption('showShell', False)
    setOption('enableLogging', False)
    saveConfig()

        
if __name__ == '__main__' :
    __file__ = 'config.py'
    print 'config.py - Unit Test'
    setOption( 'myOption', 'myValue' )
    print getOption( 'myOption' )
    print 'showMessageWatcher=', getOption( 'showMessageWatcher' )
    setOption( 'showMessageWatcher', 1 )
    print 'showMessageWatcher=', getOption( 'showMessageWatcher' )