Patchwork [STABLE] i18n: use actual filename, in which function is defined, for hg.pot

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Aug. 2, 2017, 1:36 p.m.
Message ID <b6d0bcd19b4dffb2360f.1501681005@speaknoevil>
Download mbox | patch
Permalink /patch/22635/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - Aug. 2, 2017, 1:36 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1501599731 -32400
#      Wed Aug 02 00:02:11 2017 +0900
# Branch stable
# Node ID b6d0bcd19b4dffb2360f909f459c27104ad16565
# Parent  76b171209151fe41dbf8dbfec473cc533f3b40ca
# Available At https://bitbucket.org/foozy/mercurial-wip
#              hg pull https://bitbucket.org/foozy/mercurial-wip -r b6d0bcd19b4d
# EXP-Topic i18n-fix-update-pot-issues
i18n: use actual filename, in which function is defined, for hg.pot

Before this patch, source filename for msgid in hg.pot file becomes
incorrect, if a function is defined in file A, but detected in dict in
file B,

For example, almost all debug* commands are defined in
debugcommands.py, but hggettext detects them in "table" of
commands.py. Therefore, docstring fragments of debug* commands are
marked as "defined in commands.py" in hg.pot file.

This is serious problem for translation, because the cost to find out
original location of texts increases very much.
Augie Fackler - Aug. 2, 2017, 2:51 p.m.
On Wed, Aug 02, 2017 at 10:36:45PM +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> # Date 1501599731 -32400
> #      Wed Aug 02 00:02:11 2017 +0900
> # Branch stable
> # Node ID b6d0bcd19b4dffb2360f909f459c27104ad16565
> # Parent  76b171209151fe41dbf8dbfec473cc533f3b40ca
> # Available At https://bitbucket.org/foozy/mercurial-wip
> #              hg pull https://bitbucket.org/foozy/mercurial-wip -r b6d0bcd19b4d
> # EXP-Topic i18n-fix-update-pot-issues
> i18n: use actual filename, in which function is defined, for hg.pot

queued, thanks
Yuya Nishihara - Aug. 3, 2017, 3:36 p.m.
On Wed, 02 Aug 2017 22:36:45 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> # Date 1501599731 -32400
> #      Wed Aug 02 00:02:11 2017 +0900
> # Branch stable
> # Node ID b6d0bcd19b4dffb2360f909f459c27104ad16565
> # Parent  76b171209151fe41dbf8dbfec473cc533f3b40ca
> # Available At https://bitbucket.org/foozy/mercurial-wip
> #              hg pull https://bitbucket.org/foozy/mercurial-wip -r b6d0bcd19b4d
> # EXP-Topic i18n-fix-update-pot-issues
> i18n: use actual filename, in which function is defined, for hg.pot

> +            funcmod = inspect.getmodule(func)
> +            extra = ''
> +            if funcmod.__package__ == funcmod.__name__:
> +                extra = '/__init__'
> +            actualpath = '%s%s.py' % (funcmod.__name__.replace('.', '/'), extra)
> +
>              src = inspect.getsource(func)
> -            name = "%s.%s" % (path, func.__name__)
> +            name = "%s.%s" % (actualpath, func.__name__)
>              lineno = inspect.getsourcelines(func)[1]

Perhaps inspect.getsourcefile() can be used.
Katsunori FUJIWARA - Aug. 4, 2017, 6:16 p.m.
At Fri, 4 Aug 2017 00:36:28 +0900,
Yuya Nishihara wrote:
> 
> On Wed, 02 Aug 2017 22:36:45 +0900, FUJIWARA Katsunori wrote:
> > # HG changeset patch
> > # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> > # Date 1501599731 -32400
> > #      Wed Aug 02 00:02:11 2017 +0900
> > # Branch stable
> > # Node ID b6d0bcd19b4dffb2360f909f459c27104ad16565
> > # Parent  76b171209151fe41dbf8dbfec473cc533f3b40ca
> > # Available At https://bitbucket.org/foozy/mercurial-wip
> > #              hg pull https://bitbucket.org/foozy/mercurial-wip -r b6d0bcd19b4d
> > # EXP-Topic i18n-fix-update-pot-issues
> > i18n: use actual filename, in which function is defined, for hg.pot
> 
> > +            funcmod = inspect.getmodule(func)
> > +            extra = ''
> > +            if funcmod.__package__ == funcmod.__name__:
> > +                extra = '/__init__'
> > +            actualpath = '%s%s.py' % (funcmod.__name__.replace('.', '/'), extra)
> > +
> >              src = inspect.getsource(func)
> > -            name = "%s.%s" % (path, func.__name__)
> > +            name = "%s.%s" % (actualpath, func.__name__)
> >              lineno = inspect.getsourcelines(func)[1]
> 
> Perhaps inspect.getsourcefile() can be used.
> 

