[Twisted-Python] Configuration: The Never Ending Story.

Moshe Zadka moshez at zadka.site.co.il
Fri May 11 14:00:12 EDT 2001

Thinking about how some UI for configuration would look like, namely command
line syntax, I have some rough sketch. I wrote this "as if" the configuration
interface I sketched earlier would actually exist. Note that the next UI
to come would force a refactoring to "general config ui utility functions"
and ui-specific functionality.

I hope this will concretize the discussion

First, some general words on how the ui will look like to the user:
the commands will be

> get <pickle file> <path>
> set <pickle file> <path> <string representation>
> array_add <pickle file> <path> <string representation>
> dict_add <pickle file> <path> <name> <string representation>

Wherever a parameter called "root" is mentioned, it will probably be
the pickle file. Note also that this is far for complete. And last
but not least, I didn't do the boring part of parsing the options
and dispatching to the functions -- fill this in from imagination.
Oh, and I obviously didn't test this wishful thinking code.

#command line configuration
def stringifyParameter(param):
    if isinstance(param, ArrayParameter):
        return "array of "+stringifyParameter(param.param)+'s'
    elif isinstance(param, DictParameter):
        return "folder of "+stringifyParameter(param.param)+'s'
    elif isinstance(param, StringParameter):
        return "string"
    elif isinstance(param, IntParameter):
        return "int"
    elif isinstance(param, InterfaceParameter):
        return param.interface.__name__

def convertString(type, s):
    if isinstance(type, ArrayParameter) or isinstance(type, DictParameter):
        raise ValueError("cannot set %s" % type.__class__.__name__)
    if isinstance(type, InterfaceParameter):
        obj = load(open(s))
        if type.interface not in obj.__implements__:
            raise ValueError(obj.__class__.__name__+" does not implement "+
        return obj
    if isinstance(type, StringParameter):
        return s
    if isinstance(type, IntParameter):
        return int(s)
    raise ValueError("unrecognized type" % type.__class__.__name__)

def traverseObject(root, path):
    path = string.split(path, '.')
    name = path[-1]
    obj = root
    for part in path[:-1]:
        if isinstance(obj, ArrayParameter):
            obj = obj.getValue()[int(part)]
        elif isinstance(obj, DictParameter):
            obj = obj.getValue()[part]
        elif isinstance(obj, InterfaceParameter):
            obj = obj.getValue().getParameters()[part]
    param = obj.getParameters()[name]
    return param

def setOption(root, path, value):
    param = traverseObject(root, path)
    value = convertString(param, value)
    obj.setAnswer(name, value)
    if hasattr(obj, 'endAnswer'):

def getOption(root, path):
    param = traverseObject(root, path)
    print stringifyParameter(param), param.getValue()

def appendToArray(root, path, value):
    param = traverseObject(root, path)
    assert isinstance(param, ArrayParameter)
    value = convertString(param.param, value)

def addToDict(root, path, name, value):
    param = traverseObject(root, path)
    assert isinstance(param, DictParameter)
    value = convertString(param.param, value)
    param.getValue()[name] = value
"I'll be ex-DPL soon anyway so I'm        |LUKE: Is Perl better than Python?
looking for someplace else to grab power."|YODA: No...no... no. Quicker,
   -- Wichert Akkerman (on debian-private)|      easier, more seductive.
For public key, finger moshez at debian.org  |http://www.{python,debian,gnu}.org

More information about the Twisted-Python mailing list