From 2be70492beb7212fdc585601c9e9939be6803acd Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 29 Aug 2012 04:50:17 +0900 Subject: spec creation moved to JSONLeapConfig __metaclass__ now we expect spec to be a dict for all the JSONLeapConfig instances. --- src/leap/base/config.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'src/leap/base/config.py') 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 # -- cgit v1.2.3