11#! /usr/bin/env python3
2- # -*- coding: iso-8859-1 -*-
3- # Originally written by Barry Warsaw <[email protected] > 4- #
5- # Minimally patched to make it even more xgettext compatible
6- # by Peter Funk <[email protected] > 7- #
8- # 2002-11-22 Jürgen Hermann <[email protected] > 9- # Added checks that _() only contains string literals, and
10- # command line args are resolved to module lists, i.e. you
11- # can now pass a filename, a module or package name, or a
12- # directory (including globbing chars, important for Win32).
13- # Made docstring fit in 80 chars wide displays using pydoc.
14- #
15-
16- # for selftesting
17- try :
18- import fintl
19- _ = fintl .gettext
20- except ImportError :
21- _ = lambda s : s
222
23- __doc__ = _ ( """pygettext -- Python equivalent of xgettext(1)
3+ """pygettext -- Python equivalent of xgettext(1)
244
255Many systems (Solaris, Linux, Gnu) provide extensive tools that ease the
266internationalization of C programs. Most of these tools are independent of
153133 conjunction with the -D option above.
154134
155135If `inputfile' is -, standard input is read.
156- """ )
136+ """
157137
158- import os
138+ import ast
139+ import getopt
140+ import glob
159141import importlib .machinery
160142import importlib .util
143+ import os
161144import sys
162- import glob
163145import time
164- import getopt
165- import ast
166146import tokenize
167147from collections import defaultdict
168148from dataclasses import dataclass , field
173153
174154# The normal pot-file header. msgmerge and Emacs's po-mode work better if it's
175155# there.
176- pot_header = _ ( '''\
156+ pot_header = '''\
177157 # SOME DESCRIPTIVE TITLE.
178158# Copyright (C) YEAR ORGANIZATION
179159# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
190170"Content-Transfer-Encoding: %(encoding)s\\ n"
191171"Generated-By: pygettext.py %(version)s\\ n"
192172
193- ''' )
173+ '''
194174
195175
196176def usage (code , msg = '' ):
@@ -204,7 +184,7 @@ def make_escapes(pass_nonascii):
204184 global escapes , escape
205185 if pass_nonascii :
206186 # Allow non-ascii characters to pass through so that e.g. 'msgid
207- # "Höhe"' would result not result in 'msgid "H\366he"'. Otherwise we
187+ # "Höhe"' would result not result in 'msgid "H\366he"'. Otherwise we
208188 # escape any character outside the 32..126 range.
209189 mod = 128
210190 escape = escape_ascii
@@ -224,6 +204,7 @@ def make_escapes(pass_nonascii):
224204def escape_ascii (s , encoding ):
225205 return '' .join (escapes [ord (c )] if ord (c ) < 128 else c for c in s )
226206
207+
227208def escape_nonascii (s , encoding ):
228209 return '' .join (escapes [b ] for b in s .encode (encoding ))
229210
@@ -416,7 +397,7 @@ def __waiting(self, ttype, tstring, lineno):
416397 if func_name not in opts .keywords :
417398 continue
418399 if len (call .args ) != 1 :
419- print (_ (
400+ print ((
420401 '*** %(file)s:%(lineno)s: Seen unexpected amount of'
421402 ' positional arguments in gettext call: %(source_segment)s'
422403 ) % {
@@ -426,7 +407,7 @@ def __waiting(self, ttype, tstring, lineno):
426407 }, file = sys .stderr )
427408 continue
428409 if call .keywords :
429- print (_ (
410+ print ((
430411 '*** %(file)s:%(lineno)s: Seen unexpected keyword arguments'
431412 ' in gettext call: %(source_segment)s'
432413 ) % {
@@ -437,7 +418,7 @@ def __waiting(self, ttype, tstring, lineno):
437418 continue
438419 arg = call .args [0 ]
439420 if not isinstance (arg , ast .Constant ):
440- print (_ (
421+ print ((
441422 '*** %(file)s:%(lineno)s: Seen unexpected argument type'
442423 ' in gettext call: %(source_segment)s'
443424 ) % {
@@ -550,7 +531,7 @@ def __addentry(self, msg, lineno=None, *, is_docstring=False):
550531 )
551532
552533 def warn_unexpected_token (self , token ):
553- print (_ (
534+ print ((
554535 '*** %(file)s:%(lineno)s: Seen unexpected token "%(token)s"'
555536 ) % {
556537 'token' : token ,
@@ -677,21 +658,21 @@ class Options:
677658 elif opt in ('-S' , '--style' ):
678659 options .locationstyle = locations .get (arg .lower ())
679660 if options .locationstyle is None :
680- usage (1 , _ ( 'Invalid value for --style: %s' ) % arg )
661+ usage (1 , f 'Invalid value for --style: { arg } ' )
681662 elif opt in ('-o' , '--output' ):
682663 options .outfile = arg
683664 elif opt in ('-p' , '--output-dir' ):
684665 options .outpath = arg
685666 elif opt in ('-v' , '--verbose' ):
686667 options .verbose = 1
687668 elif opt in ('-V' , '--version' ):
688- print (_ ( 'pygettext.py (xgettext for Python) %s' ) % __version__ )
669+ print (f 'pygettext.py (xgettext for Python) { __version__ } ' )
689670 sys .exit (0 )
690671 elif opt in ('-w' , '--width' ):
691672 try :
692673 options .width = int (arg )
693674 except ValueError :
694- usage (1 , _ ( '--width argument must be an integer: %s' ) % arg )
675+ usage (1 , f '--width argument must be an integer: { arg } ' )
695676 elif opt in ('-x' , '--exclude-file' ):
696677 options .excludefilename = arg
697678 elif opt in ('-X' , '--no-docstrings' ):
@@ -719,8 +700,8 @@ class Options:
719700 with open (options .excludefilename ) as fp :
720701 options .toexclude = fp .readlines ()
721702 except IOError :
722- print (_ (
723- "Can't read --exclude-file: %s" ) % options . excludefilename , file = sys .stderr )
703+ print (f"Can't read --exclude-file: { options . excludefilename } " ,
704+ file = sys .stderr )
724705 sys .exit (1 )
725706 else :
726707 options .toexclude = []
@@ -739,12 +720,12 @@ class Options:
739720 for filename in args :
740721 if filename == '-' :
741722 if options .verbose :
742- print (_ ( 'Reading standard input' ) )
723+ print ('Reading standard input' )
743724 fp = sys .stdin .buffer
744725 closep = 0
745726 else :
746727 if options .verbose :
747- print (_ ( 'Working on %s' ) % filename )
728+ print (f 'Working on { filename } ' )
748729 fp = open (filename , 'rb' )
749730 closep = 1
750731 try :
@@ -779,7 +760,3 @@ class Options:
779760
780761if __name__ == '__main__' :
781762 main ()
782- # some more test strings
783- # this one creates a warning
784- _ ('*** Seen unexpected token "%(token)s"' ) % {'token' : 'test' }
785- _ ('more' 'than' 'one' 'string' )
0 commit comments