| 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 Foundation.ChainedDict import ChainedDict |
|---|
| 27 |
from Foundation.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 |
BaseSettingPrefix = "SkinSettings" |
|---|
| 41 |
BaseSettingsSave = (BaseNamespace, 'save') |
|---|
| 42 |
BaseSettingsInherit = (BaseNamespace, 'inherit') |
|---|
| 43 |
BaseSettingsLevel = (BaseNamespace, 'level') |
|---|
| 44 |
BaseSettingsRoot = (BaseNamespace, 'root') |
|---|
| 45 |
|
|---|
| 46 |
default_settings = { |
|---|
| 47 |
BaseSettingsSave:'', |
|---|
| 48 |
BaseSettingsInherit:'', |
|---|
| 49 |
BaseSettingsLevel:'0', |
|---|
| 50 |
BaseSettingsRoot:'0', |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 54 |
#~ Protected Methods |
|---|
| 55 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 56 |
|
|---|
| 57 |
def _InheritSettings(self, settings, InheritList, SettingPrefix=None, Resilliant=0): |
|---|
| 58 |
SettingPrefix = SettingPrefix or self.BaseSettingPrefix |
|---|
| 59 |
InheritList.reverse() |
|---|
| 60 |
for SettingName in InheritList: |
|---|
| 61 |
SettingName = SettingPrefix + SettingName |
|---|
| 62 |
try: InheritedSettings = getattr(self.context, SettingName, None) |
|---|
| 63 |
except AttributeError: |
|---|
| 64 |
if not Resilliant: raise |
|---|
| 65 |
if InheritedSettings is not None: |
|---|
| 66 |
settings.update(InheritedSettings) |
|---|
| 67 |
return settings |
|---|
| 68 |
|
|---|
| 69 |
def _SaveSettingsAdv(self, SettingName, settings=None, SettingPrefix=None, SettingLevel=None): |
|---|
| 70 |
settings = settings or self.local_settings |
|---|
| 71 |
|
|---|
| 72 |
# Save settings to be inherited |
|---|
| 73 |
SettingPrefix = SettingPrefix or self.BaseSettingPrefix |
|---|
| 74 |
SettingName = SettingPrefix + SettingName |
|---|
| 75 |
|
|---|
| 76 |
if SettingLevel is not None: |
|---|
| 77 |
ctx = self.context._GetContext(SettingLevel) |
|---|
| 78 |
else: ctx = self.context._OwnerContext(self.BaseSettingsRoot, returnLast=1) |
|---|
| 79 |
|
|---|
| 80 |
return setattr(ctx, SettingName, settings) |
|---|
| 81 |
|
|---|
| 82 |
def _SaveSettings(self, settings=None, SettingPrefix=None): |
|---|
| 83 |
settings = settings or self.local_settings |
|---|
| 84 |
if self.BaseSettingsLevel in settings: |
|---|
| 85 |
SettingLevel = int(settings[self.BaseSettingsLevel]) |
|---|
| 86 |
del settings[self.BaseSettingsLevel] |
|---|
| 87 |
else: SettingLevel = None |
|---|
| 88 |
if self.BaseSettingsSave in settings: |
|---|
| 89 |
SettingName = settings[self.BaseSettingsSave] |
|---|
| 90 |
del settings[self.BaseSettingsSave] |
|---|
| 91 |
return self._SaveSettingsAdv(SettingName, settings, SettingPrefix, SettingLevel) |
|---|
| 92 |
|
|---|
| 93 |
def _InitSettings(self, settings, bAllowInheritance=1): |
|---|
| 94 |
if not bAllowInheritance: |
|---|
| 95 |
newsettings = settings |
|---|
| 96 |
else: |
|---|
| 97 |
# Get inherited settings |
|---|
| 98 |
newsettings = {} |
|---|
| 99 |
BaseSettingsList = self.default_settings.get(self.BaseSettingsInherit, '') |
|---|
| 100 |
if BaseSettingsList: self._InheritSettings(newsettings, strtolist(BaseSettingsList), Resilliant=1) |
|---|
| 101 |
BaseSettingsList = settings.get(self.BaseSettingsInherit, '') |
|---|
| 102 |
if BaseSettingsList: self._InheritSettings(newsettings, strtolist(BaseSettingsList)) |
|---|
| 103 |
|
|---|
| 104 |
# Merge the two settings |
|---|
| 105 |
if newsettings: newsettings.update(settings) |
|---|
| 106 |
else: newsettings = settings |
|---|
| 107 |
|
|---|
| 108 |
if self.BaseSettingsInherit in newsettings: |
|---|
| 109 |
del newsettings[self.BaseSettingsInherit] |
|---|
| 110 |
|
|---|
| 111 |
self._SaveSettings(newsettings) |
|---|
| 112 |
|
|---|
| 113 |
# now set our settings to be a chained grouping of |
|---|
| 114 |
# the modified incoming settings, and the chained settings |
|---|
| 115 |
self.settings = ChainedDict(newsettings) |
|---|
| 116 |
self.settings.chained = self.default_settings |
|---|
| 117 |
|
|---|
| 118 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 119 |
#~ Properties |
|---|
| 120 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 121 |
|
|---|
| 122 |
def _GetLocalSettings(self): |
|---|
| 123 |
try: |
|---|
| 124 |
result = {} |
|---|
| 125 |
for each in self.settings: |
|---|
| 126 |
if not isinstance(each, tuple): |
|---|
| 127 |
result[each] = self.settings[each] |
|---|
| 128 |
return result |
|---|
| 129 |
except AttributeError: return {} |
|---|
| 130 |
local_settings = property(fget=_GetLocalSettings) |
|---|
| 131 |
|
|---|
| 132 |
def _GetSourceSettings(self): |
|---|
| 133 |
try: return self.settings.source |
|---|
| 134 |
except AttributeError: return self.settings |
|---|
| 135 |
source_settings = property(fget=_GetSourceSettings) |
|---|
| 136 |
|
|---|