More code re-organizing

This commit is contained in:
Brett Williams
2023-10-21 22:45:30 -05:00
parent 7a52cce40a
commit 9027cd7202
26 changed files with 33 additions and 48 deletions

View File

@@ -0,0 +1,56 @@
import logging
import os
import subprocess
logger = logging.getLogger()
SUBPROCESS_TIMEOUT = 5
class BaseRenderEngine(object):
install_paths = []
supported_extensions = []
def __init__(self, custom_path=None):
self.custom_renderer_path = custom_path
if not self.renderer_path():
raise FileNotFoundError(f"Cannot find path to renderer for {self.name()} instance")
def renderer_path(self):
return self.custom_renderer_path or self.default_renderer_path()
@classmethod
def name(cls):
return cls.__name__.lower()
@classmethod
def default_renderer_path(cls):
path = None
try: # Linux and macOS
path = subprocess.check_output(['which', cls.name()], timeout=SUBPROCESS_TIMEOUT).decode('utf-8').strip()
except (subprocess.CalledProcessError, FileNotFoundError):
for p in cls.install_paths:
if os.path.exists(p):
path = p
except Exception as e:
logger.exception(e)
return path
def version(self):
raise NotImplementedError("version not implemented")
def get_help(self):
path = self.renderer_path()
if not path:
raise FileNotFoundError("renderer path not found")
help_doc = subprocess.check_output([path, '-h'], stderr=subprocess.STDOUT,
timeout=SUBPROCESS_TIMEOUT).decode('utf-8')
return help_doc
@classmethod
def get_output_formats(cls):
raise NotImplementedError(f"get_output_formats not implemented for {cls.__name__}")
@classmethod
def get_arguments(cls):
pass