File indexing completed on 2025-01-19 03:59:59

0001 #!/usr/bin/env python3
0002 
0003 import re
0004 import sys
0005 import os
0006 import argparse
0007 sys.path.insert(0, os.path.join(
0008     os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
0009     "lib"
0010 ))
0011 from lottie.exporters import exporters
0012 from lottie.importers import importers
0013 from lottie.utils.stripper import float_strip, heavy_strip
0014 from lottie import __version__
0015 
0016 
0017 desc = """Converts between multiple formats
0018 
0019 Supported formats:
0020 
0021 - Input:
0022 %s
0023 
0024 - Output:
0025 %s
0026 """ % (
0027     "\n".join("%s- %s" % (" "*2, e.name) for e in importers),
0028     "\n".join("%s- %s" % (" "*2, e.name) for e in exporters),
0029 )
0030 
0031 parser = argparse.ArgumentParser(
0032     description=desc,
0033     formatter_class=argparse.RawTextHelpFormatter,
0034     conflict_handler='resolve'
0035 )
0036 parser.add_argument("--version", "-v", action="version", version="%(prog)s - python-lottie " + __version__)
0037 
0038 group = importers.set_options(parser)
0039 group.add_argument(
0040     "infile",
0041     default="-",
0042     nargs="?",
0043     help="Input file"
0044 )
0045 group.add_argument(
0046     "--input-format", "-if",
0047     default=None,
0048     choices=[importer.slug for importer in importers],
0049     help="Explicit output format (if missing implied by the input filename)",
0050 )
0051 
0052 
0053 group = exporters.set_options(parser)
0054 
0055 group.add_argument(
0056     "outfile",
0057     default="-",
0058     nargs="?",
0059     help="Output file"
0060 )
0061 group.add_argument(
0062     "--output-format", "-of",
0063     default=None,
0064     choices=[exporter.slug for exporter in exporters],
0065     help="Explicit output format (if missing implied by the output filename)",
0066 )
0067 group.add_argument(
0068     "--sanitize",
0069     default=False,
0070     action="store_true",
0071     help="Ensure the animation is 512x512 and 30 or 60 fps",
0072 )
0073 group.add_argument(
0074     "--optimize",
0075     "-O",
0076     default=1,
0077     type=int,
0078     choices=[0, 1, 2],
0079     help="Optimize the animation parameter:\n" +
0080          " * 0 no optimization\n" +
0081          " * 1 truncate floats\n" +
0082          " * 2 truncate floats and names"
0083 )
0084 group.add_argument(
0085     "--fps",
0086     default=None,
0087     type=int,
0088     help="If present, changes the output fps to match this value"
0089 )
0090 
0091 
0092 def print_dep_message(loader):
0093     if not loader.failed_modules:
0094         return
0095 
0096     sys.stderr.write("Make sure you have the correct dependencies installed\n")
0097 
0098     for failed, dep in loader.failed_modules.items():
0099         sys.stderr.write("For %s install %s\n" % (failed, dep))
0100 
0101 
0102 if __name__ == "__main__":
0103     ns = parser.parse_args()
0104     if ns.infile == "-" and not ns.input_format:
0105         parser.print_help()
0106 
0107     infile = ns.infile
0108     importer = None
0109     if infile == "-":
0110         infile = sys.stdin
0111     else:
0112         suf = os.path.splitext(infile)[1][1:]
0113         for p in importers:
0114             if suf in p.extensions:
0115                 importer = p
0116                 break
0117     if ns.input_format:
0118         importer = None
0119         for p in importers:
0120             if p.slug == ns.input_format:
0121                 importer = p
0122                 break
0123     if not importer:
0124         sys.stderr.write("Unknown importer\n")
0125         print_dep_message(importers)
0126         sys.exit(1)
0127 
0128     outfile = ns.outfile
0129     exporter = None
0130     if outfile == "-":
0131         outfile = sys.stdout
0132     else:
0133         exporter = exporters.get_from_filename(outfile)
0134     if ns.output_format:
0135         exporter = exporters.get(ns.output_format)
0136 
0137     if not exporter:
0138         sys.stderr.write("Unknown exporter\n")
0139         print_dep_message(exporters)
0140         sys.exit(1)
0141 
0142     i_options = {}
0143     for opt in importer.extra_options:
0144         i_options[opt.name] = getattr(ns, opt.nsvar(importer.slug))
0145 
0146     o_options = exporter.argparse_options(ns)
0147 
0148     an = importer.process(infile, **i_options)
0149 
0150     if ns.fps:
0151         an.frame_rate = ns.fps
0152 
0153     if ns.optimize == 1:
0154         float_strip(an)
0155     elif ns.optimize >= 2:
0156         heavy_strip(an)
0157 
0158     exporter.process(an, outfile, **o_options)