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