16 ''' |
16 ''' |
17 Configuration flattener |
17 Configuration flattener |
18 ''' |
18 ''' |
19 |
19 |
20 |
20 |
21 import re |
|
22 import os |
21 import os |
23 import sys |
22 import sys |
24 import codecs |
|
25 import logging |
23 import logging |
26 import xml.parsers.expat |
24 import pkg_resources |
27 import unittest, os, sys, pkg_resources |
|
28 |
25 |
29 pkg_resources.require('lxml') |
26 pkg_resources.require('lxml') |
30 |
27 |
31 try: |
28 from cone.public import exceptions |
32 from cElementTree import ElementTree |
|
33 except ImportError: |
|
34 try: |
|
35 from elementtree import ElementTree |
|
36 except ImportError: |
|
37 try: |
|
38 from xml.etree import cElementTree as ElementTree |
|
39 except ImportError: |
|
40 from xml.etree import ElementTree |
|
41 |
|
42 import __init__ |
|
43 |
|
44 from cone.public import exceptions,plugin,utils,api |
|
45 |
29 |
46 class XsltTransformer(): |
30 class XsltTransformer(): |
47 """ |
31 """ |
48 XSLT Transformer |
32 XSLT Transformer |
49 """ |
33 """ |
50 |
34 |
51 def _init(self,ref,configuration): |
35 def _init(self,ref,configuration): |
52 self.logger = logging.getLogger('cone.gcfml(%s)' % self.ref) |
36 self.logger = logging.getLogger('cone.gcfml(%s)' % self.ref) |
53 |
37 |
54 |
38 |
55 def transform_lxml(self, input, xslt, output, enc, linesep=os.linesep): |
39 def transform_lxml(self, input, xslt, enc, linesep=os.linesep): |
56 """ |
40 """ |
57 XSLT transform with lxml. |
41 XSLT transform with lxml. |
58 """ |
42 """ |
59 from lxml import etree |
43 from lxml import etree |
60 |
44 |
61 if not enc: |
45 if not enc: |
62 enc = sys.getdefaultencoding() |
46 enc = sys.getdefaultencoding() |
63 try: |
47 try: |
64 xslt_doc = etree.parse(xslt) |
48 xslt_doc = etree.parse(xslt) |
65 transform = etree.XSLT(xslt_doc) |
49 transform = etree.XSLT(xslt_doc) |
|
50 |
|
51 input_doc = etree.parse(input) |
|
52 result = str(transform(input_doc)) |
|
53 postprocessed_result = post_process_result(result, enc, linesep) |
|
54 return postprocessed_result |
66 |
55 |
67 input_doc = etree.parse(input) |
56 except Exception, e: |
68 result = str(transform(input_doc)) |
57 logging.getLogger('cone.gcfml').error('Failed to do XSLT transformation: %s' % e) |
69 postprocessed_result = post_process_result(result, enc, linesep) |
58 raise exceptions.ConeException('Failed to do XSLT transformation: %s' % (e)) |
70 |
|
71 if not filter_file_writing(postprocessed_result): |
|
72 write_string_to_file(postprocessed_result, output, enc) |
|
73 |
|
74 except Exception, e: |
|
75 logging.getLogger('cone.gcfml').error('Failed to do XSLT transformation: %s' % e) |
|
76 raise exceptions.ConeException('Failed to do XSLT transformation: %s' % (e)) |
|
77 |
|
78 |
|
79 def transform_4s(self, input, xslt, output, enc, linesep=os.linesep): |
|
80 """ |
|
81 XSLT transform with 4Suite |
|
82 """ |
|
83 from Ft.Xml.Xslt import Transform |
|
84 from Ft.Xml.Xslt import Processor |
|
85 from Ft.Xml import InputSource |
|
86 from Ft.Lib.Uri import OsPathToUri |
|
87 |
|
88 |
|
89 if not enc: |
|
90 enc = sys.getdefaultencoding() |
|
91 |
|
92 try: |
|
93 processor = Processor.Processor() |
|
94 |
|
95 srcAsUri = OsPathToUri(input) |
|
96 source = InputSource.DefaultFactory.fromUri(srcAsUri) |
|
97 |
|
98 ssAsUri = OsPathToUri(xslt) |
|
99 transform = InputSource.DefaultFactory.fromUri(ssAsUri) |
|
100 |
|
101 processor.appendStylesheet(transform) |
|
102 result = processor.run(source) |
|
103 |
|
104 postprocessed_result = post_process_result(result, enc, linesep) |
|
105 |
|
106 if not filter_file_writing(postprocessed_result): |
|
107 write_string_to_file(postprocessed_result, output, enc) |
|
108 |
|
109 except Exception, e: |
|
110 logging.getLogger('cone.gcfml').error('Failed to do XSLT transformation: %s' % e) |
|
111 raise exceptions.ConeException('Failed to do XSLT transformation: %s' % (e)) |
|
112 |
|
113 def filter_file_writing(string): |
|
114 """ |
|
115 Returns True if writing result file should be ignored. |
|
116 """ |
|
117 string = string.rstrip('\n\r') |
|
118 if string == '' or string == '<?xml version="1.0" encoding="UTF-16"?>' or \ |
|
119 string == '<?xml version="1.0" encoding="UTF-8"?>': |
|
120 return True |
|
121 |
|
122 return False |
|
123 |
59 |
124 |
60 |
125 def post_process_result(string, enc, linesep): |
61 def post_process_result(string, enc, linesep): |
126 """ |
62 """ |
127 Does post process for result from XSLT transform |
63 Does post process for result from XSLT transform |
128 - encoding |
64 - encoding |
129 - removes extra line separators |
65 - removes extra line separators |
130 - changes line separators |
66 - changes line separators |
131 """ |
67 """ |
132 output_string = None |
68 output_string = None |
133 |
69 |
134 try: |
70 try: |
135 output_string = string.decode(enc) |
71 output_string = string.decode(enc) |
136 if not output_string.startswith('<'): |
72 if not output_string.startswith('<'): |
137 output_string = '\n' + output_string |
73 output_string = '\n' + output_string |
138 output_string = output_string.replace('<?xml version="1.0" encoding="UTF-16"?>', '<?xml version="1.0" encoding="UTF-16"?>\n\n') |
74 output_string = output_string.replace('<?xml version="1.0" encoding="UTF-16"?>', '<?xml version="1.0" encoding="UTF-16"?>\n\n') |
139 output_string = output_string.replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0" encoding="UTF-8"?>\n\n') |
75 output_string = output_string.replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0" encoding="UTF-8"?>\n\n') |
140 output_string = output_string.replace('\n\n','\n') |
76 output_string = output_string.replace('\n\n','\n') |
141 output_string = output_string.replace('\n', linesep) |
77 output_string = output_string.replace('\n', linesep) |
142 output_string+= linesep |
78 output_string+= linesep |
143 except Exception, e: |
79 except Exception, e: |
144 logging.getLogger('cone.gcfml').error('Cannot post process result: %s \nException: %s' % (string, e)) |
80 logging.getLogger('cone.gcfml').error('Cannot post process result: %s \nException: %s' % (string, e)) |
145 raise exceptions.ConeException('Cannot post process result: %s \nException: %s' % (string, e)) |
81 raise exceptions.ConeException('Cannot post process result: %s \nException: %s' % (string, e)) |
146 |
82 |
147 return output_string |
83 return output_string |
148 |
|
149 def write_string_to_file(string, output, enc): |
|
150 """ |
|
151 Writes string to file |
|
152 """ |
|
153 try: |
|
154 outfile = os.path.abspath(output) |
|
155 |
|
156 if not os.path.exists(os.path.dirname(outfile)): |
|
157 os.makedirs(os.path.dirname(outfile)) |
|
158 |
|
159 fl = codecs.open(outfile, 'w', enc) |
|
160 fl.write(string) |
|
161 fl.close() |
|
162 |
|
163 except Exception, e: |
|
164 logging.getLogger('cone.gcfml').error('Cannot write Element to file (%s). Exception: %s' % (output, e)) |
|
165 raise exceptions.ConeException('Cannot write Element to file (%s). Exception: %s' % (output, e)) |
|
166 |
84 |
167 |
85 |
168 |
86 |