]> wagnertech.de Git - mDoc.git/blob - python/mDoc/mdoc/worker.py
120bf5e82547173431db54e88af2eea19168755b
[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, flags=re.MULTILINE)
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         items = None
28         nexthiera = {}
29         if elem in hierarc:
30             items = hierarc[elem]
31         elif elem in globals:
32             items = globals[elem]
33         else:
34             print ("Warning: No mapping for item:" +elem)
35         if items:
36             # search for "first"
37             for item in items:
38                 if "position" in item["attrs"]:
39                     if item["attrs"]["position"] == "first":
40                         print (item["value"], end="", file=outfile)
41             # proceed working
42             for item in items:
43                 if not "position" in item["attrs"]:
44                     nexthiera = item["items"]
45                     if value:
46                         print (value, end="", file=outfile)
47                     process_file_layer(source, outfile, globals, nexthiera)
48             # search for "last"
49             for item in items:
50                 if "position" in item["attrs"]:
51                     if item["attrs"]["position"] == "last":
52                         print (item["value"], end="", file=outfile)
53         (ctl,elem, value, attrs) = source.extractElement(ctl)
54         
55 def convert (file, format):
56     
57     source = XmlExtractor()
58     source.openInput(file)
59
60     # read whole template
61     tple = XmlExtractor()
62     tpl = tple.readFile("tpl/"+format+".tpl")
63     globals = tpl["mdoc"][0]["items"]["global"][0]["items"]
64     hierarc = tpl["mdoc"][0]["items"]["hierarchical"][0]["items"]
65     
66     # open output file
67     ext = tpl["mdoc"][0]["attrs"]["extension"]
68     outname = re.sub(r"\.xml", "."+ext, file)
69     outfile = open(outname, "w")
70     #print ("hallo", file=outfile)
71     
72     process_file_layer(source, outfile, globals, hierarc)
73