[imp] improved qweb scanning function
[odoo/odoo.git] / npybabel.py
1 #!/usr/bin/python
2 # EASY-INSTALL-ENTRY-SCRIPT: 'Babel==0.9.6','console_scripts','pybabel'
3 __requires__ = 'Babel==0.9.6'
4 import sys
5 from pkg_resources import load_entry_point
6 import re
7
8 if __name__ == '__main__':
9     sys.exit(
10         load_entry_point('Babel==0.9.6', 'console_scripts', 'pybabel')()
11     )
12     
13 QWEB_EXPR = re.compile(r"""(?:\< *t\-tr *\>(.*?)\< *\/t\-tr *\>)|(?:\_t *\( *((?:\".*?\")|(?:\'.*?\')) *\))""")
14     
15 def extract_qweb(fileobj, keywords, comment_tags, options):
16     """Extract messages from XXX files.
17     :param fileobj: the file-like object the messages should be extracted
18                     from
19     :param keywords: a list of keywords (i.e. function names) that should
20                      be recognized as translation functions
21     :param comment_tags: a list of translator tags to search for and
22                          include in the results
23     :param options: a dictionary of additional options (optional)
24     :return: an iterator over ``(lineno, funcname, message, comments)``
25              tuples
26     :rtype: ``iterator``
27     """
28     content = fileobj.read()
29     found = QWEB_EXPR.finditer(content)
30     result = []
31     index = 0
32     line_nbr = 0
33     for f in found:
34         group = 1 if f.group(1) else 2
35         while index < f.start():
36             if content[index] == "\n":
37                 line_nbr += 1
38             index += 1
39         result.append((line_nbr, None, f.group(group), ""))
40     return result