root/trunk/RBSkinning/RBSkinning/InheritableSettings.py

Revision 495, 5.7 kB (checked in by sholloway, 6 years ago)

Simplifed some inheritable settings code
Adjusted CompoundSkinObject? for the inheritable settings code
Modifed expander code to work with variable expander windows from the Skin Object

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ License
4 ##~
5 ##- The RuneBlade Foundation library is intended to ease some
6 ##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.)
7 ##- applications, while providing the flexibility to modularly change the
8 ##- architecture. Enjoy.
9 ##~
10 ##~ Copyright (C) 2002  TechGame Networks, LLC.
11 ##~
12 ##~ This library is free software; you can redistribute it and/or
13 ##~ modify it under the terms of the BSD style License as found in the
14 ##~ LICENSE file included with this distribution.
15 ##~
16 ##~ TechGame Networks, LLC can be reached at:
17 ##~ 3578 E. Hartsel Drive #211
18 ##~ Colorado Springs, Colorado, USA, 80920
19 ##~
20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #~ Imports
24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 from RBFoundation.ChainedDict import ChainedDict
27 from RBFoundation.Utilities import *
28
29 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 #~ Definitions
31 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32
33 class InheritSettingsMixin(object):
34     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35     #~ Constants / Variables / Etc.
36     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37
38     BaseNamespace = 'http://namespaces.runeblade.com/skin'
39
40     BaseSettingsSave = (BaseNamespace, 'save')
41     BaseSettingsInherit = (BaseNamespace, 'inherit')
42     BaseSettingsLoad = (BaseNamespace, 'load')
43     BaseSettingsRoot = (BaseNamespace, 'root')
44
45     # SettingsSaveOpt is generally used to signify whether certain attributes
46     # should be saved when skin:save is invoked...  True signifies it should be
47     # saved, False signifys that it should not be saved.  The default is
48     # signified by the None entry.
49     SettingsSaveOpt = {
50         BaseSettingsSave: False,
51         BaseSettingsInherit: False,
52         BaseSettingsLoad: False,
53         BaseSettingsRoot: False,
54         None: True,
55         }
56
57     default_settings = {
58         BaseSettingsInherit:'',
59         BaseSettingsLoad:'',
60         #BaseSettingsSave:'',
61         #BaseSettingsRoot:'0',
62         }
63
64     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65     #~ Protected Methods
66     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67
68     def _InheritSettings(self, settings, InheritList, Resilliant=0):
69         InheritList.reverse()
70         for SettingName in filter(None, InheritList):
71             try: InheritedSettings = self.context.getnamedvar(SettingName)
72             except AttributeError:
73                 if not Resilliant: raise
74                 else: InheritedSettings=None
75             if InheritedSettings is not None:
76                 settings.update(InheritedSettings)
77         return settings
78
79     def _SaveSettingsAdv(self, SettingName, settings):
80         settings = settings.copy()
81         # Prune out settings that shouldn't be saved
82         default = self.SettingsSaveOpt.get(None, 1)
83         for key in settings.keys():
84             if not self.SettingsSaveOpt.get(key, default):
85                 del settings[key]
86             elif isinstance(key, tuple):
87                 if not self.SettingsSaveOpt.get(key[-1], default):
88                     del settings[key]
89
90         # Save settings to be inherited
91         ctx = self.context._OwnerContext(self.BaseSettingsRoot, returnLast=1)
92         obj, setname = ctx.getnamedvar(SettingName, -1)
93         return setattr(obj, setname[0], settings)
94
95     def _SaveSettings(self, settings=None):
96         try: SettingName = self.settings[self.BaseSettingsSave]
97         except KeyError: pass
98         else: return self._SaveSettingsAdv(SettingName, settings)
99
100     def _InitSettings(self, settings, bAllowInheritance=1):
101         if not bAllowInheritance:
102             newsettings = settings
103         else:
104             # Get inherited settings
105             newsettings = {}
106             BaseSettingsList = self.default_settings.get(self.BaseSettingsInherit)
107             if BaseSettingsList:
108                 self._InheritSettings(newsettings, strtolist(BaseSettingsList), Resilliant=1)
109             BaseSettingsList = settings.get(self.BaseSettingsInherit)
110             if BaseSettingsList:
111                 self._InheritSettings(newsettings, strtolist(BaseSettingsList))
112
113             # Merge the two settings
114             if newsettings: newsettings.update(settings)
115             else: newsettings = settings
116
117             # Get the override load settings -- load them over top the passed settings
118             BaseSettingsList = self.default_settings.get(self.BaseSettingsLoad)
119             if BaseSettingsList:
120                 self._InheritSettings(newsettings, strtolist(BaseSettingsList), Resilliant=1)
121             BaseSettingsList = settings.get(self.BaseSettingsLoad)
122             if BaseSettingsList:
123                 self._InheritSettings(newsettings, strtolist(BaseSettingsList))
124
125         # now set our settings to be a chained grouping of
126         # the modified incoming settings, and the chained settings
127         self.settings = ChainedDict(newsettings)
128         self.settings.chained = self.default_settings
129
130         self._SaveSettings(self.source_settings)
131
132     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133     #~ Properties
134     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135
136     def _GetLocalSettings(self):
137         try:
138             result = {}
139             for each in self.settings:
140                 if not isinstance(each, tuple):
141                     result[each] = self.settings[each]
142             return result
143         except AttributeError: return {}
144     local_settings = property(fget=_GetLocalSettings)
145
146     def _GetSourceSettings(self):
147         try: return self.settings.source
148         except AttributeError: return self.settings
149     source_settings = property(fget=_GetSourceSettings)
150
Note: See TracBrowser for help on using the browser.