Source

trac-basic-macros / trac-dev / tracbm / tracbm / config.py

Full commit

# Copyright 2009-2011 Olemis Lang <olemis at gmail.com>
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


r"""Insert configuration options in wiki pages

Copyright 2009-2011 Olemis Lang <olemis at gmail.com>
Licensed under the Apache License
"""
__author__ = 'Olemis Lang'

from trac.core import TracError
from trac.util.text import to_unicode
from trac.wiki.api import parse_args
from trac.wiki.macros import WikiMacroBase

from genshi.builder import tag

class ConfigOptionMacro(WikiMacroBase):
    r"""Insert configuration options in Wiki pages
    
    Usage is as follows.
    
    `[[ConfigOption(section, name, type=option_type, sep=char)]]`
    
    ''section'' :  the section in trac.ini (mandatory)
    ''name'' :     option name (mandatory)
    ''type'' :     if this keyword argument is present then 
                   the value will be rendered using an
                   appropriate format according to the option 
                   type. Supported values are `text` (default),
                   `bool`, `int`, `list`, `path`, `extension`.
    ''sep'' :      list separator (otional, ignored if 
                   `type`! = `list`)
    """
    def expand_macro(self, formatter, name, content):
        # TODO: Implement `type` and `sep` options
        # TODO: Permissions for sections and individual options
        args, kw = parse_args(content)
        opt_type, sep = [kw.get(k) for k in ('type', 'sep')]
        try :
            value = self.env.config.get(args[0], args[1])
        except IndexError :
            raise TracError('Section and option name are both needed')
        else :
            return value