<commit_before>#!/usr/bin/env python
# bpython 0.8.0::fancy curses interface to the Python repl::<NAME> 2008
#
# The MIT License
#
# Copyright (c) 2008 <NAME>
#
# 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.
#
# Requires at least Python 2.4, pygments and pyparsing
# Debian/Ubuntu: aptitude install python-pyments python-pyparsing
#

import os
import sys
import curses
import code
import traceback
import re
import time
import urllib
import rlcompleter
import inspect
import signal
import struct
import termios
import fcntl
import string
import socket
import pydoc
import types
from cStringIO import StringIO
from optparse import OptionParser
from urlparse import urljoin
from xmlrpclib import ServerProxy, Error as XMLRPCError
from ConfigParser import ConfigParser, NoSectionError, NoOptionError

# These are used for syntax hilighting.
from pygments import format
from pygments.lexers import PythonLexer
from pygments.token import Token
from bpython.formatter import BPythonFormatter
from itertools import chain

# This for import completion
from bpython import importcompletion

# And these are used for argspec.
from pyparsing import Forward, Suppress, QuotedString, dblQuotedString, \
    Group, OneOrMore, ZeroOrMore, Literal, Optional, Word, \
    alphas, alphanums, printables, ParseException

from bpython import __version__

def log(x):
    f = open('/home/bob/tmp/bpython.out', 'a')
    f.write('%s\n' % (x,))


stdscr = None

class Struct(object):
    """Simple class for instantiating objects we can add arbitrary attributes
    to and use for various arbitrary things."""
    pass


class FakeStdin(object):
    """Provide a fake stdin type for things like raw_input() etc."""

    def __init__(self, interface):
        """Take the curses Repl on init and assume it provides a get_key method
        which, fortunately, it does."""

        self.interface = interface

    def readline(self):
        """I can't think of any reason why anything other than readline would
        be useful in the context of an interactive interpreter so this is the
        only one I've done anything with. The others are just there in case
        someone does something weird to stop it from blowing up."""

        buffer = ''
        while True:
            key = self.interface.get_key()
            sys.stdout.write(key)
# Include the \n in the buffer - raw_input() seems to deal with trailing
# linebreaks and will break if it gets an empty string.
            buffer += key
            if key == '\n':
                break

        return buffer

    def read(self, x):
        pass

    def readlines(self, x):
        pass

OPTS = Struct()
DO_RESIZE = False

# TODO:
#
# C-l doesn't repaint the screen yet.
#
# Tab completion does not work if not at the end of the line.
#
# Numerous optimisations can be made but it seems to do all the lookup stuff
# fast enough on even my crappy server so I'm not too bothered about that
# at the moment.
#
# The popup window that displays the argspecs and completion suggestions
# needs to be an instance of a ListWin class or something so I can wrap
# the addstr stuff to a higher level.
#


def DEBUG(s):
    """This shouldn't ever be called in any release of bpython, so
    beat me up if you find anything calling it."""
    open('/home/bob/tmp/plonker', 'a').write("%s\n" % (str(s), ))


def make_colours():
    """Init all the colours in curses and bang them into a dictionary"""

    for i in range(63):
        if i > 7:
            j = i / 8
        else:
            j = -1
        curses.init_pair(i+1, i % 8, j)

    # blacK, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default:
    c = {
        'k' : 0,
        'r' : 1,
        'g' : 2,
        'y' : 3,
        'b' : 4,
        'm' : 5,
        'c' : 6,
        'w' : 7,
        'd' : -1,
    }
    return c


def next_token_inside_string(s, inside_string):
    """Given a code string s and an initial state inside_string, return
    whether the next token will be inside a string or not."""
    for token, value in PythonLexer().get_tokens(s):
        if token is Token.String and value in ['"""', "'''", '"', "'"]:
            if not inside_string:
                inside_string = value
            elif value == inside_string:
                inside_string = False
    return inside_string


