GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — bpython/bpython/repl.py at master · brianhaines/bpython Expand file tree/ Copy pathrepl.pyMore file actions 1138 lines (974 loc) · 42.7 KB / Copy pathrepl.pyFile metadata and controls1138 lines (974 loc) · 42.7 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 # The MIT License # # Copyright (c) 2009-2011 the bpython authors. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # from __future__ import with_statement import code import codecs import errno import inspect import logging import os import pydoc import shlex import subprocess import sys import tempfile import textwrap import traceback import unicodedata from itertools import takewhile from locale import getpreferredencoding from socket import error as SocketError from string import Template from urllib import quote as urlquote from urlparse import urlparse from xmlrpclib import ServerProxy, Error as XMLRPCError from pygments.token import Token from bpython import inspection from bpython._py3compat import PythonLexer, py3 from bpython.formatter import Parenthesis from bpython.translations import _ import bpython.autocomplete as autocomplete class Interpreter(code.InteractiveInterpreter): def __init__(self, locals=None, encoding=None): """The syntaxerror callback can be set at any time and will be called on a caught syntax error. The purpose for this in bpython is so that the repl can be instantiated after the interpreter (which it necessarily must be with the current factoring) and then an exception callback can be added to the Interpeter instance afterwards - more specifically, this is so that autoindentation does not occur after a traceback.""" self.encoding = encoding or sys.getdefaultencoding() self.syntaxerror_callback = None # Unfortunately code.InteractiveInterpreter is a classic class, so no super() code.InteractiveInterpreter.__init__(self, locals) if not py3: def runsource(self, source, filename='<input>', symbol='single', encode=True): if encode: source = '# coding: %s\n%s' % (self.encoding, source.encode(self.encoding)) return code.InteractiveInterpreter.runsource(self, source, filename, symbol) def showsyntaxerror(self, filename=None): """Override the regular handler, the code's copied and pasted from code.py, as per showtraceback, but with the syntaxerror callback called and the text in a pretty colour.""" if self.syntaxerror_callback is not None: self.syntaxerror_callback() type, value, sys.last_traceback = sys.exc_info() sys.last_type = type sys.last_value = value if filename and type is SyntaxError: # Work hard to stuff the correct filename in the exception try: msg, (dummy_filename, lineno, offset, line) = value.args except: # Not the format we expect; leave it alone pass else: # Stuff in the right filename and right lineno if not py3: lineno -= 1 value = SyntaxError(msg, (filename, lineno, offset, line)) sys.last_value = value list = traceback.format_exception_only(type, value) self.writetb(list) def showtraceback(self): """This needs to override the default traceback thing so it can put it into a pretty colour and maybe other stuff, I don't know""" try: t, v, tb = sys.exc_info() sys.last_type = t sys.last_value = v sys.last_traceback = tb tblist = traceback.extract_tb(tb) del tblist[:1] # Set the right lineno (encoding header adds an extra line) if not py3: for i, (filename, lineno, module, something) in enumerate(tblist): if filename == '<input>': tblist[i] = (filename, lineno - 1, module, something) l = traceback.format_list(tblist) if l: l.insert(0, "Traceback (most recent call last):\n") l[len(l):] = traceback.format_exception_only(t, v) finally: tblist = tb = None self.writetb(l) def writetb(self, lines): """This outputs the traceback and should be overridden for anything fancy.""" for line in lines: self.write(line) class History(object): """Stores readline-style history and current place in it""" def __init__(self, entries=None, duplicates=False): if entries is None: self.entries = [''] else: self.entries = list(entries) self.index = 0 # how many lines back in history is currently selected # where 0 is the saved typed line, 1 the prev entered line self.saved_line = '' # what was on the prompt before using history self.duplicates = duplicates def append(self, line): line = line.rstrip('\n') if line: if not self.duplicates: # remove duplicates try: while True: self.entries.remove(line) except ValueError: pass self.entries.append(line) def first(self): """Move back to the beginning of the history.""" if not self.is_at_end: self.index = len(self.entries) return self.entries[-self.index] def back(self, start=True, search=False, target=None, include_current=False): """Move one step back in the history.""" if target is None: target = self.saved_line if not self.is_at_end: if search: self.index += self.find_partial_match_backward(target, include_current) elif start: self.index += self.find_match_backward(target, include_current) else: self.index += 1 return self.entry @property def entry(self): """The current entry, which may be the saved line""" return self.entries[-self.index] if self.index else self.saved_line @property def entries_by_index(self): return list(reversed(self.entries + [self.saved_line])) def find_match_backward(self, search_term, include_current=False): for idx, val in enumerate(self.entries_by_index[self.index + (0 if include_current else 1):]): if val.startswith(search_term): return idx + (0 if include_current else 1) return 0 def find_partial_match_backward(self, search_term, include_current=False): for idx, val in enumerate(self.entries_by_index[self.index + (0 if include_current else 1):]): if search_term in val: return idx + (0 if include_current else 1) return 0 def forward(self, start=True, search=False, target=None, include_current=False): """Move one step forward in the history.""" if target is None: target = self.saved_line if self.index > 1: if search: self.index -= self.find_partial_match_forward(target, include_current) elif start: self.index -= self.find_match_forward(target, include_current) else: self.index -= 1 return self.entry else: self.index = 0 return self.saved_line def find_match_forward(self, search_term, include_current=False): #TODO these are no longer efficient, because we realize the whole list. Does this matter? for idx, val in enumerate(reversed(self.entries_by_index[:max(0, self.index - (1 if include_current else 0))])): if val.startswith(search_term): return idx + (0 if include_current else 1) return self.index def find_partial_match_forward(self, search_term, include_current=False): for idx, val in enumerate(reversed(self.entries_by_index[:max(0, self.index - (1 if include_current else 0))])): if search_term in val: return idx + (0 if include_current else 1) return self.index def last(self): """Move forward to the end of the history.""" if not self.is_at_start: self.index = 0 return self.entries[0] @property def is_at_end(self): return self.index >= len(self.entries) or self.index == -1 @property def is_at_start(self): return self.index == 0 def enter(self, line): if self.index == 0: self.saved_line = line @classmethod def from_filename(cls, filename): history = cls() history.load(filename) return history def load(self, filename, encoding): with codecs.open(filename, 'r', encoding, 'ignore') as hfile: for line in hfile: self.append(line) def reset(self): self.index = 0 self.saved_line = '' def save(self, filename, encoding, lines=0): with codecs.open(filename, 'w', encoding, 'ignore') as hfile: for line in self.entries[-lines:]: hfile.write(line) hfile.write('\n') class MatchesIterator(object): """Stores a list of matches and which one is currently selected if any. Also responsible for doing the actual replacement of the original line with the selected match. A MatchesIterator can be `clear`ed to reset match iteration, and `update`ed to set what matches will be iterated over.""" def __init__(self): self.current_word = '' # word being replaced in the original line of text self.matches = None # possible replacements for current_word self.index = -1 # which word is currently replacing the current word self.orig_cursor_offset = None # cursor position in the original line self.orig_line = None # original line (before match replacements) self.completer = None # class describing the current type of completion def __nonzero__(self): """MatchesIterator is False when word hasn't been replaced yet""" return self.index != -1 def __iter__(self): return self def current(self): if self.index == -1: raise ValueError('No current match.') return self.matches[self.index] def next(self): self.index = (self.index + 1) % len(self.matches) return self.matches[self.index] def previous(self): if self.index <= 0: self.index = len(self.matches) self.index -= 1 return self.matches[self.index] def cur_line(self): """Returns a cursor offset and line with the current substitution made""" return self.substitute(self.current()) def substitute(self, match): """Returns a cursor offset and line with match substituted in""" start, end, word = self.completer.locate(self.orig_cursor_offset, self.orig_line) result = start + len(match), self.orig_line[:start] + match + self.orig_line[end:] return result def is_cseq(self): return bool(os.path.commonprefix(self.matches)[len(self.current_word):]) def substitute_cseq(self): """Returns a new line by substituting a common sequence in, and update matches""" cseq = os.path.commonprefix(self.matches) new_cursor_offset, new_line = self.substitute(cseq) if len(self.matches) == 1: self.clear() else: self.update(new_cursor_offset, new_line, self.matches, self.completer) if len(self.matches) == 1: self.clear() return new_cursor_offset, new_line def update(self, cursor_offset, current_line, matches, completer): """Called to reset the match index and update the word being replaced Should only be called if there's a target to update - otherwise, call clear""" self.orig_cursor_offset = cursor_offset self.orig_line = current_line assert matches is not None self.matches = matches self.completer = completer #assert self.completer.locate(self.orig_cursor_offset, self.orig_line) is not None, (self.completer.locate, self.orig_cursor_offset, self.orig_line) self.index = -1 self.start, self.end, self.current_word = self.completer.locate(self.orig_cursor_offset, self.orig_line) def clear(self): self.matches = [] self.cursor_offset = -1 self.current_line = '' self.current_word = '' self.start = None self.end = None self.index = -1 class Interaction(object): def __init__(self, config, statusbar=None): self.config = config if statusbar: self.statusbar = statusbar def confirm(self, s): raise NotImplementedError def notify(self, s, n=10): raise NotImplementedError def file_prompt(self, s): raise NotImplementedError class Repl(object): """Implements the necessary guff for a Python-repl-alike interface The execution of the code entered and all that stuff was taken from the Python code module, I had to copy it instead of inheriting it, I can't remember why. The rest of the stuff is basically what makes it fancy. It reads what you type, passes it to a lexer and highlighter which returns a formatted string. This then gets passed to echo() which parses that string and prints to the curses screen in appropriate colours and/or bold attribute. The Repl class also keeps two stacks of lines that the user has typed in: One to be used for the undo feature. I am not happy with the way this works. The only way I have been able to think of is to keep the code that's been typed in in memory and re-evaluate it in its entirety for each "undo" operation. Obviously this means some operations could be extremely slow. I'm not even by any means certain that this truly represents a genuine "undo" implementation, but it does seem to be generally pretty effective. If anyone has any suggestions for how this could be improved, I'd be happy to hear them and implement it/accept a patch. I researched a bit into the idea of keeping the entire Python state in memory, but this really seems very difficult (I believe it may actually be impossible to work) and has its own problems too. The other stack is for keeping a history for pressing the up/down keys to go back and forth between lines. XXX Subclasses should implement echo, current_line, cw """ def __init__(self, interp, config): """Initialise the repl. interp is a Python code.InteractiveInterpreter instance config is a populated bpython.config.Struct. """ self.config = config self.cut_buffer = '' self.buffer = [] self.interp = interp self.interp.syntaxerror_callback = self.clear_current_line self.match = False self.rl_history = History(duplicates=config.hist_duplicates) self.s_hist = [] self.history = [] self.evaluating = False self.matches_iter = MatchesIterator() self.argspec = None self.current_func = None self.highlighted_paren = None self._C = {} self.prev_block_finished = 0 self.interact = Interaction(self.config) # previous pastebin content to prevent duplicate pastes, filled on call # to repl.pastebin self.prev_pastebin_content = '' self.prev_pastebin_url = '' # Necessary to fix mercurial.ui.ui expecting sys.stderr to have this # attribute self.closed = False pythonhist = os.path.expanduser(self.config.hist_file) if os.path.exists(pythonhist): self.rl_history.load(pythonhist, getpreferredencoding() or "ascii") @property def ps1(self): try: return str(sys.ps1) except AttributeError: return '>>> ' @property def ps2(self): try: return str(sys.ps2) except AttributeError: return '... ' def startup(self): """ Execute PYTHONSTARTUP file if it exits. Call this after front end-specific initialisation. """ filename = os.environ.get('PYTHONSTARTUP') if filename and os.path.isfile(filename): with open(filename, 'r') as f: if py3: self.interp.runsource(f.read(), filename, 'exec') else: self.interp.runsource(f.read(), filename, 'exec', encode=False) def current_string(self, concatenate=False): """If the line ends in a string get it, otherwise return ''""" tokens = self.tokenize(self.current_line) string_tokens = list(takewhile(token_is_any_of([Token.String, Token.Text]), reversed(tokens))) if not string_tokens: return '' opening = string_tokens.pop()[1] string = list() for (token, value) in reversed(string_tokens): if token is Token.Text: continue elif opening is None: opening = value elif token is Token.String.Doc: string.append(value[3:-3]) opening = None elif value == opening: opening = None if not concatenate: string = list() else: string.append(value) if opening is None: return '' return ''.join(string) def get_object(self, name): attributes = name.split('.') obj = eval(attributes.pop(0), self.interp.locals) while attributes: with inspection.AttrCleaner(obj): obj = getattr(obj, attributes.pop(0)) return obj def get_args(self): """Check if an unclosed parenthesis exists, then attempt to get the argspec() for it. On success, update self.argspec and return True, otherwise set self.argspec to None and return False""" self.current_func = None if not self.config.arg_spec: return False # Get the name of the current function and where we are in # the arguments stack = [['', 0, '']] try: for (token, value) in PythonLexer().get_tokens( self.current_line): if token is Token.Punctuation: if value in '([{': stack.append(['', 0, value]) elif value in ')]}': stack.pop() elif value == ',': try: stack[-1][1] += 1 except TypeError: stack[-1][1] = '' stack[-1][0] = '' elif value == ':' and stack[-1][2] == 'lambda': stack.pop() else: stack[-1][0] = '' elif (token is Token.Name or token in Token.Name.subtypes or token is Token.Operator and value == '.'): stack[-1][0] += value elif token is Token.Operator and value == '=': stack[-1][1] = stack[-1][0] stack[-1][0] = '' elif token is Token.Keyword and value == 'lambda': stack.append(['', 0, value]) else: stack[-1][0] = '' while stack[-1][2] in '[{': stack.pop() _, arg_number, _ = stack.pop() func, _, _ = stack.pop() except IndexError: return False if not func: return False try: f = self.get_object(func) except Exception: # another case of needing to catch every kind of error # since user code is run in the case of descriptors # XXX: Make sure you raise here if you're debugging the completion # stuff ! return False if inspect.isclass(f): try: if f.__init__ is not object.__init__: f = f.__init__ except AttributeError: return None self.current_func = f self.argspec = inspection.getargspec(func, f) if self.argspec: self.argspec.append(arg_number) return True return False def get_source_of_current_name(self): """Return the source code of the object which is bound to the current name in the current input line. Return `None` if the source cannot be found.""" try: obj = self.current_func if obj is None: line = self.current_line if inspection.is_eval_safe_name(line): obj = self.get_object(line) if obj is None: return None source = inspect.getsource(obj) except (AttributeError, IOError, NameError, TypeError): return None else: return source def set_docstring(self): self.docstring = None if not self.get_args(): self.argspec = None elif self.current_func is not None: try: self.docstring = pydoc.getdoc(self.current_func) except IndexError: self.docstring = None else: # pydoc.getdoc() returns an empty string if no # docstring was found if not self.docstring: self.docstring = None def complete(self, tab=False): """Construct a full list of possible completions and construct and display them in a window. Also check if there's an available argspec (via the inspect module) and bang that on top of the completions too. The return value is whether the list_win is visible or not. If no matches are found, just return whether there's an argspec to show If any matches are found, save them and select the first one. If tab is True exactly one match found, make the replacement and return the result of running complete() again on the new line. """ self.set_docstring() matches, completer = autocomplete.get_completer( self.cursor_offset, self.current_line, self.interp.locals, self.argspec, '\n'.join(self.buffer + [self.current_line]), self.config.autocomplete_mode if hasattr(self.config, 'autocomplete_mode') else autocomplete.SIMPLE, self.config.complete_magic_methods) #TODO implement completer.shown_before_tab == False (filenames shouldn't fill screen) if (matches is None # no completion is relevant or len(matches) == 0): # a target for completion was found # but no matches were found self.matches_iter.clear() return bool(self.argspec) self.matches_iter.update(self.cursor_offset, self.current_line, matches, completer) if len(matches) == 1: self.matches_iter.next() if tab: # if this complete is being run for a tab key press, tab() to do the swap self.cursor_offset, self.current_line = self.matches_iter.substitute_cseq() return Repl.complete(self) elif self.matches_iter.current_word == matches[0]: self.matches_iter.clear() return False return completer.shown_before_tab else: assert len(matches) > 1 return tab or completer.shown_before_tab def format_docstring(self, docstring, width, height): """Take a string and try to format it into a sane list of strings to be put into the suggestion box.""" lines = docstring.split('\n') out = [] i = 0 for line in lines: i += 1 if not line.strip(): out.append('\n') for block in textwrap.wrap(line, width): out.append(' ' + block + '\n') if i >= height: return out i += 1 # Drop the last newline out[-1] = out[-1].rstrip() return out def next_indentation(self): """Return the indentation of the next line based on the current input buffer.""" if self.buffer: indentation = next_indentation(self.buffer[-1], self.config.tab_length) if indentation and self.config.dedent_after > 0: line_is_empty = lambda line: not line.strip() empty_lines = takewhile(line_is_empty, reversed(self.buffer)) if sum(1 for _ in empty_lines) >= self.config.dedent_after: indentation -= 1 else: indentation = 0 return indentation def formatforfile(self, s): """Format the stdout buffer to something suitable for writing to disk, i.e. without >>> and ... at input lines and with "# OUT: " prepended to output lines.""" def process(): for line in s.split('\n'): if line.startswith(self.ps1): yield line[len(self.ps1):] elif line.startswith(self.ps2): yield line[len(self.ps2):] elif line.rstrip(): yield "# OUT: %s" % (line,) return "\n".join(process()) def write2file(self): """Prompt for a filename and write the current contents of the stdout buffer to disk.""" try: fn = self.interact.file_prompt('Save to file (Esc to cancel): ') if not fn: self.interact.notify("Save cancelled.") return except ValueError: self.interact.notify("Save cancelled.") return if fn.startswith('~'): fn = os.path.expanduser(fn) if not fn.endswith('.py') and self.config.save_append_py: fn = fn + '.py' mode = 'w' if os.path.exists(fn): mode = self.interact.file_prompt('%s already exists. Do you want ' 'to (c)ancel, (o)verwrite or ' '(a)ppend? ' % (fn, )) if mode in ('o', 'overwrite'): mode = 'w' elif mode in ('a', 'append'): mode = 'a' else: self.interact.notify('Save cancelled.') return s = self.formatforfile(self.getstdout()) try: f = open(fn, mode) f.write(s) f.close() except IOError: self.interact.notify("Disk write error for file '%s'." % (fn, )) else: self.interact.notify('Saved to %s.' % (fn, )) def pastebin(self, s=None): """Upload to a pastebin and display the URL in the status bar.""" if s is None: s = self.getstdout() if (self.config.pastebin_confirm and not self.interact.confirm(_("Pastebin buffer? (y/N) "))): self.interact.notify(_("Pastebin aborted")) return return self.do_pastebin(s) def do_pastebin(self, s): """Actually perform the upload.""" if s == self.prev_pastebin_content: self.interact.notify(_('Duplicate pastebin. Previous URL: %s') % (self.prev_pastebin_url, )) return self.prev_pastebin_url if self.config.pastebin_helper: return self.do_pastebin_helper(s) else: return self.do_pastebin_xmlrpc(s) def do_pastebin_xmlrpc(self, s): """Upload to pastebin via XML-RPC.""" try: pasteservice = ServerProxy(self.config.pastebin_url) except IOError, e: self.interact.notify(_("Pastebin error for URL '%s': %s") % (self.config.pastebin_url, str(e))) return self.interact.notify(_('Posting data to pastebin...')) try: paste_id = pasteservice.pastes.newPaste('pycon', s, '', '', '', self.config.pastebin_private) except (SocketError, XMLRPCError), e: self.interact.notify(_('Upload failed: %s') % (str(e), ) ) return self.prev_pastebin_content = s paste_url_template = Template(self.config.pastebin_show_url) paste_id = urlquote(paste_id) paste_url = paste_url_template.safe_substitute(paste_id=paste_id) self.prev_pastebin_url = paste_url self.interact.notify(_('Pastebin URL: %s') % (paste_url, ), 10) return paste_url def do_pastebin_helper(self, s): """Call out to helper program for pastebin upload.""" self.interact.notify(_('Posting data to pastebin...')) try: helper = subprocess.Popen('', executable=self.config.pastebin_helper, stdin=subprocess.PIPE, stdout=subprocess.PIPE) helper.stdin.write(s.encode(getpreferredencoding())) output = helper.communicate()[0].decode(getpreferredencoding()) paste_url = output.split()[0] except OSError, e: if e.errno == errno.ENOENT: self.interact.notify(_('Upload failed: ' 'Helper program not found.')) else: self.interact.notify(_('Upload failed: ' 'Helper program could not be run.')) return if helper.returncode != 0: self.interact.notify(_('Upload failed: ' 'Helper program returned non-zero exit ' 'status %s.' % (helper.returncode, ))) return if not paste_url: self.interact.notify(_('Upload failed: ' 'No output from helper program.')) return else: parsed_url = urlparse(paste_url) if (not parsed_url.scheme or any(unicodedata.category(c) == 'Cc' for c in paste_url)): self.interact.notify(_("Upload failed: " "Failed to recognize the helper " "program's output as an URL.")) return self.prev_pastebin_content = s self.interact.notify(_('Pastebin URL: %s') % (paste_url, ), 10) return paste_url def push(self, s, insert_into_history=True): """Push a line of code onto the buffer so it can process it all at once when a code block ends""" s = s.rstrip('\n') self.buffer.append(s) if insert_into_history: self.insert_into_history(s) more = self.interp.runsource('\n'.join(self.buffer)) if not more: self.buffer = [] return more def insert_into_history(self, s): if self.config.hist_length: histfilename = os.path.expanduser(self.config.hist_file) oldhistory = self.rl_history.entries self.rl_history.entries = [] if os.path.exists(histfilename): self.rl_history.load(histfilename, getpreferredencoding()) self.rl_history.append(s) try: self.rl_history.save(histfilename, getpreferredencoding(), self.config.hist_length) except EnvironmentError, err: self.interact.notify("Error occured while writing to file %s (%s) " % (histfilename, err.strerror)) self.rl_history.entries = oldhistory self.rl_history.append(s) else: self.rl_history.append(s) def undo(self, n=1): """Go back in the undo history n steps and call reeavluate() Note that in the program this is called "Rewind" because I want it to be clear that this is by no means a true undo implementation, it is merely a convenience bonus.""" if not self.history: return None if len(self.history) < n: n = len(self.history) entries = list(self.rl_history.entries) self.history = self.history[:-n] self.reevaluate() self.rl_history.entries = entries def flush(self): """Olivier Grisel brought it to my attention that the logging module tries to call this method, since it makes assumptions about stdout that may not necessarily be true. The docs for sys.stdout say: "stdout and stderr needn't be built-in file objects: any object is acceptable as long as it has a write() method that takes a string argument." So I consider this to be a bug in logging, and this is a hack to fix it, unfortunately. I'm sure it's not the only module to do it.""" def close(self): """See the flush() method docstring.""" def tokenize(self, s, newline=False): """Tokenizes a line of code, returning pygments tokens with side effects/impurities: - reads self.cpos to see what parens should be highlighted - reads self.buffer to see what came before the passed in line - sets self.highlighted_paren to (buffer_lineno, tokens_for_that_line) for buffer line that should replace that line to unhighlight it - calls reprint_line with a buffer's line's tokens and the buffer lineno that has changed iff that line is the not the current line """ source = '\n'.join(self.buffer + [s]) cursor = len(source) - self.cpos if self.cpos: cursor += 1 stack = list() all_tokens = list(PythonLexer().get_tokens(source)) # Unfortunately, Pygments adds a trailing newline and strings with # no size, so strip them while not all_tokens[-1][1]: all_tokens.pop() all_tokens[-1] = (all_tokens[-1][0], all_tokens[-1][1].rstrip('\n')) line = pos = 0 parens = dict(zip('{([', '})]')) line_tokens = list() saved_tokens = list() search_for_paren = True for (token, value) in split_lines(all_tokens): pos += len(value) if token is Token.Text and value == '\n': line += 1 # Remove trailing newline line_tokens = list() saved_tokens = list() continue line_tokens.append((token, value)) saved_tokens.append((token, value)) if not search_for_paren: continue under_cursor = (pos == cursor) if token is Token.Punctuation: if value in parens: if under_cursor: line_tokens[-1] = (Parenthesis.UnderCursor, value) # Push marker on the stack stack.append((Parenthesis, value)) else: stack.append((line, len(line_tokens) - 1, line_tokens, value)) elif value in parens.itervalues(): saved_stack = list(stack) try: while True: opening = stack.pop() if parens[opening[-1]] == value: break except IndexError: # SyntaxError.. more closed parentheses than # opened or a wrong closing paren opening = None if not saved_stack: search_for_paren = False else: stack = saved_stack if opening and opening[0] is Parenthesis: # Marker found line_tokens[-1] = (Parenthesis, value) search_for_paren = False elif opening and under_cursor and not newline: if self.cpos: line_tokens[-1] = (Parenthesis.UnderCursor, value) else: # The cursor is at the end of line and next to # the paren, so it doesn't reverse the paren. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |