| 1 |
import os |
| 2 |
import sys |
| 3 |
import pkgutil |
| 4 |
import inspect |
| 5 |
import importlib |
| 6 |
import logging |
| 7 |
|
| 8 |
from babelfont.Font import Font |
| 9 |
from babelfont.fontFilters import parse_filter |
| 10 |
|
| 11 |
logger = logging.getLogger(__name__) |
| 12 |
|
| 13 |
|
| 14 |
class BaseConvertor: |
| 15 |
filename: str |
| 16 |
scratch: object |
| 17 |
font: Font |
| 18 |
compile_only: bool |
| 19 |
|
| 20 |
suffix = ".XXX" |
| 21 |
|
| 22 |
LOAD_FILTERS = [] |
| 23 |
COMPILE_FILTERS = [] |
| 24 |
SAVE_FILTERS = [] |
| 25 |
|
| 26 |
def __init__(self): |
| 27 |
self.logger = logging.getLogger(self.__class__.__name__) |
| 28 |
|
| 29 |
@classmethod |
| 30 |
def can_load(cls, other, **kwargs): |
| 31 |
return other.filename.endswith(cls.suffix) |
| 32 |
|
| 33 |
@classmethod |
| 34 |
def can_save(cls, other, **kwargs): |
| 35 |
return other.filename.endswith(cls.suffix) |
| 36 |
|
| 37 |
@classmethod |
| 38 |
def load(cls, convertor, compile_only=False, filters=True): |
| 39 |
self = cls() |
| 40 |
self.font = Font() |
| 41 |
# Pass on information to child |
| 42 |
self.filename = convertor.filename |
| 43 |
self.scratch = convertor.scratch |
| 44 |
self.compile_only = compile_only |
| 45 |
loaded = self._load() |
| 46 |
if filters: |
| 47 |
for f in cls.LOAD_FILTERS: |
| 48 |
fltr, filterargs = parse_filter(f) |
| 49 |
fltr(loaded, filterargs) |
| 50 |
|
| 51 |
return loaded |
| 52 |
|
| 53 |
@classmethod |
| 54 |
def save(cls, obj, convertor, **kwargs): |
| 55 |
self = cls() |
| 56 |
self.font = obj |
| 57 |
# Pass on information to child |
| 58 |
self.filename = convertor.filename |
| 59 |
self.scratch = convertor.scratch |
| 60 |
return self._save() |
| 61 |
|
| 62 |
def _load(self): |
| 63 |
raise NotImplementedError |
| 64 |
|
| 65 |
def _save(self): |
| 66 |
raise NotImplementedError |
| 67 |
|
| 68 |
|
| 69 |
class Convert: |
| 70 |
convertors = [] |
| 71 |
|
| 72 |
@classmethod |
| 73 |
def _load_convertors(cls): |
| 74 |
if cls.convertors: |
| 75 |
return |
| 76 |
convertorpath = os.path.join( |
| 77 |
os.path.dirname(sys.modules[cls.__module__].__file__) |
| 78 |
) |
| 79 |
# Additional plugin path here? |
| 80 |
loaders = pkgutil.iter_modules([convertorpath]) |
| 81 |
for loader, module_name, is_pkg in loaders: |
| 82 |
spec = loader.find_spec(module_name) |
| 83 |
_module = importlib.util.module_from_spec(spec) |
| 84 |
spec.loader.exec_module(_module) |
| 85 |
classes = [ |
| 86 |
x[1] |
| 87 |
for x in inspect.getmembers(_module, inspect.isclass) |
| 88 |
if issubclass(x[1], BaseConvertor) and x[1] is not BaseConvertor |
| 89 |
] |
| 90 |
cls.convertors.extend(classes) |
| 91 |
|
| 92 |
def __init__(self, filename): |
| 93 |
self._load_convertors() |
| 94 |
self.filename = filename |
| 95 |
self.scratch = {} |
| 96 |
|
| 97 |
def load_convertor(self, **kwargs): |
| 98 |
for c in self.convertors: |
| 99 |
if c.can_load(self, **kwargs): |
| 100 |
return c |
| 101 |
|
| 102 |
def save_convertor(self, **kwargs): |
| 103 |
for c in self.convertors: |
| 104 |
if c.can_save(self, **kwargs): |
| 105 |
return c |
| 106 |
|
| 107 |
def load(self, **kwargs): |
| 108 |
c = self.load_convertor(**kwargs) |
| 109 |
if not c: |
| 110 |
logger.error("Could not find a convertor from %s", self.filename) |
| 111 |
raise NotImplementedError |
| 112 |
return c.load(self, **kwargs) |
| 113 |
|
| 114 |
def save(self, obj, **kwargs): |
| 115 |
c = self.save_convertor(**kwargs) |
| 116 |
if not c: |
| 117 |
logger.error("Could not find a convertor to %s", self.filename) |
| 118 |
raise NotImplementedError |
| 119 |
return c.save(obj, self, **kwargs) |