New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addressing problems raised in sphinx_issues #10104 #10379
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments:
A
sphinx/builders/gettext.py
Outdated
@@ -32,7 +32,7 @@ class Message: | |||
"""An entry of translatable message.""" | |||
def __init__(self, text: str, locations: List[Tuple[str, int]], uuids: List[str]): | |||
self.text = text | |||
self.locations = locations | |||
self.locations = list(set(locations)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use sorted
here -- set
doesn't guarantee any sort order at a language level.
self.locations = list(set(locations)) | |
self.locations = sorted({*locations}) |
sphinx/builders/gettext.py
Outdated
for message in self.messages: | ||
positions = [(source, line) for source, line, uuid in self.metadata[message]] | ||
positions = [(os.path.relpath(source, start=os.getcwd()), line) for source, line, uuid in self.metadata[message]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would call os.getcwd()
for every item in the loop. Does ommitting start=
still give a usable result? Otherwise you could use cache os.getcwd()
in a local.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understandable change.
But this should be moved to the outside of this class because this class does not have appropriate directory information. I think what directory you want is the base directory of the document (a.k.a srcdir). But it seems you're using os.getcwd()
. But it does not equal to srcdir. It usually equals to the confdir that contains conf.py, but not promised. It sometimes returns a different directory if 3rd party extension changes it.
BTW, please use sphinx.util.osutil:relpath()
instead of os.path.relpath()
. It will not be crashed even if path
and start
are on different drives on Windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the issue #10104 after the code reviews from Adam Turner and Takeshi Komiya:
- Added the class variable 'builder' to Catalog class, so any instances of Catalog can access the information within the builder, including things such as app, config, srcdir, outdir etc.. see MessageCatalogBuilder for details.
- Initialise this Catalog.builder as the instance of MessageCatalogBuilder is created, ie. in the init()
- As Message class is created, list of locations passing to it will be made singular, using set(), sorted alphabetically for easier searching and identifying origins where the message can be found.
- source dir is used within the iter of the Catalog, using sphinx.util.osutil.relpath, for safer functionality, to work out the relative path for each location entry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the class variable 'builder' to Catalog class
Modifying global state like this is a bad idea, please could you refactor?
A
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, adding a class variable is not a good idea. Catalog
class is a data class that represents message catalog. It is not related to the builder at all. So the path manipulation using builder should be called outside of the catalog class. How about processing just before calling GettextRenderer
?
@@ -1,5 +1,5 @@ | |||
"""The MessageCatalogBuilder class.""" | |||
|
|||
import os |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep a blank line here
import os | |
import os |
Thank you so much for these feedbacks. I will return to fix the code at a later time and post another commit to the pull request.
Best Regards,
Hoang Tran
… On 23 Apr 2022, at 12:09, Adam Turner ***@***.***> wrote:
@AA-Turner requested changes on this pull request.
A few comments:
A
In sphinx/builders/gettext.py <#10379 (comment)>:
> @@ -32,7 +32,7 @@ class Message:
"""An entry of translatable message."""
def __init__(self, text: str, locations: List[Tuple[str, int]], uuids: List[str]):
self.text = text
- self.locations = locations
+ self.locations = list(set(locations))
I'd use sorted here -- set doesn't guarantee any sort order at a language level.
⬇️ Suggested change
- self.locations = list(set(locations))
+ self.locations = sorted({*locations})
In sphinx/builders/gettext.py <#10379 (comment)>:
> for message in self.messages:
- positions = [(source, line) for source, line, uuid in self.metadata[message]]
+ positions = [(os.path.relpath(source, start=os.getcwd()), line) for source, line, uuid in self.metadata[message]]
This would call os.getcwd() for every item in the loop. Does ommitting start= still give a usable result? Otherwise you could use cache os.getcwd() in a local.
In sphinx/builders/gettext.py <#10379 (comment)>:
> @@ -1,5 +1,5 @@
"""The MessageCatalogBuilder class."""
-
+import os
Keep a blank line here
⬇️ Suggested change
-import os
+
+import os
—
Reply to this email directly, view it on GitHub <#10379 (review)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AKIONBYYLLCUQ4CPX5HMNNDVGPK6RANCNFSM5UEIUEYQ>.
You are receiving this because you authored the thread.
|
…ner and Takeshi Komiya: - Added the class variable 'builder' to Catalog class, so any instances of Catalog can access the information within the builder, including things such as app, config, srcdir, outdir etc.. see MessageCatalogBuilder for details. - Initialise this Catalog.builder as the instance of MessageCatalogBuilder is created, ie. in the init() - As Message class is created, list of locations passing to it will be made singular, using set(), sorted alphabetically for easier searching and identifying origins where the message can be found. - source dir is used within the __iter__ of the Catalog, using sphinx.util.osutil.relpath, for safer functionality, to work out the relative path for each location entry.
@@ -32,13 +32,14 @@ class Message: | |||
"""An entry of translatable message.""" | |||
def __init__(self, text: str, locations: List[Tuple[str, int]], uuids: List[str]): | |||
self.text = text | |||
self.locations = locations | |||
self.locations = list(sorted(list(set(locations)))) #make list unique and sorted alphabetically |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorted
returns a list and can take a set as input
self.locations = list(sorted(list(set(locations)))) #make list unique and sorted alphabetically | |
self.locations = sorted(set(locations)) # sort and de-duplicate the locations list |
Finally, I reimplemented this as #10104 and merged it. Thank you for your contribution! |
I would like to add a little comment as I only found out today after using
to remove duplicated user comments |
Sorry the above solution is NOT perfect yet. I have just found another incident where the
are duplicated, by running
these two files are in the following links: https://gitlab.com/inkscape/inkscape/-/jobs/2544777172/artifacts/file/po/inkscape.pot The output file
The using the Sphinx code to flat out the comment sections and msgid, msgstr using:
The result file shown this line is repeated twice:
I have to use the PyCharmIDE to debug and found out, so the correction code is this:
the section surrounded by '**". This is an extraction from the file
routine:
Ok, I'm out. |
Subject: Solving problems raised in issues #10104
Purpose
Detail
Relates