[pygettext] --package-name and --package-version unknown

aapost aapost at idontexist.club
Thu May 4 21:24:17 EDT 2023


On 5/4/23 17:38, c.buhtz at posteo.jp wrote:
> am I right to assume that "pygettext" is part of the official Python3 
> "package"? So it is OK to aks here?
> 
> How can I set the "Project-Id-Version"? With "xgettext" I would use the 
> arguments "--package-name" and "--package-version" for this but they are 
> unknown for "pygettext".

pygettext is deprecated since xgettext supports python now, so using 
xgettext is recommended.

That being said, pygettext does not support the options, but it could be 
modified pretty easily.
Untested but if you wanted to add that functionality in just create a 
modified pygettext.py with something like:


link PACKAGE and VERSION to variables:

pot_header = _('''\
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL at ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: %(packagename)s %(packageversion)s\\n"
"POT-Creation-Date: %(time)s\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: FULL NAME <EMAIL at ADDRESS>\\n"
"Language-Team: LANGUAGE <LL at li.org>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=%(charset)s\\n"
"Content-Transfer-Encoding: %(encoding)s\\n"
"Generated-By: pygettext.py %(version)s\\n"
''')

add attributes to Options class:

     class Options:
         # constants
         GNU = 1
         SOLARIS = 2
         # defaults
         extractall = 0 # FIXME: currently this option has no effect at all.
         escape = 0
         keywords = []
         outpath = ''
         outfile = 'messages.pot'
         writelocations = 1
         locationstyle = GNU
         verbose = 0
         width = 78
         excludefilename = ''
         docstrings = 0
         nodocstrings = {}
         packagename = "PACKAGE"
         packageversion = "VERSION"

modify option parsing for loop to look for new options:
     for opt, arg in opts:

         elif opt in ('--package-name',):
             options.packagename = arg
         elif opt in ('--package-version',):
             options.packageversion = arg

grab those options when generating file output:

def write(self, fp):
         options = self.__options
         packagename = options.packagename
         packageversion = options.packageversion
         timestamp = time.strftime('%Y-%m-%d %H:%M%z')
         encoding = fp.encoding if fp.encoding else 'UTF-8'
         print(pot_header % {'packagename': packagename,
                             'packageversion': packageversion,
                             'time': timestamp, 'version': __version__,
                             'charset': encoding,
                             'encoding': '8bit'}, file=fp)

(did not test, so might be a bug or two)



More information about the Python-list mailing list