Beispiel #1
0
    def _source_info_for ( self, file_name, cur_line, frame ):
        """ Returns the color code and optional tag reference for the source
            code for the file name and stack frame specified by *file_name* and
            *frame*, and where execution is currently on *cur_line*.
        """
        start, lines = self.shell.source_for_frame( frame )
        if start is None:
            return ( color_tag_for( 'B' ), '' )

        context = self.shell.context
        if context < 50:
            begin  = max( 0, cur_line - start - context ) + 1
            end    = min( len( lines ), cur_line - start + context + 1 ) + 1
            lines  = source_context( lines, begin, end )
            start += (begin - 1)

        lines = python_colorize(
            trim_margin( lines ), frame.f_locals, self.tags
        )
        format = '%%0%dd' % len( str( start + len( lines ) - 1 ) )
        source = '\n'.join(
            [ '%s \x008%s|\x00E %s' % ( LinePrefix[ (start + i) == cur_line ],
                    format % (start + i), line )
              for i, line in enumerate( lines ) ]
        )
        tag = len( self.tags )
        self.tags.append(
            ContentTag( content = '%s\n%s%s' % ( Separator, source, Separator) )
        )

        return ( color_tag_for( 'B', tag ), tag_ref_for( tag ) )
Beispiel #2
0
    def as_object ( self, item, indent, name = '<object>' ):
        """ Returns the 'object' *item* pretty-printed as a string.
        """
        if indent != '':
            cc = color_tag_for( 'B', self.tags )
            self.tags.append( ValueTag( label = name, value = item ) )

            return ('%s%s%s\x002( \x00A@0x%X\x002 )' %
                    ( indent, cc, item.__class__.__name__, id( item ) ))

        da    = self.display_attributes
        names = da.names
        if (len( names ) == 1) and (names[0] is None):
            da.names = da.all_names = names = sorted(
                [ name for name in item.__dict__.iterkeys() ]
            )

        result      = []
        next_indent = indent + IndentLevel
        max_len     = -(reduce( lambda x, y: max( x, len( y ) ), names, 0 ) + 1)
        for name in names:
            try:
                value = getattr( item, name )
            except:
                # If we can't retrieve the value, then just ignore it:
                continue

            result.append( "%s\x00C%*s\x002= %s" % (
                next_indent, max_len, name,
                self.as_str( value, next_indent, name ).strip()
            ) )

        return ('%s\x00B%s\x002( \x00A@0x%X\x002,\n%s\n%s)' %
                ( indent, item.__class__.__name__, id( item ),
                  ',\n'.join( result ), indent ))
Beispiel #3
0
    def _item_contents_default ( self ):
        files       = []
        directories = []
        tags        = self.tags
        path        = self.item
        cc2         = color_tag_for( '5' )
        for name in listdir( path ):
            file_name = join( path, name )
            if isdir( file_name ):
                directories.append(
                    file_info_for( file_name, color_tag_for( 'B', tags ), cc2 )
                )
            else:
                files.append(
                    file_info_for( file_name, color_tag_for( 'C', tags ), cc2 )
                )

            tags.append( FileTag( file = file_name ) )

        return '\n'.join( directories + files )
Beispiel #4
0
    def _file_info_for ( self, file_name, line ):
        """ Returns the color code and adjusted file name to use for a specified
            file name.
        """
        key = ( file_name, line )
        tag = self.file_names.get( key )
        if tag is None:
            self.file_names[ key ] = tag = len( self.tags )
            self.tags.append( PythonFileTag(
                file    = file_name,
                tooltip = file_name,
                line    = line
            ) )

        return ( color_tag_for( 'C', tag ), basename( file_name ) )
Beispiel #5
0
    def help_for_items ( self ):
        """ Returns help information about shell item keyboard and mouse
            shortcuts.
        """
        key_info    = {}
        method_info = set()
        self._item_keys_for( ShellItem, key_info, method_info )
        keys = key_info.keys()
        keys.sort( lambda l, r: cmp_key_name( l, r ) )
        max_len       = reduce( lambda x, y: max( x, len( y ) ), keys, 0 )
        format_tagged = '%%%ds = %%s%%s' % (-(max_len + 8))
        format_simple = '%%%ds = %%s%%s' % (-(max_len + 4))
        output        = []
        tags          = []
        for key in keys:
            for kind, info in key_info[ key ]:
                if kind == 'Shell':
                    kind = ''
                else:
                    kind = '\x00C[%s]\x000 ' % kind

                info = '\n'.join( [
                    line.strip() for line in info.split( '\n' )
                ] )
                items = info.split( '\n\n', 1 )
                info  = ('\n' + (' ' * (max_len + 3))).join(
                        items[0].split( '\n' ) )
                cc     = '\x00B'
                format = format_simple
                if len( items ) > 1:
                    cc     = color_tag_for( 'B', tags )
                    format = format_tagged
                    tags.append( TextTag( text = items[1] ) )

                output.append(
                    format % ( '%s%s\x000' % ( cc, key ), kind, info )
                )

        return self.shell.history_item_for(
            OutputItem, (replace_markers( ShellItems ) % ('\n'.join( output ))),
            tags = tags, lod = 2
        )
