i18n/hggettext
changeset 43691 47ef023d0165
parent 38815 617ae7e33a65
child 45763 bea8cf87bef3
equal deleted inserted replaced
43690:15cccbacd5ce 43691:47ef023d0165
    55         s = '""\n"' + lineterm.join(lines) + '"'
    55         s = '""\n"' + lineterm.join(lines) + '"'
    56     return s
    56     return s
    57 
    57 
    58 
    58 
    59 def poentry(path, lineno, s):
    59 def poentry(path, lineno, s):
    60     return ('#: %s:%d\n' % (path, lineno) +
    60     return (
    61             'msgid %s\n' % normalize(s) +
    61         '#: %s:%d\n' % (path, lineno)
    62             'msgstr ""\n')
    62         + 'msgid %s\n' % normalize(s)
       
    63         + 'msgstr ""\n'
       
    64     )
       
    65 
    63 
    66 
    64 doctestre = re.compile(r'^ +>>> ', re.MULTILINE)
    67 doctestre = re.compile(r'^ +>>> ', re.MULTILINE)
       
    68 
    65 
    69 
    66 def offset(src, doc, name, lineno, default):
    70 def offset(src, doc, name, lineno, default):
    67     """Compute offset or issue a warning on stdout."""
    71     """Compute offset or issue a warning on stdout."""
    68     # remove doctest part, in order to avoid backslash mismatching
    72     # remove doctest part, in order to avoid backslash mismatching
    69     m = doctestre.search(doc)
    73     m = doctestre.search(doc)
    70     if m:
    74     if m:
    71         doc = doc[:m.start()]
    75         doc = doc[: m.start()]
    72 
    76 
    73     # Backslashes in doc appear doubled in src.
    77     # Backslashes in doc appear doubled in src.
    74     end = src.find(doc.replace('\\', '\\\\'))
    78     end = src.find(doc.replace('\\', '\\\\'))
    75     if end == -1:
    79     if end == -1:
    76         # This can happen if the docstring contains unnecessary escape
    80         # This can happen if the docstring contains unnecessary escape
    77         # sequences such as \" in a triple-quoted string. The problem
    81         # sequences such as \" in a triple-quoted string. The problem
    78         # is that \" is turned into " and so doc wont appear in src.
    82         # is that \" is turned into " and so doc wont appear in src.
    79         sys.stderr.write("%s:%d:warning:"
    83         sys.stderr.write(
    80                          " unknown docstr offset, assuming %d lines\n"
    84             "%s:%d:warning:"
    81                          % (name, lineno, default))
    85             " unknown docstr offset, assuming %d lines\n"
       
    86             % (name, lineno, default)
       
    87         )
    82         return default
    88         return default
    83     else:
    89     else:
    84         return src.count('\n', 0, end)
    90         return src.count('\n', 0, end)
    85 
    91 
    86 
    92 
   119         cmdtable = getattr(mod, 'table', {})
   125         cmdtable = getattr(mod, 'table', {})
   120     functions.extend((c[0], False) for c in cmdtable.itervalues())
   126     functions.extend((c[0], False) for c in cmdtable.itervalues())
   121 
   127 
   122     for func, rstrip in functions:
   128     for func, rstrip in functions:
   123         if func.__doc__:
   129         if func.__doc__:
   124             docobj = func # this might be a proxy to provide formatted doc
   130             docobj = func  # this might be a proxy to provide formatted doc
   125             func = getattr(func, '_origfunc', func)
   131             func = getattr(func, '_origfunc', func)
   126             funcmod = inspect.getmodule(func)
   132             funcmod = inspect.getmodule(func)
   127             extra = ''
   133             extra = ''
   128             if funcmod.__package__ == funcmod.__name__:
   134             if funcmod.__package__ == funcmod.__name__:
   129                 extra = '/__init__'
   135                 extra = '/__init__'
   153     # It is very important that we import the Mercurial modules from
   159     # It is very important that we import the Mercurial modules from
   154     # the source tree where hggettext is executed. Otherwise we might
   160     # the source tree where hggettext is executed. Otherwise we might
   155     # accidentally import and extract strings from a Mercurial
   161     # accidentally import and extract strings from a Mercurial
   156     # installation mentioned in PYTHONPATH.
   162     # installation mentioned in PYTHONPATH.
   157     sys.path.insert(0, os.getcwd())
   163     sys.path.insert(0, os.getcwd())
   158     from mercurial import demandimport; demandimport.enable()
   164     from mercurial import demandimport
       
   165 
       
   166     demandimport.enable()
   159     for path in sys.argv[1:]:
   167     for path in sys.argv[1:]:
   160         if path.endswith('.txt'):
   168         if path.endswith('.txt'):
   161             rawtext(path)
   169             rawtext(path)
   162         else:
   170         else:
   163             docstrings(path)
   171             docstrings(path)