1# -*- coding: utf-8 -*-2# Copyright 2011 Takeshi KOMIYA3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.1516from importlib import metadata1718from blockdiag.utils.logging import warning1920drawers = {}212223def init_imagedrawers(debug=False):24 for drawer in metadata.entry_points(group='blockdiag_imagedrawers'):25 try:26 module = drawer.load()27 if hasattr(module, 'setup'):28 module.setup(module)29 except Exception as exc:30 if debug:31 warning('Failed to load %s: %r' % (drawer.name, exc))323334def install_imagedrawer(ext, drawer):35 drawers[ext] = drawer363738def create(_format, filename, **kwargs):39 if len(drawers) == 0:40 init_imagedrawers(debug=kwargs.get('debug'))4142 _format = _format.lower()43 if _format in drawers:44 drawer = drawers[_format](filename, **kwargs)45 else:46 msg = 'failed to load %s image driver' % _format47 raise RuntimeError(msg)4849 if 'linejump' in kwargs.get('filters', []):50 from blockdiag.imagedraw.filters.linejump import LineJumpDrawFilter51 jumpsize = kwargs.get('jumpsize', 0)52 drawer = LineJumpDrawFilter(drawer, jumpsize)5354 return drawer