def set_node_by_xpath(mapping, xpath, value, extend=False, setter_attr=None):
    """Set the node pointed to by xpath from mapping.

    Args:
        mapping: nested dictionary.
        xpath: string-like selector.
        value: value to set.
        extend: if True, create the nested structure if it doesn't exist,
            otherwise, raise an exception.
        setter_attr: use a special setter method attribute in mapping, instead of replacing
                     the node by the new value (note: do not use a property setter attribute)

    Example:

    >>> tree = {'level1': {'level2': {'level3': 'bottom'}}}
    >>> set_node_by_xpath(tree, 'level1.level2.level3', 'bottom')

    """
    segments = xpath.split('.')
    attrname = segments.pop()
    for segment in segments:
        if segment not in mapping:
            if not extend:
                raise KeyError(
                    "Invalid '{}' selector: '{}' doesn't match anything.".
                    format(xpath, segment))
            mapping[segment] = {}
        mapping = mapping[segment]
    if setter_attr:
        setter = getattr(mapping[attrname], setter_attr)
        setter(value)
    else:
        mapping[attrname] = value
