Skip to content

Commit

Permalink
Settings system. Now is probably the right moment to tell if you don'…
Browse files Browse the repository at this point in the history
…t like it :)
  • Loading branch information
enkore committed Feb 22, 2013
1 parent bf3b77d commit c967cde
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions i3pystatus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,49 @@
import time
from contextlib import contextmanager

class ConfigurationError(Exception):
def __init__(self, module, key=None, missing=None):
message = "Module '{0}'".format(module)
if key is not None:
message += ": invalid option '{0}'".format(key)
if missing is not None:
message += ": missing required options: {0}".format(missing)

super().__init__(message)

class Module:
output = None
position = 0
settings = tuple()
required = tuple()

def __init__(self, *args, **kwargs):
required = set()
self.required = set(self.required)

if len(args) == 1 and not len(kwargs):
# User can also pass in a dict for their settings
# Note: you could do that anyway, with the ** syntax
# Note2: just for backwards compatibility
kwargs = args[0]

for key, value in kwargs.items():
if key in self.settings:
setattr(self, key, value)
required.add(key)
else:
raise ConfigurationError(type(self).__name__, key=key)

required &= set(self.required)
if len(required) != len(self.required):
raise ConfigurationError(type(self).__name__, missing=self.required-required)

self.init()

def init(self):
"""Convenience method which is called after all settings are set
In case you don't want to type that super()…blabla :-)"""

def registered(self, status_handler):
"""Called when this module is registered with a status handler"""
Expand Down

0 comments on commit c967cde

Please sign in to comment.