Skip to content

Commit a019d0e

Browse files
author
Jürgen Hermann
committed
Resolve pygettext args; described command line calls
1 parent 6f808f1 commit a019d0e

File tree

2 files changed

+115
-7
lines changed

2 files changed

+115
-7
lines changed

I18N_PROGRESS.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ This list has been generated using the MANIFEST file. We should be able to
22
write a simple script to compare the two and make sure that all MANIFEST
33
files appear in here.
44

5+
To generate a messages.pot file, use this command:
6+
7+
python tools/pygettext.py roundup roundup-* cgi-bin/roundup.cgi
8+
9+
"messages.pot" then contains a positive list of files using _(), which can
10+
be extracted by;
11+
12+
grep "#: " messages.pot | tr ":#0123456789 " "\012" | sort | uniq
13+
14+
Of course, this does not check whether a file is fully translated, only
15+
whether there is at least one use of "_()".
16+
517

618
THESE FILES DO NOT USE _()
719
==========================

tools/pygettext.py

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,104 @@ def normalize(s):
231231
s = '""\n"' + lineterm.join(lines) + '"'
232232
return s
233233

234+
235+
236+
def containsAny(str, set):
237+
""" Check whether 'str' contains ANY of the chars in 'set'
238+
"""
239+
return 1 in [c in str for c in set]
240+
241+
242+
def _visit_pyfiles(list, dirname, names):
243+
""" Helper for getFilesForName().
244+
"""
245+
# get extension for python source files
246+
if not globals().has_key('_py_ext'):
247+
import imp
248+
global _py_ext
249+
_py_ext = [triple[0] for triple in imp.get_suffixes() if triple[2] == imp.PY_SOURCE][0]
250+
251+
# don't recurse into CVS directories
252+
if 'CVS' in names:
253+
names.remove('CVS')
254+
255+
# add all *.py files to list
256+
list.extend(
257+
[os.path.join(dirname, file)
258+
for file in names
259+
if os.path.splitext(file)[1] == _py_ext])
260+
261+
262+
def _get_modpkg_path(dotted_name, pathlist=None):
263+
""" Get the filesystem path for a module or a package.
264+
265+
Return the file system path to a file for a module,
266+
and to a directory for a package. Return None if
267+
the name is not found, or is a builtin or extension module.
268+
"""
269+
import imp
270+
271+
# split off top-most name
272+
parts = dotted_name.split('.', 1)
273+
274+
if len(parts) > 1:
275+
# we have a dotted path, import top-level package
276+
try:
277+
file, pathname, description = imp.find_module(parts[0], pathlist)
278+
if file: file.close()
279+
except ImportError:
280+
return None
281+
282+
# check if it's indeed a package
283+
if description[2] == imp.PKG_DIRECTORY:
284+
# recursively handle the remaining name parts
285+
pathname = _get_modpkg_path(parts[1], [pathname])
286+
else:
287+
pathname = None
288+
else:
289+
# plain name
290+
try:
291+
file, pathname, description = imp.find_module(dotted_name, pathlist)
292+
if file: file.close()
293+
if description[2] not in [imp.PY_SOURCE, imp.PKG_DIRECTORY]:
294+
pathname = None
295+
except ImportError:
296+
pathname = None
297+
298+
return pathname
299+
300+
301+
def getFilesForName(name):
302+
""" Get a list of module files for a filename, a module or package name,
303+
or a directory.
304+
"""
305+
import imp
306+
307+
if not os.path.exists(name):
308+
# check for glob chars
309+
if containsAny(name, "*?[]"):
310+
import glob
311+
files = glob.glob(name)
312+
list = []
313+
for file in files:
314+
list.extend(getFilesForName(file))
315+
return list
316+
317+
# try to find module or package
318+
name = _get_modpkg_path(name)
319+
if not name:
320+
return []
321+
322+
if os.path.isdir(name):
323+
# find all python files in directory
324+
list = []
325+
os.path.walk(name, _visit_pyfiles, list)
326+
return list
327+
elif os.path.exists(name):
328+
# a single file
329+
return [name]
330+
331+
return []
234332

235333

236334
class TokenEater:
@@ -417,13 +515,11 @@ class Options:
417515
else:
418516
options.toexclude = []
419517

420-
# on win32, do internal globbing
421-
if sys.platform == 'win32':
422-
import glob
423-
expanded = []
424-
for arg in args:
425-
expanded.extend(glob.glob(arg))
426-
args = expanded
518+
# resolve args to module lists
519+
expanded = []
520+
for arg in args:
521+
expanded.extend(getFilesForName(arg))
522+
args = expanded
427523

428524
# slurp through all the files
429525
eater = TokenEater(options)

0 commit comments

Comments
 (0)