# Buggy code
def main(args, error):
    """CLI entry point for single file conversions"""
    del error  # unused

    if args.interfac:
        interfacfile = args.interfac
    else:
        # if no interfac was given, assume
        # the same path of the drs archive.
        drspath = os.path.split(args.drs)[0]
        interfacfile = os.path.join(drspath, "interfac.drs")

    # if .png was passed, strip it away.
    if args.output.endswith(".png"):
        args.output = args.output[:-4]

    # here, try opening slps from interfac or whereever
    info("opening slp in drs '%s:%s'..." % (args.drs, args.slp))
    slpfile = DRS(open(args.drs, "rb")).root[args.slp].open("rb")

    # import here to prevent that the __main__ depends on SLP
    # just by importing this singlefile.py.
    from .slp import SLP

    # parse the slp image
    info("parsing slp image...")
    slpimage = SLP(slpfile.read())

    # open color palette
    info("opening palette in drs '%s:%s.bina'..." % (interfacfile, args.palette))
    palettefile = (
        DRS(open(interfacfile, "rb")).root["%s.bina" % args.palette].open("rb")
    )
    info("parsing palette data...")
    palette = ColorTable(palettefile.read())

    # create texture
    info("packing texture...")
    tex = Texture(slpimage, palette)

    # to save as png:
    path, filename = os.path.split(args.output)
    tex.save(Directory(path).root, filename)


# 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 main(args, error):
    """CLI entry point for single file conversions"""
    del error  # unused

    drspath = Path(args.drs.name)
    outputpath = Path(args.output)

    if args.interfac:
        interfacfile = args.interfac
    else:
        # if no interfac was given, assume
        # the same path of the drs archive.

        interfacfile = drspath.with_name("interfac.drs").open("rb")  # pylint: disable=no-member

    # here, try opening slps from interfac or whereever
    info("opening slp in drs '%s:%s'..." % (drspath, args.slp))
    slpfile = DRS(args.drs).root[args.slp].open("rb")

    # import here to prevent that the __main__ depends on SLP
    # just by importing this singlefile.py.
    from .slp import SLP

    # parse the slp image
    info("parsing slp image...")
    slpimage = SLP(slpfile.read())

    # open color palette
    info("opening palette in drs '%s:%s.bina'..." % (interfacfile.name, args.palette))
    palettefile = DRS(interfacfile).root["%s.bina" % args.palette].open("rb")

    info("parsing palette data...")
    palette = ColorTable(palettefile.read())

    # create texture
    info("packing texture...")
    tex = Texture(slpimage, palette)

    # to save as png:
    tex.save(Directory(outputpath.parent).root, outputpath.name)