diff options
author | kali <kali@leap.se> | 2012-08-29 04:50:17 +0900 |
---|---|---|
committer | kali <kali@leap.se> | 2012-08-29 04:50:17 +0900 |
commit | 2be70492beb7212fdc585601c9e9939be6803acd (patch) | |
tree | 5f0e8e0bf3a8977254684e1cb41e962553692fca /src/leap/base/config.py | |
parent | 7a8f4db1a4743582c34a52ab448eece0e7689bc8 (diff) |
spec creation moved to JSONLeapConfig __metaclass__
now we expect spec to be a dict for all the JSONLeapConfig instances.
Diffstat (limited to 'src/leap/base/config.py')
-rw-r--r-- | src/leap/base/config.py | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/leap/base/config.py b/src/leap/base/config.py index 93a89638..1ced471b 100644 --- a/src/leap/base/config.py +++ b/src/leap/base/config.py @@ -1,7 +1,6 @@ """ Configuration Base Class """ -import configuration # python configuration module, not local! import grp import json import logging @@ -12,6 +11,8 @@ import os logger = logging.getLogger(name=__name__) logger.setLevel('DEBUG') +import configuration + from leap.base import exceptions from leap.base import constants from leap.util.fileutil import (mkdir_p) @@ -45,8 +46,54 @@ class BaseLeapConfig(object): raise NotImplementedError("abstract base class") +class MetaConfigWithSpec(type): + """ + metaclass for JSONLeapConfig classes. + It creates a configuration spec out of + the `spec` dictionary. + """ + # XXX in the near future, this is the + # place where we want to enforce + # singletons, read-only and stuff. + def __new__(meta, classname, bases, classDict): + spec_options = classDict.get('spec', None) + # XXX if not spec_options, raise BadConfiguration or something + # we create a configuration spec attribute from the spec dict + config_class = type( + classname + "Spec", + (configuration.Configuration, object), + {'options': spec_options}) + classDict['spec'] = config_class + + return type.__new__(meta, classname, bases, classDict) + +########################################################## +# hacking in progress: + +# Configs have: +# - a slug (from where a filename/folder is derived) +# - a spec (for validation and defaults). +# this spec is basically a dict that will be used +# for type casting and validation, and defaults settings. + +# all config objects, since they are derived from BaseConfig, implement basic +# useful methods: +# - save +# - load +# - get_config (returns a optparse.OptionParser object) + +# TODO: +# - have a good type cast repertory (uris, version, hashes...) +# - raise validation errors +# - multilingual objects + +########################################################## + + class JSONLeapConfig(BaseLeapConfig): + __metaclass__ = MetaConfigWithSpec + def __init__(self, *args, **kwargs): # sanity check assert self.slug is not None @@ -94,6 +141,7 @@ class JSONLeapConfig(BaseLeapConfig): config_file = get_config_file(filename, folder) return config_file + # # utility functions # |