Beispiel #6
0
    def help_for_commands ( self ):
        """ Returns help information about the available shell commands.
        """
        summaries = []
        codes     = []
        tags      = []
        sccf      = self.shell.shell_command_class_for
        registry  = self.shell.registry
        commands  = [ name for name, value in registry.iteritems() ]
        commands.sort()

        for command in commands:
            summary       = ''
            command_class = sccf( command )
            if command_class is not None:
                summary = command_class( command = command ).summary
                if summary != '':
                    summary = ' = ' + summary

            summaries.append( summary )

            cc = 'BC'[ registry[ command ][1] is not None ]
            codes.append( color_tag_for( cc, tags ) )
            tags.append( CommandTag( command = '/%s?' % command ) )

        # Generate the command related help text:
        max_len = -(reduce( lambda x, y: max( x, len( y ) ), commands, 0 ) + 2)
        items   = [ '\n'.join( [
            '%s/%*s%s' % ( code, max_len, command + '\x000', summary )
            for command, summary, code in zip( commands, summaries, codes )
        ] ) ]

        # Generate the final help text:
        help = (ShellCommands % tuple( items )).replace( '[[', '\x00B'
                                              ).replace( ']]', '\x000'
                                              ).replace( '<<', '\x00C'
                                              ).replace( '>>', '\x000' )

        return self.shell.history_item_for(
            OutputItem, help, tags = tags, lod = 2
        )
Beispiel #7
0
    def execute ( self ):
        """ Displays help information about using the shell commands.
        """
        method = getattr( self, 'help_for_' + self.options, None )
        if method is not None:
            return method()

        help = OptionsMap.get( self.options )
        if help is not None:
            return help

        # Create the tag links for the various help sections:
        items = []
        tags  = []
        for key in ( 'commands', 'variables', 'keys', 'items', 'debug' ):
            items.append( color_tag_for( 'B', tags ) )
            tags.append( CommandTag( command = '/ ' + key ) )

        # Return the final help text:
        return self.shell.history_item_for(
            OutputItem, (ShellHelp % tuple( items )).replace( '>>', '\x000' ),
            tags = tags, lod = 2
        )
Beispiel #8
0
def python_colorize ( code, locals = {}, tags = None ):
    """ Returns a colorized version of the specified Python *code*.
    """
    from facets.ui.vip_shell.tags.value_tag import ValueTag

    last_color  = ''
    last_column = last_line = 0
    prev_line   = ''
    was_dot     = False
    context     = None
    cache       = {}
    tokens      = []
    tokenizer   = generate_tokens( StringIO( as_string( code ) ).readline )
    try:
        for type, token, first, last, line in tokenizer:
            if type == 0:
                break

            if type in ( 5, 6 ):
                continue

            if type == 1:
                if token in PythonKeywords:
                    color = 'H'
                elif token in PythonSpecial:
                    color = 'J'
                else:
                    color = 'E'
                    if was_dot:
                        if context is not None:
                            try:
                                context = getattr( context, token )
                                label  += ('.' + token)
                            except:
                                context = label = None
                        else:
                            label = None
                    else:
                        context = label = None
                        if token in locals:
                            context = locals[ token ]
                            label   = token

                    if label is not None:
                        tag = cache.get( label )
                        if ((tag is None) and
                            (not isinstance( context, BoringTypes ))):
                            cache[ label ] = tag = len( tags )
                            tags.append(
                                ValueTag( label = label, value = context )
                            )

                        if tag is not None:
                            color = last_color = 'E'
                            token = ('%s%s%s' % (
                                     color_tag_for( 'C', tag ), token,
                                     color_tag_for( color ) ))
                        else:
                            context = None
            else:
                color = TokenColor.get( type, 'E' )

            first_line, first_column = first
            if last_line != first_line:
                if prev_line[-2:] == '\\\n':
                    tokens.append( ' \\\n' )

                last_column = 0

            prefix = ' ' * (first_column - last_column)

            if color != last_color:
                token = color_tag_for( color ) + token

            if type != 4:
                if color != 'E':
                    token = token.replace( '\n', '\n' + color_tag_for( color ) )

                token = prefix + token

            tokens.append( token )
            prev_line  = line
            last_color = color
            was_dot    = (token == '.')
            last_line, last_column = last
    except:
        pass

    return as_like( ''.join( tokens ), code )
Beispiel #9
0
from facets.ui.graphics_text \
    import color_tag_for, tag_ref_for

from facets.ui.vip_shell.tags.api \
    import PythonFileTag, ContentTag

from facets.ui.vip_shell.helper \
    import max_len, trim_margin, python_colorize, source_context

#-------------------------------------------------------------------------------
#  Constants:
#-------------------------------------------------------------------------------

# Separator line:
Separator = '\n' + color_tag_for( 'C' )

# Possible line prefixes:
LinePrefix = ( '   ', '\x00C-->' )

#-------------------------------------------------------------------------------
#  'ExceptionItem' class:
#-------------------------------------------------------------------------------

class ExceptionItem ( LabeledItem ):
    """ An exception raised by executing a command.
    """

    #-- Facet Definitions ------------------------------------------------------

    type       = 'exception'