From b0e2c850651b476108b287a1126d63be29ffe5fd Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Wed, 29 Dec 2010 13:33:06 +0200 Subject: [PATCH] translate: implement a counter to export the code line number Before this, all code sources were like "#: code:foo/bar.py:0", now they will have line no like "#: code:foo/bar.py:123". Note that the gettext() wouldn't be affected, since it doesn't consult the file name or line so far. bzr revid: p_christ@hol.gr-20101229113306-vo3gflwunq2f75qm --- bin/tools/translate.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bin/tools/translate.py b/bin/tools/translate.py index 47cd24cfd16..8cf09f96fef 100644 --- a/bin/tools/translate.py +++ b/bin/tools/translate.py @@ -772,6 +772,8 @@ def trans_generate(lang, modules, dbname=None): if module in installed_modules: frelativepath = str("addons" + frelativepath) ite = re_dquotes.finditer(code_string) + code_offset = 0 + code_line = 1 for i in ite: src = i.group(1) if src.startswith('""'): @@ -779,11 +781,18 @@ def trans_generate(lang, modules, dbname=None): src = src[2:-2] else: src = join_dquotes.sub(r'\1', src) + # try to count the lines from the last pos to our place: + code_line += code_string[code_offset:i.start(1)].count('\n') # now, since we did a binary read of a python source file, we # have to expand pythonic escapes like the interpreter does. src = src.decode('string_escape') - push_translation(module, terms_type, frelativepath, 0, encode(src)) + push_translation(module, terms_type, frelativepath, code_line, encode(src)) + code_line += i.group(1).count('\n') + code_offset = i.end() # we have counted newlines up to the match end + ite = re_quotes.finditer(code_string) + code_offset = 0 #reset counters + code_line = 1 for i in ite: src = i.group(1) if src.startswith("''"): @@ -791,8 +800,11 @@ def trans_generate(lang, modules, dbname=None): src = src[2:-2] else: src = join_quotes.sub(r'\1', src) + code_line += code_string[code_offset:i.start(1)].count('\n') src = src.decode('string_escape') - push_translation(module, terms_type, frelativepath, 0, encode(src)) + push_translation(module, terms_type, frelativepath, code_line, encode(src)) + code_line += i.group(1).count('\n') + code_offset = i.end() # we have counted newlines up to the match end for path in path_list: logger.debug("Scanning files of modules at %s", path)