'Resolve option profile (e.g. prepopulate)
@param options: the template options as dict like:
{"name": ("item1", "item2",...),...},
The "mandatory" list will always be
added, while the "default" list will
be added only if setting is None.
@param setting: the active setting, as single item
or tuple/list, items with a "template:"
prefix (like "template:name") refer to
the respective list in options
@param resolved: internal (for recursion)
@example:
# Template provides:
settings.base.prepopulate_options = {
"mandatory": "locations/intl",
"brazil": "locations/brazil",
"germany": "locations/germany",
"default": "default",
"demo": ("template:default", "demo/users"),
# Set up a demo for Brazil:
settings.base.prepopulate = ("template:brazil", "template:demo")
# result:
["locations/intl", "locations/brazil", "default", "demo/users"]
# Set up a production instance for Germany:
settings.base.prepopulate = ("template:germany", "template:default")
# result:
["locations/intl", "locations/germany", "default"]
# Default setup:
settings.base.prepopulate = None
# result:
["locations/intl", "default"]
# Custom options:
settings.base.prepopulate = ["template:demo", "IFRC/Train"]
# result:
["locations/intl", "default", "demo/users", "IFRC/Train"]
@note: the result list is deduplicated, maintaining the original
order by first occurrence'

def resolve_profile(self, options, setting, resolved=None):

    default = (resolved is None)
    if default:
        resolved = set()
    seen = resolved.add
    resolve = self.resolve_profile
    result = []
    def append(item):
        if (item not in resolved):
            seen(item)
            if (isinstance(item, basestring) and (item[:9] == 'template:')):
                if options:
                    option = options.get(item[9:])
                    if option:
                        result.extend(resolve(options, option, resolved=resolved))
            else:
                result.append(item)
        return
    if default:
        if ('default/base' in setting):
            append('default/base')
        if options:
            append('template:mandatory')
    if (setting is not None):
        if (not isinstance(setting, (tuple, list))):
            setting = (setting,)
        for item in setting:
            append(item)
    elif default:
        append('template:default')
    return result