Ah, I overlooked it. I'll post follow up patch on default. Thanks!
Katsunori FUJIWARA - Aug. 12, 2017, 3:13 p.m.
At Sat, 05 Aug 2017 03:16:57 +0900,
FUJIWARA Katsunori wrote:
> 
> At Fri, 4 Aug 2017 00:36:28 +0900,
> Yuya Nishihara wrote:
> > 
> > On Wed, 02 Aug 2017 22:36:45 +0900, FUJIWARA Katsunori wrote:
> > > # HG changeset patch
> > > # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> > > # Date 1501599731 -32400
> > > #      Wed Aug 02 00:02:11 2017 +0900
> > > # Branch stable
> > > # Node ID b6d0bcd19b4dffb2360f909f459c27104ad16565
> > > # Parent  76b171209151fe41dbf8dbfec473cc533f3b40ca
> > > # Available At https://bitbucket.org/foozy/mercurial-wip
> > > #              hg pull https://bitbucket.org/foozy/mercurial-wip -r b6d0bcd19b4d
> > > # EXP-Topic i18n-fix-update-pot-issues
> > > i18n: use actual filename, in which function is defined, for hg.pot
> > 
> > > +            funcmod = inspect.getmodule(func)
> > > +            extra = ''
> > > +            if funcmod.__package__ == funcmod.__name__:
> > > +                extra = '/__init__'
> > > +            actualpath = '%s%s.py' % (funcmod.__name__.replace('.', '/'), extra)
> > > +
> > >              src = inspect.getsource(func)
> > > -            name = "%s.%s" % (path, func.__name__)
> > > +            name = "%s.%s" % (actualpath, func.__name__)
> > >              lineno = inspect.getsourcelines(func)[1]
> > 
> > Perhaps inspect.getsourcefile() can be used.
> > 
> 
> Ah, I overlooked it. I'll post follow up patch on default. Thanks!

I gave up using inspect.getsourcefile() in this case, because:

  - hggettext insert result of os.getcwd() into sys.path at first, in
    order to avoid loading modules from already installed Mercurial,

  - and this makes inspect.getsourcefile() return absolute path

Even with "." instead of os.getcwd(), we should trim additional "./"
prefix of filenames returned by inspect.getsourcefile(), in order to
keep current order of entries in hg.pot.

If we omit trimming this additional "./", order of entries in hg.pot
is drastically changed, and it might cause meaningless changes of *.po
files.

Simple replacement with inspect.getsourcefile() doesn't work as we
expected at first :-<


> -- 
> ----------------------------------------------------------------------
> [FUJIWARA Katsunori]                             foozy@lares.dti.ne.jp
>
Yuya Nishihara - Aug. 13, 2017, 2:25 a.m.
On Sun, 13 Aug 2017 00:13:58 +0900, FUJIWARA Katsunori wrote:
> At Sat, 05 Aug 2017 03:16:57 +0900,
> FUJIWARA Katsunori wrote:
> > 
> > At Fri, 4 Aug 2017 00:36:28 +0900,
> > Yuya Nishihara wrote:
> > > 
> > > On Wed, 02 Aug 2017 22:36:45 +0900, FUJIWARA Katsunori wrote:
> > > > # HG changeset patch
> > > > # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> > > > # Date 1501599731 -32400
> > > > #      Wed Aug 02 00:02:11 2017 +0900
> > > > # Branch stable
> > > > # Node ID b6d0bcd19b4dffb2360f909f459c27104ad16565
> > > > # Parent  76b171209151fe41dbf8dbfec473cc533f3b40ca
> > > > # Available At https://bitbucket.org/foozy/mercurial-wip
> > > > #              hg pull https://bitbucket.org/foozy/mercurial-wip -r b6d0bcd19b4d
> > > > # EXP-Topic i18n-fix-update-pot-issues
> > > > i18n: use actual filename, in which function is defined, for hg.pot
> > > 
> > > > +            funcmod = inspect.getmodule(func)
> > > > +            extra = ''
> > > > +            if funcmod.__package__ == funcmod.__name__:
> > > > +                extra = '/__init__'
> > > > +            actualpath = '%s%s.py' % (funcmod.__name__.replace('.', '/'), extra)
> > > > +
> > > >              src = inspect.getsource(func)
> > > > -            name = "%s.%s" % (path, func.__name__)
> > > > +            name = "%s.%s" % (actualpath, func.__name__)
> > > >              lineno = inspect.getsourcelines(func)[1]
> > > 
> > > Perhaps inspect.getsourcefile() can be used.
> > > 
> > 
> > Ah, I overlooked it. I'll post follow up patch on default. Thanks!
> 
> I gave up using inspect.getsourcefile() in this case, because:
> 
>   - hggettext insert result of os.getcwd() into sys.path at first, in
>     order to avoid loading modules from already installed Mercurial,
> 
>   - and this makes inspect.getsourcefile() return absolute path
> 
> Even with "." instead of os.getcwd(), we should trim additional "./"
> prefix of filenames returned by inspect.getsourcefile(), in order to
> keep current order of entries in hg.pot.
> 
> If we omit trimming this additional "./", order of entries in hg.pot
> is drastically changed, and it might cause meaningless changes of *.po
> files.
> 
> Simple replacement with inspect.getsourcefile() doesn't work as we
> expected at first :-<

Fair enough. Thanks for trying that.

Patch

diff --git a/i18n/hggettext b/i18n/hggettext
--- a/i18n/hggettext
+++ b/i18n/hggettext
@@ -112,14 +112,20 @@  def docstrings(path):
 
     for func, rstrip in functions:
         if func.__doc__:
+            funcmod = inspect.getmodule(func)
+            extra = ''
+            if funcmod.__package__ == funcmod.__name__:
+                extra = '/__init__'
+            actualpath = '%s%s.py' % (funcmod.__name__.replace('.', '/'), extra)
+
             src = inspect.getsource(func)
-            name = "%s.%s" % (path, func.__name__)
+            name = "%s.%s" % (actualpath, func.__name__)
             lineno = inspect.getsourcelines(func)[1]
             doc = func.__doc__
             if rstrip:
                 doc = doc.rstrip()
             lineno += offset(src, doc, name, 1)
-            print(poentry(path, lineno, doc))
+            print(poentry(actualpath, lineno, doc))
 
 
 def rawtext(path):