/Lib/curses/__init__.py

http://unladen-swallow.googlecode.com/ · Python · 59 lines · 26 code · 9 blank · 24 comment · 7 complexity · 1be42d05971db94a8a24611dd2d4da30 MD5 · raw file

  1. """curses
  2. The main package for curses support for Python. Normally used by importing
  3. the package, and perhaps a particular module inside it.
  4. import curses
  5. from curses import textpad
  6. curses.initwin()
  7. ...
  8. """
  9. __revision__ = "$Id: __init__.py 61064 2008-02-25 16:29:58Z andrew.kuchling $"
  10. from _curses import *
  11. from curses.wrapper import wrapper
  12. import os as _os
  13. import sys as _sys
  14. # Some constants, most notably the ACS_* ones, are only added to the C
  15. # _curses module's dictionary after initscr() is called. (Some
  16. # versions of SGI's curses don't define values for those constants
  17. # until initscr() has been called.) This wrapper function calls the
  18. # underlying C initscr(), and then copies the constants from the
  19. # _curses module to the curses package's dictionary. Don't do 'from
  20. # curses import *' if you'll be needing the ACS_* constants.
  21. def initscr():
  22. import _curses, curses
  23. # we call setupterm() here because it raises an error
  24. # instead of calling exit() in error cases.
  25. setupterm(term=_os.environ.get("TERM", "unknown"),
  26. fd=_sys.__stdout__.fileno())
  27. stdscr = _curses.initscr()
  28. for key, value in _curses.__dict__.items():
  29. if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'):
  30. setattr(curses, key, value)
  31. return stdscr
  32. # This is a similar wrapper for start_color(), which adds the COLORS and
  33. # COLOR_PAIRS variables which are only available after start_color() is
  34. # called.
  35. def start_color():
  36. import _curses, curses
  37. retval = _curses.start_color()
  38. if hasattr(_curses, 'COLORS'):
  39. curses.COLORS = _curses.COLORS
  40. if hasattr(_curses, 'COLOR_PAIRS'):
  41. curses.COLOR_PAIRS = _curses.COLOR_PAIRS
  42. return retval
  43. # Import Python has_key() implementation if _curses doesn't contain has_key()
  44. try:
  45. has_key
  46. except NameError:
  47. from has_key import has_key