class Interpreter(code.InteractiveInterpreter):

    def __init__(self):
        """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.syntaxerror_callback = None
# Unfortunately code.InteractiveInterpreter is a classic class, so no super()
        code.InteractiveInterpreter.__init__(self)

    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<commit_msg>fixed highlighting of prefixed strings<commit_after>#!/usr/bin/env python
# bpython 0.8.0::fancy curses interface to the Python repl::<NAME> 2008
#
# The MIT License
#
# Copyright (c) 2008 <NAME>
#
# 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.
#
# Requires at least Python 2.4, pygments and pyparsing
# Debian/Ubuntu: aptitude install python-pyments python-pyparsing
#

import os
import sys
import curses
import code
import traceback
import re
import time
import urllib
import rlcompleter
import inspect
import signal
import struct
import termios
import fcntl
import string
import socket
import pydoc
import types
from cStringIO import StringIO
from optparse import OptionParser
from urlparse import urljoin
from xmlrpclib import ServerProxy, Error as XMLRPCError
from ConfigParser import ConfigParser, NoSectionError, NoOptionError

# These are used for syntax hilighting.
from pygments import format
from pygments.lexers import PythonLexer
from pygments.token import Token
from bpython.formatter import BPythonFormatter
from itertools import chain

# This for import completion
from bpython import importcompletion

# And these are used for argspec.
from pyparsing import Forward, Suppress, QuotedString, dblQuotedString, \
    Group, OneOrMore, ZeroOrMore, Literal, Optional, Word, \
    alphas, alphanums, printables, ParseException

from bpython import __version__

def log(x):
    f = open('/home/bob/tmp/bpython.out', 'a')
    f.write('%s\n' % (x,))


stdscr = None

class Struct(object):
    """Simple class for instantiating objects we can add arbitrary attributes
    to and use for various arbitrary things."""
    pass


class FakeStdin(object):
    """Provide a fake stdin type for things like raw_input() etc."""

    def __init__(self, interface):
        """Take the curses Repl on init and assume it provides a get_key method
        which, fortunately, it does."""

        self.interface = interface

    def readline(self):
        """I can't think of any reason why anything other than readline would
        be useful in the context of an interactive interpreter so this is the
        only one I've done anything with. The others are just there in case
        someone does something weird to stop it from blowing up."""

        buffer = ''
        while True:
            key = self.interface.get_key()
            sys.stdout.write(key)
# Include the \n in the buffer - raw_input() seems to deal with trailing
# linebreaks and will break if it gets an empty string.
            buffer += key
            if key == '\n':
                break

        return buffer

    def read(self, x):
        pass

    def readlines(self, x):
        pass

OPTS = Struct()
DO_RESIZE = False

# TODO:
#
# C-l doesn't repaint the screen yet.
#
# Tab completion does not work if not at the end of the line.
#
# Numerous optimisations can be made but it seems to do all the lookup stuff
# fast enough on even my crappy server so I'm not too bothered about that
# at the moment.
#
# The popup window that displays the argspecs and completion suggestions
# needs to be an instance of a ListWin class or something so I can wrap
# the addstr stuff to a higher level.
#


def DEBUG(s):
    """This shouldn't ever be called in any release of bpython, so
    beat me up if you find anything calling it."""
    open('/home/bob/tmp/plonker', 'a').write("%s\n" % (str(s), ))


def make_colours():
    """Init all the colours in curses and bang them into a dictionary"""

    for i in range(63):
        if i > 7:
            j = i / 8
        else:
            j = -1
        curses.init_pair(i+1, i % 8, j)

    # blacK, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default:
    c = {
        'k' : 0,
        'r' : 1,
        'g' : 2,
        'y' : 3,
        'b' : 4,
        'm' : 5,
        'c' : 6,
        'w' : 7,
        'd' : -1,
    }
    return c


def next_token_inside_string(s, inside_string):
    """Given a code string s and an initial state inside_string, return
    whether the next token will be inside a string or not."""
    for token, value in PythonLexer().get_tokens(s):
        if token is Token.String:
            value = value.lstrip('bBrRuU')
            if value in ['"""', "'''", '"', "'"]:
                if not inside_string:
                    inside_string = value
                elif value == inside_string:
                    inside_string = False
    return inside_string


class Interpreter(code.InteractiveInterpreter):

    def __init__(self):
        """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.syntaxerror_callback = None
# Unfortunately code.InteractiveInterpreter is a classic class, so no super()
        code.InteractiveInterpreter.__init__(self)

    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
{'fraction_non_alphanumeric': 0.07306041091689666, 'fraction_numerical': 0.0044464888071143824, 'mean_word_length': 3.000306654400491, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2523178', 'n_tokens_mistral': 3685, 'n_tokens_neox': 3486, 'n_words': 1649}