equal
deleted
inserted
replaced
30 def w(msg): |
30 def w(msg): |
31 if isinstance(msg, BaseException): |
31 if isinstance(msg, BaseException): |
32 try: |
32 try: |
33 msg = str(msg) + "\n" |
33 msg = str(msg) + "\n" |
34 except: |
34 except: |
35 msg = u(msg) + u"\n" |
35 msg = u(msg) + "\n" |
36 if type(msg) is str: |
|
37 msg = codecs.encode(msg, sys.stderr.encoding) |
|
38 sys.stderr.write(msg) |
36 sys.stderr.write(msg) |
39 |
37 |
40 optParser = OptionParser() |
38 optParser = OptionParser() |
41 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
39 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
42 help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
40 help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
112 sys.exit(1) |
110 sys.exit(1) |
113 |
111 |
114 try: |
112 try: |
115 ymlC = ymlCStyle() |
113 ymlC = ymlCStyle() |
116 |
114 |
117 rtext = u"" |
115 rtext = "" |
118 |
116 |
119 if not options.emptyinput: |
117 if not options.emptyinput: |
120 files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) |
118 files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) |
121 |
119 |
122 if options.xml: |
120 if options.xml: |
130 sys.exit(0) |
128 sys.exit(0) |
131 else: |
129 else: |
132 rtext = backend.finish(result) |
130 rtext = backend.finish(result) |
133 |
131 |
134 if not rtext: |
132 if not rtext: |
135 rtext = u"<empty/>" |
133 rtext = "<empty/>" |
136 |
134 |
137 def ymldebug(context, text): |
135 def ymldebug(context, text): |
138 if options.trace: |
136 if options.trace: |
139 sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n") |
137 sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n") |
140 return "" |
138 return "" |
151 ymlns['assert'] = ymlassert |
149 ymlns['assert'] = ymlassert |
152 |
150 |
153 if options.xpath: |
151 if options.xpath: |
154 tree = etree.fromstring(rtext) |
152 tree = etree.fromstring(rtext) |
155 ltree = tree.xpath(codecs.decode(options.xpath, options.encoding)) |
153 ltree = tree.xpath(codecs.decode(options.xpath, options.encoding)) |
156 rtext = u"" |
154 rtext = "" |
157 try: |
155 try: |
158 for rtree in ltree: |
156 for rtree in ltree: |
159 rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode) |
157 rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode) |
160 except: |
158 except: |
161 rtext = ltree |
159 rtext = ltree |
184 for key, value in params.iteritems(): |
182 for key, value in params.iteritems(): |
185 if type(value) is not str: |
183 if type(value) is not str: |
186 params[key] = u(value) |
184 params[key] = u(value) |
187 if options.stringparams: |
185 if options.stringparams: |
188 for key, value in eval(options.stringparams).iteritems(): |
186 for key, value in eval(options.stringparams).iteritems(): |
189 params[key] = u"'" + u(value) + u"'" |
187 params[key] = "'" + u(value) + "'" |
190 |
188 |
191 rresult = transform(doc, **params) |
189 rresult = transform(doc, **params) |
192 # lxml is somewhat buggy |
190 # lxml is somewhat buggy |
193 try: |
191 try: |
194 rtext = u(rresult) |
192 rtext = u(rresult) |
224 |
222 |
225 except KeyboardInterrupt: |
223 except KeyboardInterrupt: |
226 w("\n") |
224 w("\n") |
227 sys.exit(1) |
225 sys.exit(1) |
228 except YMLAssert as msg: |
226 except YMLAssert as msg: |
229 w(u"YML Assertion failed: " + u(msg) + u"\n") |
227 w("YML Assertion failed: " + u(msg) + "\n") |
230 sys.exit(2) |
228 sys.exit(2) |
231 except KeyError as msg: |
229 except KeyError as msg: |
232 w(u"not found: " + u(msg) + u"\n") |
230 w("not found: " + u(msg) + "\n") |
233 sys.exit(4) |
231 sys.exit(4) |
234 except LookupError as msg: |
232 except LookupError as msg: |
235 w(u"not found: " + u(msg) + u"\n") |
233 w("not found: " + u(msg) + "\n") |
236 sys.exit(4) |
234 sys.exit(4) |
237 except etree.XMLSyntaxError as e: |
235 except etree.XMLSyntaxError as e: |
238 log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL) |
236 log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL) |
239 for entry in log: |
237 for entry in log: |
240 w(u"XML error: " + u(entry.message) + u"\n") |
238 w("XML error: " + u(entry.message) + "\n") |
241 sys.exit(5) |
239 sys.exit(5) |
242 except Exception as msg: |
240 except Exception as msg: |
243 w(msg) |
241 w(msg) |
244 sys.exit(5) |
242 sys.exit(5) |