home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_keyword.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  2.1 KB  |  97 lines

  1. #! /usr/bin/env python
  2.  
  3. """Keywords (from "graminit.c")
  4.  
  5. This file is automatically generated; please don't muck it up!
  6.  
  7. To update the symbols in this file, 'cd' to the top directory of
  8. the python source tree after building the interpreter and run:
  9.  
  10.     python Lib/keyword.py
  11. """
  12.  
  13. __all__ = ["iskeyword"]
  14.  
  15. kwlist = [
  16. #--start keywords--
  17.         'and',
  18.         'assert',
  19.         'break',
  20.         'class',
  21.         'continue',
  22.         'def',
  23.         'del',
  24.         'elif',
  25.         'else',
  26.         'except',
  27.         'exec',
  28.         'finally',
  29.         'for',
  30.         'from',
  31.         'global',
  32.         'if',
  33.         'import',
  34.         'in',
  35.         'is',
  36.         'lambda',
  37.         'not',
  38.         'or',
  39.         'pass',
  40.         'print',
  41.         'raise',
  42.         'return',
  43.         'try',
  44.         'while',
  45. #--end keywords--
  46.         ]
  47.  
  48. kwdict = {}
  49. for keyword in kwlist:
  50.     kwdict[keyword] = 1
  51.  
  52. iskeyword = kwdict.has_key
  53.  
  54. def main():
  55.     import sys, re
  56.  
  57.     args = sys.argv[1:]
  58.     iptfile = args and args[0] or "Python/graminit.c"
  59.     if len(args) > 1: optfile = args[1]
  60.     else: optfile = "Lib/keyword.py"
  61.  
  62.     # scan the source file for keywords
  63.     fp = open(iptfile)
  64.     strprog = re.compile('"([^"]+)"')
  65.     lines = []
  66.     while 1:
  67.         line = fp.readline()
  68.         if not line: break
  69.         if line.find('{1, "') > -1:
  70.             match = strprog.search(line)
  71.             if match:
  72.                 lines.append("        '" + match.group(1) + "',\n")
  73.     fp.close()
  74.     lines.sort()
  75.  
  76.     # load the output skeleton from the target
  77.     fp = open(optfile)
  78.     format = fp.readlines()
  79.     fp.close()
  80.  
  81.     # insert the lines of keywords
  82.     try:
  83.         start = format.index("#--start keywords--\n") + 1
  84.         end = format.index("#--end keywords--\n")
  85.         format[start:end] = lines
  86.     except ValueError:
  87.         sys.stderr.write("target does not contain format markers\n")
  88.         sys.exit(1)
  89.  
  90.     # write the output file
  91.     fp = open(optfile, 'w')
  92.     fp.write(''.join(format))
  93.     fp.close()
  94.  
  95. if __name__ == "__main__":
  96.     main()
  97.