Ticket #281: refactor-error-reporting-for-extraction-errors-r967.diff
| File refactor-error-reporting-for-extraction-errors-r967.diff, 8.7 KB (added by cboos, 15 years ago) |
|---|
-
genshi/filters/i18n.py
20 20 21 21 from compiler import ast 22 22 from gettext import NullTranslations 23 import logging 23 24 import os 24 25 import re 25 26 from types import FunctionType … … 156 157 assert callable(dgettext), "No domain gettext function passed" 157 158 gettext = lambda msg: dgettext(ctxt.get('_i18n.domain'), msg) 158 159 159 msgbuf = MessageBuffer(self.params )160 msgbuf = MessageBuffer(self.params, MsgDirective) 160 161 161 162 new_stream = [] 162 163 stream = iter(_apply_directives(stream, directives, ctxt)) … … 171 172 for skind, sdata, spos in _apply_directives(substream, 172 173 subdirectives, 173 174 ctxt): 174 try: 175 msgbuf.append(*previous) 176 previous = skind, sdata, spos 177 except IndexError: 178 raise IndexError("Not enough parameters passed to '%s' " 179 "on '%s', line number %s: %s" % 180 (type(self).__name__, 181 os.path.basename(spos[0]), spos[1], 182 self.params)) 183 try: 184 msgbuf.append(*previous) 185 except IndexError: 186 raise IndexError("Not enough parameters passed to '%s' on '%s'," 187 " line number %s: %s" % 188 (type(self).__name__, 189 os.path.basename(previous[2][0]), 190 previous[2][1], self.params), previous[1]) 191 175 msgbuf.append(*previous) 176 previous = skind, sdata, spos 177 msgbuf.append(*previous) 192 178 previous = kind, data, pos 193 179 194 180 for event in msgbuf.translate(gettext(msgbuf.format())): … … 199 185 return new_stream 200 186 201 187 def extract(self, stream, ctxt): 202 msgbuf = MessageBuffer(self.params )188 msgbuf = MessageBuffer(self.params, MsgDirective) 203 189 204 190 stream = iter(stream) 205 191 stream.next() # the outer start tag 206 192 previous = stream.next() 207 193 for event in stream: 208 try: 209 msgbuf.append(*previous) 210 except IndexError: 211 raise IndexError("Not enough parameters passed to '%s' on '%s'," 212 " line number %s: %s" % 213 (type(self).__name__, 214 os.path.basename(previous[2][0]), 215 previous[2][1], self.params)) 194 msgbuf.append(*previous) 216 195 previous = event 217 196 218 197 yield None, msgbuf.format(), filter(None, [ctxt.get('_i18n.comment')]) … … 222 201 223 202 def __call__(self, stream, directives, ctxt, **vars): 224 203 225 msgbuf = MessageBuffer(ctxt.get('_i18n.choose.params', [])[:]) 204 msgbuf = MessageBuffer(ctxt.get('_i18n.choose.params', [])[:], 205 InnerChooseDirective) 226 206 227 207 stream = iter(_apply_directives(stream, directives, ctxt)) 228 208 yield stream.next() # the outer start tag … … 372 352 singular_stream = list(_apply_directives(substream, 373 353 subdirectives, 374 354 ctxt)) 375 new_stream.append((MSGBUF, (), -1)) # msgbuf place holder355 new_stream.append((MSGBUF, (), ('', -1))) # msgbuf place holder 376 356 singular_msgbuf = ctxt.get('_i18n.choose.SingularDirective') 377 357 elif isinstance(subdirectives[0], 378 358 PluralDirective) and not plural_stream: … … 412 392 if previous is START: 413 393 stream.next() 414 394 415 singular_msgbuf = MessageBuffer(self.params [:])416 plural_msgbuf = MessageBuffer(self.params [:])395 singular_msgbuf = MessageBuffer(self.params, ChooseDirective) 396 plural_msgbuf = MessageBuffer(self.params, ChooseDirective) 417 397 418 398 for kind, event, pos in stream: 419 399 if kind is SUB: … … 426 406 plural_msgbuf = subdirective.extract(substream, ctxt, 427 407 plural_msgbuf) 428 408 elif not isinstance(subdirective, StripDirective): 429 try: 430 singular_msgbuf.append(kind, event, pos) 431 plural_msgbuf.append(kind, event, pos) 432 except IndexError: 433 raise IndexError("Not enough parameters passed to " 434 "'%s' on '%s', line number %s: " 435 "%s" % (type(self).__name__, 436 os.path.basename(pos[0]), 437 pos[1], self.params)) 409 singular_msgbuf.append(kind, event, pos) 410 plural_msgbuf.append(kind, event, pos) 438 411 else: 439 try: 440 singular_msgbuf.append(kind, event, pos) 441 plural_msgbuf.append(kind, event, pos) 442 except IndexError: 443 raise IndexError("Not enough parameters passed to '%s' on " 444 "'%s', line number %s: %s" % 445 (type(self).__name__, 446 os.path.basename(pos[0]), pos[1], 447 self.params)) 448 412 singular_msgbuf.append(kind, event, pos) 413 plural_msgbuf.append(kind, event, pos) 449 414 yield 'ngettext', \ 450 415 (singular_msgbuf.format(), plural_msgbuf.format()), \ 451 416 filter(None, [ctxt.get('_i18n.comment')]) … … 851 816 852 817 if msgbuf: 853 818 msgbuf.append(kind, data, pos) 854 # Un-comment bel low to extract messages without adding819 # Un-comment below to extract messages without adding 855 820 # directives 856 821 # else: 857 822 # msg_params = attrs.get(i18n_msg) … … 859 824 # print kind, data, pos 860 825 # if type(msg_params) is list: # event tuple 861 826 # msg_params = msg_params[0][1] 827 # # warning: MessageBuffer constructor has changed 862 828 # msgbuf = MessageBuffer( 863 829 # msg_params, attrs.get(i18n_comment), pos[1] 864 830 # ) … … 942 908 :since: version 0.5 943 909 """ 944 910 945 def __init__(self, params=u'', comment=None, lineno=-1):911 def __init__(self, params=u'', directive=None): 946 912 """Initialize the message buffer. 947 913 948 914 :param params: comma-separated list of parameter names … … 952 918 """ 953 919 if isinstance(params, basestring): 954 920 params = [name.strip() for name in params.split(',')] 921 self.orig_params = params 955 922 # params list needs to be copied so that directives can be evaluated 956 923 # more than once 957 924 self.params = params[:] 958 self.comment = comment 959 self.lineno = lineno 925 self.directive = directive 960 926 self.string = [] 961 927 self.events = {} 962 928 self.values = {} … … 979 945 self.string.append(data) 980 946 self.events.setdefault(self.stack[-1], []).append(None) 981 947 elif kind is EXPR: 982 param = self.params.pop(0) 948 if self.params: 949 param = self.params.pop(0) 950 else: 951 raise IndexError("'%s' parameters given to 'i18n:%s' but more " 952 "expressions used in '%s', line %s" % ( 953 ', '.join(self.orig_params), 954 self.directive.tagname, 955 os.path.basename(pos[0]), pos[1])) 983 956 self.string.append('%%(%s)s' % param) 984 957 self.events.setdefault(self.stack[-1], []).append(None) 985 958 self.values[param] = (kind, data, pos) … … 1168 1141 yield message 1169 1142 1170 1143 def setup_i18n(template, translator): 1171 """Conv inience function to setup both the i18n filter and the i18n1144 """Convenience function to setup both the i18n filter and the i18n 1172 1145 directives. 1173 1146 1174 1147 :param template: an instance of a genshi template
