Esempio n. 1
0
def main(args=tuple(sys.argv[1:])):
    """args: sequence of command line argument strings"""

    parser = build_argparser()
    parsed_args = parser.parse_args(args)

    # get all input paths, or exit with an error if there aren't any
    all_inpaths = glob_all(parsed_args.input_imxfiles)
    argparse_exit_if_no_paths(all_inpaths, progname=parser.prog)

    # create outdir if it doesn't exist (now that we know we have at least 1 input file)
    outdir = parsed_args.directory
    if outdir:
        os.makedirs(outdir, exist_ok=True)

    # process all input paths
    for inpath in all_inpaths:
        # print first part of the verbose message
        if parsed_args.verbose:
            print(f"converting {inpath!r} -> ", end="")

        with open(inpath, "rb") as imxfile:
            pixfmt = fast_imx_pixfmt(imxfile)
            newext = f"{_d}{pixfmt}{PNG_EXT}"
            outname = replaceext(os.path.basename(inpath), newext)
            outpath = os.path.join(outdir, outname)
            # print rest of the verbose message, now that we know outpath
            if parsed_args.verbose:
                print(f"{outpath!r}")

            imximage = read_imx(imxfile)

        write_to_png(imximage, outpath)
Esempio n. 2
0
def main(args=tuple(sys.argv[1:])):
    """args: sequence of command line argument strings"""

    parser = build_argparser()
    parsed_args = parser.parse_args(args)

    # get all input paths, or exit with an error if there aren't any
    all_inpaths = glob_all(parsed_args.input_pngfiles)
    argparse_exit_if_no_paths(all_inpaths, progname=parser.prog)

    # create outdir if it doesn't exist (now that we know we have at least 1 input file)
    outdir = parsed_args.directory
    if outdir:
        os.makedirs(outdir, exist_ok=True)

    # process all input paths
    for inpath in all_inpaths:

        # try to get pixel format from filename, e.g. filename.<pixfmt>.png
        pixfmt = os.path.splitext(os.path.splitext(inpath)[0])[1][1:]
        do_strip_pixfmt = parsed_args.strip_pixfmt_name and pixfmt in PIXEL_FORMATS
        if parsed_args.auto_pixfmt or pixfmt not in PIXEL_FORMATS:
            pixfmt = None

        # create output filename/path
        outname_noext = os.path.splitext(os.path.basename(inpath))[0]
        if do_strip_pixfmt:
            outname_noext = os.path.splitext(outname_noext)[0]
        outname = f"{outname_noext}{IMX_EXT}"
        outpath = os.path.join(outdir, outname)

        if parsed_args.verbose:
            print(f"converting {inpath!r} -> {outpath!r}")
        imximage = read_from_png(inpath, pixfmt=pixfmt)
        write_imx(imximage, outpath)
Esempio n. 3
0
def run_script(subsongtype, args):
    """run a subsong2xxx script with args (e.g. "wav" runs subsong2wav)

    subsongtype: a key from extutils.SUBSONG_FORMATS, e.g. "wav". Passing "wav" will run
      subsong2wav, and so on.
    args: sequence of command line argument strings, such as from sys.argv[1:]
    """
    parser = build_argparser_for_outformat(subsongtype)
    parsed_args = parser.parse_args(args)

    # get all input paths, or exit with an error if there aren't any
    all_inpaths = glob_all(parsed_args.input_subsongfiles)
    argparse_exit_if_no_paths(all_inpaths, progname=parser.prog)

    # create outdir if it doesn't exist (now that we know we have at least 1 input file)
    outdir = parsed_args.directory
    if outdir:
        os.makedirs(outdir, exist_ok=True)

    # process all input paths
    for inpath in all_inpaths:
        outpath = subsong_replaceext(inpath, subsongtype)
        outpath = os.path.join(outdir, os.path.basename(outpath))
        if parsed_args.verbose:
            print(f"converting {inpath!r} -> {outpath!r}")
        subsong = read_subsong(inpath)
        write_subsong(subsong, outpath)
Esempio n. 4
0
def main(args=tuple(sys.argv[1:])):
    """args: sequence of command line argument strings"""

    parser = build_argparser()
    parsed_args = parser.parse_args(args)

    # get all input paths, convert all dirs to *.XGM.toml paths if they contain any
    all_inpaths = glob_all(parsed_args.input_paths)
    all_inpaths_toml = glob_all_dirs_to_wildcards(all_inpaths,
                                                  XGMTOML_EXT_GLOB)

    # or exit with an error if there aren't any matching paths
    errormessage = (
        f"error: No {XGMTOML_EXT} files to process. You may have specified directories "
        "that don't contain any, or a wildcard that doesn't match any.")
    argparse_exit_if_no_paths(all_inpaths_toml,
                              progname=parser.prog,
                              errormessage=errormessage)

    # create outdir if it doesn't exist (now that we know we have at least 1 input file)
    outdir = parsed_args.directory
    if outdir:
        os.makedirs(outdir, exist_ok=True)

    if parsed_args.verbose:
        # noinspection PyUnusedLocal
        def verbosefunc(contentsidx, num_contents, contentfile):
            print(f"  {contentfile.name16!r} ->")

    else:
        verbosefunc = None

    # process all input paths
    for inpath in all_inpaths_toml:
        # goal: in.XGM.toml -> in.XGM
        output_name = replaceext(os.path.basename(inpath), XGM_EXT,
                                 XGMTOML_EXT)
        output_path = os.path.join(outdir, output_name)

        # pack XGM container file
        if parsed_args.verbose:
            print(f"packing {inpath!r} -> {output_path!r}")
        xgm = read_toml(inpath)
        write_xgm(xgm, output_path, progressfunc=verbosefunc)