This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish/subscribe implemented for config
- Loading branch information
1 parent
ba50185
commit f64331a
Showing
8 changed files
with
104 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from UserDict import IterableUserDict | ||
from configobj import ConfigObj | ||
|
||
class ConfigObserver(ConfigObj): | ||
def __init__(self, *args, **kw): | ||
self.observers = [] | ||
self.observeKeys = {} | ||
|
||
self.validator = None | ||
self.validating = False | ||
super(ConfigObserver, self).__init__(*args, **kw) | ||
|
||
def setValidator(self, validator): | ||
self.validator = validator | ||
|
||
def observe(self, observer): | ||
self.observers.append(observer) | ||
|
||
def observeKey(self, key, observer): | ||
if not key in self.observeKeys: | ||
self.observeKeys[key] = [] | ||
self.observeKeys[key].append(observer) | ||
|
||
def notify(self, key): | ||
for o in self.observers: | ||
o.notify(self, key) | ||
|
||
def notifyKey(self, key): | ||
self.notify(key) | ||
if key in self.observeKeys: | ||
for o in self.observeKeys[key]: | ||
o.notify(self, key) | ||
|
||
def __setitem__(self, key, value, unrepr = False): | ||
currentValue = '' | ||
if key in self: | ||
currentValue = self[key] | ||
|
||
ConfigObj.__setitem__(self, key, value, unrepr) | ||
if currentValue == value: return | ||
|
||
result = True | ||
if self.validator: | ||
if not self.validating: | ||
self.validating = True | ||
result = self.validate(self.validator, preserve_errors = True) | ||
|
||
if result and self.validating: | ||
self.notifyKey(key) | ||
self.validating = False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters