def smart_ext(name, ext=None):
    """
    Smartly add extention to the file name

    Usage::

        >>> smart_ext("config")
        "config"
        >>> smart_ext("config.py")
        "config.py"
        >>> smart_ext("config", ext="py")
        "config.py"
        >>> smart_ext("config.py", ext="py")
        "config.py"

    """
    if ext is None:
        return name
    ext = '.%s' % ext if not ext.startswith('.') else ext
    name = name.strip()
    existing_ext = name.rsplit('.', 1)[-1]
    if existing_ext == name:
        return '%s%s' % (name, ext)
    return name
