| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
from xml.etree.ElementTree import ParseError |
| 6 |
from .xmlparser import XmlConfig |
| 7 |
from .txtparser import TextConfig |
| 8 |
from .imagedata import ImageConverterPool, ImageConverter |
| 9 |
from .imagedata import ImageFormatPool, FormatRule |
| 10 |
from .imagedata import image_setup |
| 11 |
from dbtexmf.xslt.xsltconf import XsltCommandPool, XsltEngine |
| 12 |
from dbtexmf.xslt import xslt_setup |
| 13 |
|
| 14 |
|
| 15 |
class ConfigFactory: |
| 16 |
""" |
| 17 |
Build the actual objects that configure the other modules from the XML |
| 18 |
parsed configuration, and publish them to the related modules |
| 19 |
""" |
| 20 |
def __init__(self, xmlconfig): |
| 21 |
self.xmlconfig = xmlconfig |
| 22 |
|
| 23 |
def publish(self): |
| 24 |
pool = self.imagedata_converter_config() |
| 25 |
if pool: image_setup().converter_pool.prepend_pool(pool) |
| 26 |
pool = self.imagedata_format_config() |
| 27 |
if pool: image_setup().format_pool.prepend_pool(pool) |
| 28 |
pool = self.xslt_config() |
| 29 |
if pool: xslt_setup().prepend_pool(pool) |
| 30 |
|
| 31 |
def imagedata_format_config(self): |
| 32 |
rules = self.xmlconfig.get("imagedata").get("formatrule", None) |
| 33 |
if not(rules): |
| 34 |
return None |
| 35 |
pool = ImageFormatPool() |
| 36 |
for rul in rules: |
| 37 |
fmt = FormatRule(rul.imgsrc, rul.imgdst, rul.docformat, rul.backend) |
| 38 |
pool.add_rule(fmt) |
| 39 |
return pool |
| 40 |
|
| 41 |
def imagedata_converter_config(self): |
| 42 |
converters = self.xmlconfig.get("imagedata").get("converter", None) |
| 43 |
if not(converters): |
| 44 |
return None |
| 45 |
pool = ImageConverterPool() |
| 46 |
for cv in converters: |
| 47 |
imc = ImageConverter(cv.imgsrc, cv.imgdst, cv.docformat, cv.backend) |
| 48 |
for cmd in cv.commands: |
| 49 |
imc.add_command(cmd.args, stdin=cmd.stdin, stdout=cmd.stdout, |
| 50 |
shell=cmd.shell) |
| 51 |
pool.add_converter(imc) |
| 52 |
return pool |
| 53 |
|
| 54 |
def xslt_config(self): |
| 55 |
engines = self.xmlconfig.get("xslt").get("engine", None) |
| 56 |
if not(engines): |
| 57 |
return None |
| 58 |
pool = XsltCommandPool() |
| 59 |
for proc in engines: |
| 60 |
if not(proc.commands): |
| 61 |
continue |
| 62 |
eng = XsltEngine(param_format=proc.param_format) |
| 63 |
for cmd in proc.commands: |
| 64 |
eng.add_command(cmd.args, stdin=cmd.stdin, stdout=cmd.stdout, |
| 65 |
shell=cmd.shell) |
| 66 |
pool.add_command_run(eng) |
| 67 |
return pool |
| 68 |
|
| 69 |
|
| 70 |
class DbtexConfig: |
| 71 |
""" |
| 72 |
Main configuration object, in charge to parse the configuration files |
| 73 |
and populate the setup. |
| 74 |
""" |
| 75 |
def __init__(self): |
| 76 |
self.options = [] |
| 77 |
self.paths = [] |
| 78 |
self.style_exts = ["", ".xml", ".specs", ".conf"] |
| 79 |
|
| 80 |
def warn(self, text): |
| 81 |
print(text, file=sys.stderr) |
| 82 |
|
| 83 |
def fromfile(self, filename): |
| 84 |
try: |
| 85 |
self.fromxmlfile(filename) |
| 86 |
except ParseError as e: |
| 87 |
self.warn("Text configuration files are deprecated. "\ |
| 88 |
"Use the XML format instead") |
| 89 |
self.fromtxtfile(filename) |
| 90 |
except Exception as e: |
| 91 |
raise e |
| 92 |
|
| 93 |
def fromxmlfile(self, filename): |
| 94 |
xmlconfig = XmlConfig() |
| 95 |
xmlconfig.fromfile(filename) |
| 96 |
self.options += xmlconfig.options() |
| 97 |
factory = ConfigFactory(xmlconfig) |
| 98 |
factory.publish() |
| 99 |
|
| 100 |
def fromtxtfile(self, filename): |
| 101 |
txtconfig = TextConfig() |
| 102 |
txtconfig.fromfile(filename) |
| 103 |
self.options += txtconfig.options() |
| 104 |
|
| 105 |
def fromstyle(self, style, paths=None): |
| 106 |
# First, find the related config file |
| 107 |
if not paths: |
| 108 |
paths = self.paths |
| 109 |
|
| 110 |
for p in paths: |
| 111 |
for e in self.style_exts: |
| 112 |
file = os.path.join(p, style + e) |
| 113 |
if os.path.isfile(file): |
| 114 |
self.fromfile(file) |
| 115 |
return |
| 116 |
|
| 117 |
# If we are here nothing found |
| 118 |
raise ValueError("'%s': style not found" % style) |
| 119 |
|