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