python-lottie  0.6.11+deved4e6c7
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.namename = name
11  self.extensionsextensions = extensions
12  self.callbackcallback = callback
13  self.extra_optionsextra_options = extra_options
14  self.generic_optionsgeneric_options = generic_options
15  self.slugslug = slug if slug is not None else extensions[0]
16 
17  def process(self, *a, **kw):
18  return self.callbackcallback(*a, **kw)
19 
20  def __repr__(self):
21  return "<%s %s>" % (self.__class__.__name__, self.slugslug)
22 
23  def argparse_options(self, ns):
24  o_options = {}
25  for opt in self.extra_optionsextra_options:
26  o_options[opt.dest] = getattr(ns, opt.nsvar(self.slugslug))
27  for opt in self.generic_optionsgeneric_options:
28  o_options[opt] = getattr(ns, opt)
29  return o_options
30 
31 
33  def __init__(self, name, **kwargs):
34  self.namename = name
35  self.kwargskwargs = kwargs
36  if "action" not in self.kwargskwargs:
37  self.kwargskwargs["metavar"] = self.namename
38  self.destdest = kwargs.pop("dest", name)
39 
40  def add_argument(self, slug, parser):
41  opt = "--%s-%s" % (slug, self.namename.replace("_", "-"))
42  parser.add_argument(opt, dest=self.nsvarnsvar(slug), **self.kwargskwargs)
43 
44  def nsvar(self, slug):
45  return "%s_%s" % (slug, self.destdest)
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_loaded = False
61  self._registry_registry = {}
62  self._module_path_module_path = os.path.dirname(module_path)
63  self._module_name_module_name = module_name.replace(".base", "")
64  self._ie_ie = ie
65  self._failed_failed = {}
66 
67  def load_modules(self):
68  self._loaded_loaded = True
69 
70  for _, modname, _ in pkgutil.iter_modules([self._module_path_module_path]):
71  if modname == "base":
72  continue
73 
74  full_modname = "." + modname
75  try:
76  importlib.import_module(full_modname, self._module_name_module_name)
77  except ImportError as e:
78  self._failed_failed[modname] = e.name
79 
80  @property
81  def failed_modules(self):
82  if not self._loaded_loaded:
83  self.load_modulesload_modules()
84 
85  return self._failed_failed
86 
87  @property
88  def items(self):
89  if not self._loaded_loaded:
90  self.load_modulesload_modules()
91  return self._registry_registry
92 
93  def __iter__(self):
94  return iter(self.itemsitems.values())
95 
96  def get(self, slug):
97  return self.itemsitems.get(slug, None)
98 
99  def __getitem__(self, key):
100  return self.getget(key)
101 
102  def get_from_filename(self, filename):
103  return self.get_from_extensionget_from_extension(os.path.splitext(filename)[1][1:])
104 
105  def get_from_extension(self, ext):
106  for p in self.itemsitems.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.itemsitems.values():
113  _add_options(parser, self._ie_ie, exporter)
114 
115  def keys(self):
116  return self.itemsitems.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_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
def __init__(self, name, extensions, callback, extra_options=[], generic_options=set(), slug=None)
Definition: baseporter.py:9
def __init__(self, name, **kwargs)
Definition: baseporter.py:33
def add_argument(self, slug, parser)
Definition: baseporter.py:40
def report_progress(self, title, value, total)
Definition: baseporter.py:127
def get_from_filename(self, filename)
Definition: baseporter.py:102
def __init__(self, module_path, module_name, ie)
Definition: baseporter.py:59
def set_options(self, parser)
Definition: baseporter.py:111
def decorator(self, name, extensions, extra_options=[], generic_options=set(), slug=None)
Definition: baseporter.py:118