python-lottie  0.6.11+devcecd248
A framework to work with lottie files and telegram animated stickers (tgs)
script.py
Go to the documentation of this file.
1 import os
2 import sys
3 import argparse
4 import inspect
5 from ..exporters import exporters
6 from .stripper import float_strip
7 
8 
9 def _get_caller():
10  return inspect.getmodule(inspect.currentframe().f_back.f_back)
11 
12 
13 def _get_parser(caller, basename, path, formats, verbosity):
14  if basename is None:
15  basename = os.path.splitext(os.path.basename(caller.__file__))[0]
16 
17  parser = argparse.ArgumentParser(
18  conflict_handler='resolve'
19  )
20 
21  parser.add_argument(
22  "--name",
23  "-n",
24  default=basename,
25  help="Output basename",
26  )
27  parser.add_argument(
28  "--path",
29  default=path,
30  help="Output path",
31  )
32  parser.add_argument(
33  "--formats", "-f",
34  nargs="+",
35  choices=list(sum((e.extensions for e in exporters), [])),
36  default=formats,
37  help="Formats to render",
38  metavar="format"
39  )
40  parser.add_argument(
41  "--verbosity",
42  type=int,
43  default=int(verbosity)
44  )
45  from .. import __version__
46  parser.add_argument(
47  "--version", "-v",
48  action="version",
49  version="%(prog)s - python-lottie script " + __version__
50  )
51  exporters.set_options(parser)
52 
53  return parser
54 
55 
56 def get_parser(basename=None, path="/tmp", formats=["html"], verbosity=1):
57  caller = _get_caller()
58  return _get_parser(caller, basename, path, formats, verbosity)
59 
60 
61 def run(animation, ns):
62  for fmt in ns.formats:
63  if ns.path == "" and ns.name == "-":
64  outfile = sys.stdout
65  else:
66  absname = os.path.abspath(os.path.join(ns.path, ns.name + "." + fmt))
67  if ns.verbosity:
68  sys.stderr.write("file://%s\n" % absname)
69  outfile = absname
70  exporter = exporters.get_from_extension(fmt)
71  exporter.process(animation, outfile, **exporter.argparse_options(ns))
72 
73 
74 def script_main(animation, basename=None, path="/tmp", formats=["html"], verbosity=1, strip=float_strip):
75  """
76  Sets up a script to output an animation into various formats
77  """
78  caller = _get_caller()
79  if caller and caller.__name__ == "__main__":
80  parser = _get_parser(caller, basename, path, formats, verbosity)
81  strip(animation)
82  run(animation, parser.parse_args())
lottie.utils.script.script_main
def script_main(animation, basename=None, path="/tmp", formats=["html"], verbosity=1, strip=float_strip)
Definition: script.py:74
lottie.utils.script.run
def run(animation, ns)
Definition: script.py:61
lottie.utils.script.get_parser
def get_parser(basename=None, path="/tmp", formats=["html"], verbosity=1)
Definition: script.py:56