python-lottie  0.6.11+devcecd248
A framework to work with lottie files and telegram animated stickers (tgs)
baseporter.py
Go to the documentation of this file.
1 import sys
2 import os
3 import pkgutil
4 import argparse
5 import importlib
6 
7 
8 class Baseporter:
9  def __init__(self, name, extensions, callback, extra_options=[], generic_options=set(), slug=None):
10  self.name = name
11  self.extensions = extensions
12  self.callback = callback
13  self.extra_options = extra_options
14  self.generic_options = generic_options
15  self.slug = slug if slug is not None else extensions[0]
16 
17  def process(self, *a, **kw):
18  return self.callback(*a, **kw)
19 
20  def __repr__(self):
21  return "<%s %s>" % (self.__class__.__name__, self.slug)
22 
23  def argparse_options(self, ns):
24  o_options = {}
25  for opt in self.extra_options:
26  o_options[opt.dest] = getattr(ns, opt.nsvar(self.slug))
27  for opt in self.generic_options:
28  o_options[opt] = getattr(ns, opt)
29  return o_options
30 
31 
33  def __init__(self, name, **kwargs):
34  self.name = name
35  self.kwargs = kwargs
36  if "action" not in self.kwargs:
37  self.kwargs["metavar"] = self.name
38  self.dest = kwargs.pop("dest", name)
39 
40  def add_argument(self, slug, parser):
41  opt = "--%s-%s" % (slug, self.name.replace("_", "-"))
42  parser.add_argument(opt, dest=self.nsvar(slug), **self.kwargs)
43 
44  def nsvar(self, slug):
45  return "%s_%s" % (slug, self.dest)
46 
47 
48 def _add_options(parser, ie, object):
49  if not object.extra_options:
50  return
51 
52  suf = " %sing options" % ie
53  group = parser.add_argument_group(object.name + suf)
54  for op in object.extra_options:
55  op.add_argument(object.slug, group)
56 
57 
58 class Loader:
59  def __init__(self, module_path, module_name, ie):
60  self._loaded = False
61  self._registry = {}
62  self._module_path = os.path.dirname(module_path)
63  self._module_name = module_name.replace(".base", "")
64  self._ie = ie
65  self._failed = {}
66 
67  def load_modules(self):
68  self._loaded = True
69 
70  for _, modname, _ in pkgutil.iter_modules([self._module_path]):
71  if modname == "base":
72  continue
73 
74  full_modname = "." + modname
75  try:
76  importlib.import_module(full_modname, self._module_name)
77  except ImportError as e:
78  self._failed[modname] = e.name
79 
80  @property
81  def failed_modules(self):
82  if not self._loaded:
83  self.load_modules()
84 
85  return self._failed
86 
87  @property
88  def items(self):
89  if not self._loaded:
90  self.load_modules()
91  return self._registry
92 
93  def __iter__(self):
94  return iter(self.items.values())
95 
96  def get(self, slug):
97  return self.items.get(slug, None)
98 
99  def __getitem__(self, key):
100  return self.get(key)
101 
102  def get_from_filename(self, filename):
103  return self.get_from_extension(os.path.splitext(filename)[1][1:])
104 
105  def get_from_extension(self, ext):
106  for p in self.items.values():
107  if ext in p.extensions:
108  return p
109  return None
110 
111  def set_options(self, parser):
112  for exporter in self.items.values():
113  _add_options(parser, self._ie, exporter)
114 
115  def keys(self):
116  return self.items.keys()
117 
118  def decorator(self, name, extensions, extra_options=[], generic_options=set(), slug=None):
119  def decorator(callback):
120  porter = Baseporter(name, extensions, callback, extra_options, generic_options, slug)
121  self._registry[porter.slug] = porter
122  return callback
123  return decorator
124 
125 
127  def report_progress(self, title, value, total):
128  sys.stderr.write("\r%s %s/%s" % (title, value, total))
129  sys.stderr.flush()
130 
131  def report_message(self, message):
132  sys.stderr.write("\r" + message + "\n")
133  sys.stderr.flush()
134 
135 
136 IoProgressReporter.instance = IoProgressReporter()
137 
138 
140  return IoProgressReporter.instance
lottie.parsers.baseporter.ExtraOption.kwargs
kwargs
Definition: baseporter.py:35
lottie.parsers.baseporter.Baseporter.name
name
Definition: baseporter.py:10
lottie.parsers.baseporter.Baseporter.argparse_options
def argparse_options(self, ns)
Definition: baseporter.py:23
lottie.parsers.baseporter.Loader._ie
_ie
Definition: baseporter.py:64
lottie.parsers.baseporter.Loader._loaded
_loaded
Definition: baseporter.py:60
lottie.parsers.baseporter.Loader._registry
_registry
Definition: baseporter.py:61
lottie.parsers.baseporter.ExtraOption.dest
dest
Definition: baseporter.py:38
lottie.parsers.baseporter.ExtraOption.__init__
def __init__(self, name, **kwargs)
Definition: baseporter.py:33
lottie.parsers.baseporter.Loader.__init__
def __init__(self, module_path, module_name, ie)
Definition: baseporter.py:59
lottie.parsers.baseporter.Loader.get_from_extension
def get_from_extension(self, ext)
Definition: baseporter.py:105
lottie.parsers.baseporter.Baseporter.extra_options
extra_options
Definition: baseporter.py:13
lottie.parsers.baseporter.Baseporter.slug
slug
Definition: baseporter.py:15
lottie.parsers.baseporter.Baseporter.callback
callback
Definition: baseporter.py:12
lottie.parsers.baseporter.Baseporter
Definition: baseporter.py:8
lottie.parsers.baseporter.Baseporter.__init__
def __init__(self, name, extensions, callback, extra_options=[], generic_options=set(), slug=None)
Definition: baseporter.py:9
lottie.parsers.baseporter.Baseporter.process
def process(self, *a, **kw)
Definition: baseporter.py:17
lottie.parsers.baseporter.Loader._module_path
_module_path
Definition: baseporter.py:62
lottie.parsers.baseporter.Loader.__iter__
def __iter__(self)
Definition: baseporter.py:93
lottie.parsers.baseporter.ExtraOption
Definition: baseporter.py:32
lottie.parsers.baseporter.IoProgressReporter.report_message
def report_message(self, message)
Definition: baseporter.py:131
lottie.parsers.baseporter.Loader.get
def get(self, slug)
Definition: baseporter.py:96
lottie.parsers.baseporter.Loader
Definition: baseporter.py:58
lottie.parsers.baseporter.Baseporter.generic_options
generic_options
Definition: baseporter.py:14
lottie.parsers.baseporter.ExtraOption.add_argument
def add_argument(self, slug, parser)
Definition: baseporter.py:40
lottie.parsers.baseporter.Loader._module_name
_module_name
Definition: baseporter.py:63
lottie.parsers.baseporter.ExtraOption.nsvar
def nsvar(self, slug)
Definition: baseporter.py:44
lottie.parsers.baseporter.Baseporter.extensions
extensions
Definition: baseporter.py:11
lottie.parsers.baseporter.Loader._failed
_failed
Definition: baseporter.py:65
lottie.parsers.baseporter.Loader.failed_modules
def failed_modules(self)
Definition: baseporter.py:81
lottie.parsers.baseporter.Baseporter.__repr__
def __repr__(self)
Definition: baseporter.py:20
lottie.parsers.baseporter.IoProgressReporter.report_progress
def report_progress(self, title, value, total)
Definition: baseporter.py:127
lottie.parsers.baseporter.ExtraOption.name
name
Definition: baseporter.py:34
lottie.parsers.baseporter.Loader.keys
def keys(self)
Definition: baseporter.py:115
lottie.parsers.baseporter.IoProgressReporter
Definition: baseporter.py:126
lottie.parsers.baseporter.Loader.load_modules
def load_modules(self)
Definition: baseporter.py:67
lottie.parsers.baseporter.io_progress
def io_progress()
Definition: baseporter.py:139
lottie.parsers.baseporter.Loader.items
def items(self)
Definition: baseporter.py:88
lottie.parsers.baseporter.Loader.set_options
def set_options(self, parser)
Definition: baseporter.py:111
lottie.parsers.baseporter.Loader.get_from_filename
def get_from_filename(self, filename)
Definition: baseporter.py:102
lottie.parsers.baseporter.Loader.decorator
def decorator(self, name, extensions, extra_options=[], generic_options=set(), slug=None)
Definition: baseporter.py:118
lottie.parsers.baseporter.Loader.__getitem__
def __getitem__(self, key)
Definition: baseporter.py:99