~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/apps/RuntimeSettings.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2001-2005 Free Software Foundation
 
3
#
 
4
# This file is part of GNU Enterprise.
 
5
#
 
6
# GNU Enterprise is free software; you can redistribute it
 
7
# and/or modify it under the terms of the GNU General Public
 
8
# License as published by the Free Software Foundation; either
 
9
# version 2, or (at your option) any later version.
 
10
#
 
11
# GNU Enterprise is distributed in the hope that it will be
 
12
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
13
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
14
# PURPOSE. See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public
 
17
# License along with program; see the file COPYING. If not,
 
18
# write to the Free Software Foundation, Inc., 59 Temple Place
 
19
# - Suite 330, Boston, MA 02111-1307, USA.
 
20
#
 
21
# FILE:
 
22
# RuntimeSettings.py
 
23
#
 
24
# DESCRIPTION:
 
25
# Saves the state of designer between runs. Saves such data as
 
26
# window positions, size, visibility, etc.
 
27
#
 
28
# NOTES:
 
29
 
 
30
import sys, os, ConfigParser
 
31
 
 
32
 
 
33
# TODO: Should this save into the Win32 registry if its available?????
 
34
# TODO: This is, after all, session-specific information and not
 
35
# TODO: user-settable configuration data.
 
36
 
 
37
if not globals().has_key('location'):
 
38
  location = None
 
39
 
 
40
def init(configFilename="default.ini", homeConfigDir=".gnue"):
 
41
  global location, config 
 
42
  if os.environ.has_key('HOME'):
 
43
    try:
 
44
      os.makedirs(os.path.join(os.environ['HOME'], homeConfigDir))
 
45
    except:
 
46
      pass
 
47
    location = os.path.join(os.environ['HOME'], homeConfigDir ,configFilename)
 
48
  elif sys.platform[:3] in ('win','mac'):
 
49
    location = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])),configFilename)
 
50
  else:
 
51
    location = configFilename
 
52
 
 
53
  try:
 
54
    config = ConfigParser.ConfigParser()
 
55
    config.read(location)
 
56
  except:
 
57
    config = None
 
58
 
 
59
def registerInstance(instance):
 
60
  instance._runtimes = []
 
61
 
 
62
 
 
63
def get(section, setting, default):
 
64
  # Backwards compatability
 
65
  if location == None:
 
66
    init()
 
67
  try:
 
68
    return config.get(section, setting)
 
69
  except:
 
70
    return default
 
71
 
 
72
 
 
73
def getint(section, setting, default):
 
74
  # Backwards compatability
 
75
  if location == None:
 
76
    init()
 
77
  try:
 
78
    return config.getint(section, setting)
 
79
  except:
 
80
    return default
 
81
 
 
82
#
 
83
# Save the runtime settings
 
84
#
 
85
def saveRuntimeSettings(instance):
 
86
  from gnue.common.apps.GDebug import _DEBUG_LEVELS
 
87
  if location:
 
88
    try:
 
89
      fh = open(location,'w')
 
90
      try:
 
91
        for h in instance._runtimes:
 
92
          section, hash = h.saveRuntimeSettings()
 
93
          if len(hash.keys()):
 
94
            if not config.has_section(section):
 
95
              config.add_section(section)
 
96
            for key in hash.keys():
 
97
              config.set(section, key, "%s" % hash[key])
 
98
 
 
99
        config.write(fh)
 
100
      except:
 
101
        print o(u_("\nWarning: Unable to save session data to %s\n") %
 
102
                   location)
 
103
        if _DEBUG_LEVELS != [0]:
 
104
          raise
 
105
      fh.close()
 
106
    except:
 
107
      print o(u_("\nWarning: Unable to save session data to %s\n") % location)
 
108
      if _DEBUG_LEVELS != [0]:
 
109
        raise
 
110
 
 
111
#
 
112
# Any object (class) that has settings it wants saved should
 
113
# register with this method.  The object should define a
 
114
# getRuntimeSettings() method.
 
115
#
 
116
def registerRuntimeSettingHandler(instance, object):
 
117
  instance._runtimes.append(object)
 
118
 
 
119
 
 
120