_sitebuiltins.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. The objects used by the site module to add custom builtins.
  3. """
  4. # Those objects are almost immortal and they keep a reference to their module
  5. # globals. Defining them in the site module would keep too many references
  6. # alive.
  7. # Note this means this module should also avoid keep things alive in its
  8. # globals.
  9. import sys
  10. class Quitter(object):
  11. def __init__(self, name, eof):
  12. self.name = name
  13. self.eof = eof
  14. def __repr__(self):
  15. return 'Use %s() or %s to exit' % (self.name, self.eof)
  16. def __call__(self, code=None):
  17. # Shells like IDLE catch the SystemExit, but listen when their
  18. # stdin wrapper is closed.
  19. try:
  20. sys.stdin.close()
  21. except:
  22. pass
  23. raise SystemExit(code)
  24. class _Printer(object):
  25. """interactive prompt objects for printing the license text, a list of
  26. contributors and the copyright notice."""
  27. MAXLINES = 23
  28. def __init__(self, name, data, files=(), dirs=()):
  29. import os
  30. self.__name = name
  31. self.__data = data
  32. self.__lines = None
  33. self.__filenames = [os.path.join(dir, filename)
  34. for dir in dirs
  35. for filename in files]
  36. def __setup(self):
  37. if self.__lines:
  38. return
  39. data = None
  40. for filename in self.__filenames:
  41. try:
  42. with open(filename, "r") as fp:
  43. data = fp.read()
  44. break
  45. except OSError:
  46. pass
  47. if not data:
  48. data = self.__data
  49. self.__lines = data.split('\n')
  50. self.__linecnt = len(self.__lines)
  51. def __repr__(self):
  52. self.__setup()
  53. if len(self.__lines) <= self.MAXLINES:
  54. return "\n".join(self.__lines)
  55. else:
  56. return "Type %s() to see the full %s text" % ((self.__name,)*2)
  57. def __call__(self):
  58. self.__setup()
  59. prompt = 'Hit Return for more, or q (and Return) to quit: '
  60. lineno = 0
  61. while 1:
  62. try:
  63. for i in range(lineno, lineno + self.MAXLINES):
  64. print(self.__lines[i])
  65. except IndexError:
  66. break
  67. else:
  68. lineno += self.MAXLINES
  69. key = None
  70. while key is None:
  71. key = input(prompt)
  72. if key not in ('', 'q'):
  73. key = None
  74. if key == 'q':
  75. break
  76. class _Helper(object):
  77. """Define the builtin 'help'.
  78. This is a wrapper around pydoc.help that provides a helpful message
  79. when 'help' is typed at the Python interactive prompt.
  80. Calling help() at the Python prompt starts an interactive help session.
  81. Calling help(thing) prints help for the python object 'thing'.
  82. """
  83. def __repr__(self):
  84. return "Type help() for interactive help, " \
  85. "or help(object) for help about object."
  86. def __call__(self, *args, **kwds):
  87. import pydoc
  88. return pydoc.help(*args, **kwds)