]> wagnertech.de Git - mDoc.git/blob - python/mDoc/mdoc/worker.py
pydev
[mDoc.git] / python / mDoc / mdoc / worker.py
1 '''
2 Created on 02.08.2019
3
4 @author: mdoc
5 '''
6
7 from mutil.XmlExtractor import XmlExtractor
8 from mutil import XmlExtractor as XMLE
9 import re
10
11 def outfirst(outfile, tpllayer, attrs, value):
12     if tpllayer["value"]:
13         ostr = tpllayer["value"]
14         if "$value" in ostr:
15             ostr = re.sub(r"\$value", value, ostr)
16         print (ostr, end="", file=outfile)
17
18 def outlast(outfile, tpllayer, attrs, value):
19     if tpllayer["value"]:
20         print (file=outfile)
21
22 def process_file_layer(source, outfile, globals, hierarc):
23
24
25     (ctl,elem, value, attrs) = source.extractElement(XMLE.EC_BEG)
26     while ctl != XMLE.EC_END:
27         item = None
28         nexthiera = {}
29         if elem in hierarc:
30             item = hierarc[elem]
31             nexthiera = hierarc[elem]["items"]
32         elif elem in globals:
33             item = globals[elem]
34         else:
35             print ("Warning: No mapping for item:" +elem)
36         if item:
37             outfirst(outfile, item, attrs, value)
38         process_file_layer(source, outfile, globals, nexthiera)
39         if item:
40             outlast(outfile, item, attrs, value)
41         (ctl,elem, value, attrs) = source.extractElement(ctl)
42         
43 def convert (file, format):
44     
45     source = XmlExtractor()
46     source.openInput(file)
47
48     # read whole template
49     tple = XmlExtractor()
50     tpl = tple.readFile("tpl/"+format+".tpl")
51     globals = tpl["mdoc"]["items"]["global"]["items"]
52     hierarc = tpl["mdoc"]["items"]["hierarchical"]["items"]
53     
54     # open output file
55     ext = tpl["mdoc"]["attrs"]["extension"]
56     outname = re.sub(r"\.xml", "."+ext, file)
57     outfile = open(outname, "w")
58     #print ("hallo", file=outfile)
59     
60     process_file_layer(source, outfile, globals, hierarc)
61