# Buggy code
def get_config_path(args):
    """
    Locates the main configuration file by name in some searchpaths.
    """

    # if we're in devmode, use only the build source config folder
    if config.DEVMODE:
        return Directory(os.path.join(config.BUILD_SRC_DIR, "cfg")).root

    # else, mount the possible locations in an union
    # to overlay the global dir and the user dir.
    result = Union().root

    # mount the global config dir
    # we don't use xdg config_dirs because that would be /etc/xdg/openage
    # and nobody would ever look in there.
    global_configs = pathlib.Path(config.GLOBAL_CONFIG_DIR)
    if global_configs.is_dir():
        result.mount(WriteBlocker(Directory(global_configs).root).root)

    # then the per-user config dir (probably ~/.config/openage)
    home_cfg = default_dirs.get_dir("config_home") / "openage"
    result.mount(Directory(home_cfg, create_if_missing=True).root)

    # the optional command line argument overrides it all
    if args.cfg_dir:
        result.mount(Directory(args.cfg_dir).root)

    return result


# Traceback (ValueError)
dev@openage:~/openage$ python3 -m openage convert-file /home/dev/aoe1/data/Border.drs 20000.slp /home/dev/aoe1/data/test.png
INFO opening slp in drs '/home/dev/aoe1/data/Border.drs:20000.slp'...
INFO parsing slp image...
INFO opening palette in drs '/home/dev/aoe1/data/interfac.drs:50500.bin'...
INFO parsing palette data...
INFO packing texture...
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/dev/openage/openage/__main__.py", line 128, in <module>
exit(main())
File "/home/dev/openage/openage/__main__.py", line 121, in main
return args.entrypoint(args, cli.error)
File "/home/dev/openage/openage/convert/singlefile.py", line 72, in main
tex.save(Directory(path).root, filename)
File "/home/dev/openage/openage/convert/texture.py", line 170, in save
raise ValueError("Texture must be saved as name.png. got: %s" % filename)
ValueError: Texture must be saved as name.png. got: test

# Fixed code
def get_config_path(custom_cfg_dir=None):
    """
    Locates the main configuration file by name in some searchpaths.
    Optionally, mount a custom directory with highest priority.
    """

    # if we're in devmode, use only the build source config folder
    if config.DEVMODE:
        return Directory(os.path.join(config.BUILD_SRC_DIR, "cfg")).root

    # else, mount the possible locations in an union
    # to overlay the global dir and the user dir.
    result = Union().root

    # mount the global config dir
    # we don't use xdg config_dirs because that would be /etc/xdg/openage
    # and nobody would ever look in there.
    global_configs = pathlib.Path(config.GLOBAL_CONFIG_DIR)
    if global_configs.is_dir():
        result.mount(WriteBlocker(Directory(global_configs).root).root)

    # then the per-user config dir (probably ~/.config/openage)
    home_cfg = default_dirs.get_dir("config_home") / "openage"
    result.mount(Directory(home_cfg, create_if_missing=True).root)

    # the optional command line argument overrides it all
    if custom_cfg_dir:
        result.mount(Directory(custom_cfg_dir).root)

    return result