File indexing completed on 2025-01-19 03:59:55
0001 import os 0002 import sys 0003 import argparse 0004 import inspect 0005 from ..exporters import exporters 0006 from .stripper import float_strip 0007 0008 0009 def _get_caller(): 0010 return inspect.getmodule(inspect.currentframe().f_back.f_back) 0011 0012 0013 def _get_parser(caller, basename, path, formats, verbosity): 0014 if basename is None: 0015 basename = os.path.splitext(os.path.basename(caller.__file__))[0] 0016 0017 parser = argparse.ArgumentParser( 0018 conflict_handler='resolve' 0019 ) 0020 0021 parser.add_argument( 0022 "--name", 0023 "-n", 0024 default=basename, 0025 help="Output basename", 0026 ) 0027 parser.add_argument( 0028 "--path", 0029 default=path, 0030 help="Output path", 0031 ) 0032 parser.add_argument( 0033 "--formats", "-f", 0034 nargs="+", 0035 choices=list(sum((e.extensions for e in exporters), [])), 0036 default=formats, 0037 help="Formates to render", 0038 metavar="format" 0039 ) 0040 parser.add_argument( 0041 "--verbosity", 0042 type=int, 0043 default=int(verbosity) 0044 ) 0045 from .. import __version__ 0046 parser.add_argument( 0047 "--version", "-v", 0048 action="version", 0049 version="%(prog)s - python-lottie script " + __version__ 0050 ) 0051 exporters.set_options(parser) 0052 0053 return parser 0054 0055 0056 def get_parser(basename=None, path="/tmp", formats=["html"], verbosity=1): 0057 caller = _get_caller() 0058 return _get_parser(caller, basename, path, formats, verbosity) 0059 0060 0061 def run(animation, ns): 0062 for fmt in ns.formats: 0063 if ns.path == "" and ns.name == "-": 0064 outfile = sys.stdout 0065 else: 0066 absname = os.path.abspath(os.path.join(ns.path, ns.name + "." + fmt)) 0067 if ns.verbosity: 0068 sys.stderr.write("file://%s\n" % absname) 0069 outfile = absname 0070 exporter = exporters.get_from_extension(fmt) 0071 exporter.process(animation, outfile, **exporter.argparse_options(ns)) 0072 0073 0074 def script_main(animation, basename=None, path="/tmp", formats=["html"], verbosity=1, strip=float_strip): 0075 """ 0076 Sets up a script to output an animation into various formats 0077 """ 0078 caller = _get_caller() 0079 if caller and caller.__name__ == "__main__": 0080 parser = _get_parser(caller, basename, path, formats, verbosity) 0081 strip(animation) 0082 run(animation, parser.parse_args())