Mercurial > p > roundup > code
comparison tools/pygettext.py @ 423:c64ce58ee529
Resolve pygettext args; described command line calls
| author | Jürgen Hermann <jhermann@users.sourceforge.net> |
|---|---|
| date | Fri, 30 Nov 2001 01:30:32 +0000 |
| parents | f0b234ce301f |
| children | 8babfb52ebbd |
comparison
equal
deleted
inserted
replaced
| 422:0f9a59c90e53 | 423:c64ce58ee529 |
|---|---|
| 229 lines[i] = escape(lines[i]) | 229 lines[i] = escape(lines[i]) |
| 230 lineterm = '\\n"\n"' | 230 lineterm = '\\n"\n"' |
| 231 s = '""\n"' + lineterm.join(lines) + '"' | 231 s = '""\n"' + lineterm.join(lines) + '"' |
| 232 return s | 232 return s |
| 233 | 233 |
| 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 [] | |
| 234 | 332 |
| 235 | 333 |
| 236 class TokenEater: | 334 class TokenEater: |
| 237 def __init__(self, options): | 335 def __init__(self, options): |
| 238 self.__options = options | 336 self.__options = options |
| 415 options.excludefilename) | 513 options.excludefilename) |
| 416 sys.exit(1) | 514 sys.exit(1) |
| 417 else: | 515 else: |
| 418 options.toexclude = [] | 516 options.toexclude = [] |
| 419 | 517 |
| 420 # on win32, do internal globbing | 518 # resolve args to module lists |
| 421 if sys.platform == 'win32': | 519 expanded = [] |
| 422 import glob | 520 for arg in args: |
| 423 expanded = [] | 521 expanded.extend(getFilesForName(arg)) |
| 424 for arg in args: | 522 args = expanded |
| 425 expanded.extend(glob.glob(arg)) | |
| 426 args = expanded | |
| 427 | 523 |
| 428 # slurp through all the files | 524 # slurp through all the files |
| 429 eater = TokenEater(options) | 525 eater = TokenEater(options) |
| 430 for filename in args: | 526 for filename in args: |
| 431 if filename == '-': | 527 if filename == '-': |
