Added the -X/--no-docstrings flag which takes a filename containing a

list of files to not extract docstrings from when the -D option is
given.  This isn't optimal, but I didn't want to change the semantics
of -D, and it's bad form to allow optional switch arguments.

Bumping __version__ to 1.4.

TokenEater.__init__(): Initialize __curfile to None.

__waiting(): In order to extract docstrings from the module, both the
    -D flag should be set, and the __curfile should not be named in
    the -X filename (i.e. it isn't in opts.nodocstrings).

set_filename(): Fixed a bug where once the first module docstring is
    extracted, no subsequent module docstrings will be extracted.  The
    bug was that the first extraction set __freshmodule to 0, but that
    flag was never reset back to 1.  set_filename() is always called
    when the next file is being processed, so use it to reset the
    __freshmodule flag.

main(): Add support for -X/--no-docstring.
This commit is contained in:
Barry Warsaw 2001-07-27 16:47:18 +00:00
parent 702d08ec05
commit 63ce5af496
1 changed files with 32 additions and 13 deletions

View File

@ -1,5 +1,5 @@
#! /usr/bin/env python #! /usr/bin/env python
# Originally written by Barry Warsaw <barry@digicool.com> # Originally written by Barry Warsaw <barry@zope.com>
# #
# Minimally patched to make it even more xgettext compatible # Minimally patched to make it even more xgettext compatible
# by Peter Funk <pf@artcom-gmbh.de> # by Peter Funk <pf@artcom-gmbh.de>
@ -49,11 +49,11 @@ Options:
-a -a
--extract-all --extract-all
Extract all strings Extract all strings.
-d name -d name
--default-domain=name --default-domain=name
Rename the default output file from messages.pot to name.pot Rename the default output file from messages.pot to name.pot.
-E -E
--escape --escape
@ -63,11 +63,11 @@ Options:
--docstrings --docstrings
Extract module, class, method, and function docstrings. These do not Extract module, class, method, and function docstrings. These do not
need to be wrapped in _() markers, and in fact cannot be for Python to need to be wrapped in _() markers, and in fact cannot be for Python to
consider them docstrings. consider them docstrings. (See also the -X option).
-h -h
--help --help
print this help message and exit Print this help message and exit.
-k word -k word
--keyword=word --keyword=word
@ -128,8 +128,13 @@ Options:
extracted from the input files. Each string to be excluded must extracted from the input files. Each string to be excluded must
appear on a line by itself in the file. appear on a line by itself in the file.
If `inputfile' is -, standard input is read. -X filename
--no-docstrings=filename
Specify a file that contains a list of files (one per line) that
should not have their docstrings extracted. This is only useful in
conjunction with the -D option above.
If `inputfile' is -, standard input is read.
""" """
import os import os
@ -146,7 +151,7 @@ try:
except ImportError: except ImportError:
def _(s): return s def _(s): return s
__version__ = '1.3' __version__ = '1.4'
default_keywords = ['_'] default_keywords = ['_']
DEFAULTKEYWORDS = ', '.join(default_keywords) DEFAULTKEYWORDS = ', '.join(default_keywords)
@ -155,8 +160,8 @@ EMPTYSTRING = ''
# The normal pot-file header. msgmerge and EMACS' po-mode work better if # The normal pot-file header. msgmerge and Emacs's po-mode work better if it's
# it's there. # there.
pot_header = _('''\ pot_header = _('''\
# SOME DESCRIPTIVE TITLE. # SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION # Copyright (C) YEAR ORGANIZATION
@ -247,6 +252,7 @@ class TokenEater:
self.__data = [] self.__data = []
self.__lineno = -1 self.__lineno = -1
self.__freshmodule = 1 self.__freshmodule = 1
self.__curfile = None
def __call__(self, ttype, tstring, stup, etup, line): def __call__(self, ttype, tstring, stup, etup, line):
# dispatch # dispatch
@ -256,8 +262,9 @@ class TokenEater:
self.__state(ttype, tstring, stup[0]) self.__state(ttype, tstring, stup[0])
def __waiting(self, ttype, tstring, lineno): def __waiting(self, ttype, tstring, lineno):
opts = self.__options
# Do docstring extractions, if enabled # Do docstring extractions, if enabled
if self.__options.docstrings: if opts.docstrings and not opts.nodocstrings.get(self.__curfile):
# module docstring? # module docstring?
if self.__freshmodule: if self.__freshmodule:
if ttype == tokenize.STRING: if ttype == tokenize.STRING:
@ -270,7 +277,7 @@ class TokenEater:
if ttype == tokenize.NAME and tstring in ('class', 'def'): if ttype == tokenize.NAME and tstring in ('class', 'def'):
self.__state = self.__suiteseen self.__state = self.__suiteseen
return return
if ttype == tokenize.NAME and tstring in self.__options.keywords: if ttype == tokenize.NAME and tstring in opts.keywords:
self.__state = self.__keywordseen self.__state = self.__keywordseen
def __suiteseen(self, ttype, tstring, lineno): def __suiteseen(self, ttype, tstring, lineno):
@ -318,6 +325,7 @@ class TokenEater:
def set_filename(self, filename): def set_filename(self, filename):
self.__curfile = filename self.__curfile = filename
self.__freshmodule = 1
def write(self, fp): def write(self, fp):
options = self.__options options = self.__options
@ -383,12 +391,12 @@ def main():
try: try:
opts, args = getopt.getopt( opts, args = getopt.getopt(
sys.argv[1:], sys.argv[1:],
'ad:DEhk:Kno:p:S:Vvw:x:', 'ad:DEhk:Kno:p:S:Vvw:x:X:',
['extract-all', 'default-domain=', 'escape', 'help', ['extract-all', 'default-domain=', 'escape', 'help',
'keyword=', 'no-default-keywords', 'keyword=', 'no-default-keywords',
'add-location', 'no-location', 'output=', 'output-dir=', 'add-location', 'no-location', 'output=', 'output-dir=',
'style=', 'verbose', 'version', 'width=', 'exclude-file=', 'style=', 'verbose', 'version', 'width=', 'exclude-file=',
'docstrings', 'docstrings', 'no-docstrings',
]) ])
except getopt.error, msg: except getopt.error, msg:
usage(1, msg) usage(1, msg)
@ -410,6 +418,7 @@ def main():
width = 78 width = 78
excludefilename = '' excludefilename = ''
docstrings = 0 docstrings = 0
nodocstrings = {}
options = Options() options = Options()
locations = {'gnu' : options.GNU, locations = {'gnu' : options.GNU,
@ -456,6 +465,16 @@ def main():
usage(1, _('--width argument must be an integer: %s') % arg) usage(1, _('--width argument must be an integer: %s') % arg)
elif opt in ('-x', '--exclude-file'): elif opt in ('-x', '--exclude-file'):
options.excludefilename = arg options.excludefilename = arg
elif opt in ('-X', '--no-docstrings'):
fp = open(arg)
try:
while 1:
line = fp.readline()
if not line:
break
options.nodocstrings[line[:-1]] = 1
finally:
fp.close()
# calculate escapes # calculate escapes
make_escapes(options.escape) make_escapes(options.escape)