python-lottie  0.7.0+dev351ce82
A framework to work with lottie files and telegram animated stickers (tgs)
core.py
Go to the documentation of this file.
1 import sys
2 import json
3 import gzip
4 import codecs
5 
6 from .base import exporter
7 from ..utils.file import open_file
8 from ..parsers.baseporter import ExtraOption
9 from .tgs_validator import TgsValidator
10 
11 
12 @exporter("Lottie JSON", ["json"], [], {"pretty"}, "lottie")
13 def export_lottie(animation, file, pretty=False):
14  with open_file(file) as fp:
15  kw = {}
16  if pretty:
17  kw = dict(indent=4)
18  json.dump(animation.to_dict(), fp, **kw)
19 
20 
21 @exporter("Telegram Animated Sticker", ["tgs"], [ ExtraOption("no_sanitize", help="Disable Sticker fit", action="store_false", dest="sanitize"),
22  ExtraOption("no_validate", help="Disable feature validation", action="store_false", dest="validate"),
23 ])
24 def export_tgs(animation, file, sanitize=False, validate=False):
25  if sanitize:
26  animation.tgs_sanitize()
27 
28  with gzip.open(file, "wb") as gzfile:
29  lottie_dict = animation.to_dict()
30  lottie_dict["tgs"] = 1
31  json.dump(lottie_dict, codecs.getwriter('utf-8')(gzfile))
32 
33  if validate:
34  validator = TgsValidator()
35  validator(animation)
36  validator.check_file_size(file)
37  if validator.errors:
38  sys.stdout.write("\n".join(map(str, validator.errors))+"\n")
39 
40 
41 class HtmlOutput:
42  def __init__(self, animation, file):
43  self.animationanimation = animation
44  self.filefile = file
45 
46  def style(self):
47  self.filefile.write("""
48  <style>
49  #bodymovin { width: %spx; height: %spx; margin: auto;
50  background-color: white;
51  background-size: 64px 64px;
52  background-image:
53  linear-gradient(to right, rgba(0, 0, 0, .3) 50%%, transparent 50%%),
54  linear-gradient(to bottom, rgba(0, 0, 0, .3) 50%%, transparent 50%%),
55  linear-gradient(to bottom, white 50%%, transparent 50%%),
56  linear-gradient(to right, transparent 50%%, rgba(0, 0, 0, .5) 50%%);
57  }
58  </style>
59  <script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.5.3/lottie.js"></script>
60  """ % (self.animationanimation.width, self.animationanimation.height))
61 
62  def body_pre(self):
63  self.filefile.write("""
64 <div id="bodymovin"></div>
65 
66 <script>
67  var animData = {
68  container: document.getElementById('bodymovin'),
69  renderer: 'svg',
70  loop: true,
71  autoplay: true,
72  """)
73 
74  def body_embedded(self):
75  self.filefile.write("animationData: ")
76  export_lottie(self.animationanimation, self.filefile, True)
77 
78  def body_post(self):
79  self.filefile.write("""
80  };
81  var anim = bodymovin.loadAnimation(animData);
82 </script>""")
83 
84  def html_begin(self):
85  self.filefile.write("""<!DOCTYPE html>
86 <html>
87 <head>
88  <meta charset="utf-8" />
89  <style>
90  html, body { width: 100%; height: 100%; margin: 0; }
91  body { display: flex; }
92  </style>""")
93  self.stylestyle()
94  self.filefile.write("</head><body>")
95 
96  def html_end(self):
97  self.filefile.write("</body></html>")
98 
99 
100 @exporter("Lottie HTML", ["html", "htm"])
101 def export_embedded_html(animation, file):
102  with open_file(file) as fp:
103  out = HtmlOutput(animation, fp)
104  out.html_begin()
105  out.body_pre()
106  out.body_embedded()
107  out.body_post()
108  out.html_end()
109 
110 
111 def export_linked_html(animation, file, path):
112  with open_file(file) as fp:
113  out = HtmlOutput(animation, fp)
114  out.html_begin()
115  out.body_pre()
116  file.write("path: %r" % path)
117  out.body_post()
118  out.html_end()
119 
def __init__(self, animation, file)
Definition: core.py:43
def export_embedded_html(animation, file)
Definition: core.py:102
def export_linked_html(animation, file, path)
Definition: core.py:112
def export_lottie(animation, file, pretty=False)
Definition: core.py:13
def export_tgs(animation, file, sanitize=False, validate=False)
Definition: core.py:25
def open_file(file_or_name, mode="w")
Definition: file.py:5