<commit_before>#!/usr/bin/python
#
# Generic TreeWidget/TreeWalker class 
#    Copyright (c) 2010  <NAME>
# Derived from Urwid example lazy directory browser / tree view:
#    Copyright (C) 2004-2009  <NAME>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

"""
Urwid tree view

Features:
- custom selectable widgets for trees
- custom list walker for displaying widgets in a tree fashion
"""


import urwid

import os


class TreeWidgetError(RuntimeError):
    pass


class TreeWidget(urwid.WidgetWrap):
    """A widget representing something in the file tree."""
    def __init__(self, node):
        self._node = node
        self._innerwidget = None
        self.selected = False

        widget = self.get_indented_widget()

        w = urwid.AttrWrap(widget, None)
        self.__super.__init__(w)
        # Compatibility fix for 0.9.9+
        if not hasattr(self, 'get_w'):
            self.get_w = self._retro_get_w
        self.update_w()
    
    def _retro_get_w(self):
        """
        Implementation of get_w() if the base urwid install doesn't support it.
        """
        return self._w

    def get_indented_widget(self):
        leftmargin = urwid.Text("")
        widgetlist = [self.get_inner_widget()]
        indent_cols = self.get_indent_cols()
        if indent_cols > 0:
            widgetlist.insert(0, ('fixed', indent_cols, leftmargin))
        return urwid.Columns(widgetlist)

    def get_indent_cols(self):
        return 3 * self.get_node().get_depth()

    def get_inner_widget(self):
        if self._innerwidget is None:
            self._innerwidget = self.load_inner_widget()
        return self._innerwidget

    def load_inner_widget(self):
        return urwid.Text(self.get_display_text())
        
    def get_node(self):
        return self._node

    def get_display_text(self):
        return (self.get_node().get_key() + ": " + 
                str(self.get_node().get_value()))

    def selectable(self):
        return True
    
    def is_selected(self):
        return self.selected

    def set_selected(self, value=True):
        self.selected = value

    def keypress(self, size, key):
        """allow subclasses to intercept keystrokes"""
        w = self.get_w()
        try:
            key = w.keypress(size, key)
        except AttributeError:
            # no biggie...we'll just handle the keypress here
            pass
        key = self.unhandled_keys(size, key)
        return key

    def unhandled_keys(self, size, key):
        """
        Override this method to intercept keystrokes in subclasses.
        Default behavior: Toggle selected on space, ignore other keys.
        """
        if key == " ":
            self.selected = not self.selected
            self.update_w()
        else:
            return key

    def update_w(self):
        """Update the attributes of self.widget based on self.selected.
        """
        if self.selected:
            self.get_w().attr = 'selected'
            self.get_w().focus_attr = 'selected focus'
        else:
            self.get_w().attr = 'body'
            self.get_w().focus_attr = 'focus'

    def next_inorder(self):
        """Return the next TreeWidget depth first from this one."""
        # first check if there's a child widget
        firstchild = self.first_child()
        if firstchild is not None:
            return firstchild

        # now we need to hunt for the next sibling
        thisnode = self.get_node()
        nextnode = thisnode.next_sibling()
        depth = thisnode.get_depth()
        while nextnode is None and depth > 0:
            # keep going up the tree until we find an ancestor next sibling
            thisnode = thisnode.get_parent()
            nextnode = thisnode.next_sibling()
            depth -= 1
            assert depth == thisnode.get_depth()
        if nextnode is None:
            # we're at the end of the tree<commit_msg>treetools: remove <0.9.9 compatibilty cruft, name indent_cols constant<commit_after>#!/usr/bin/python
#
# Generic TreeWidget/TreeWalker class 
#    Copyright (c) 2010  <NAME>
# Derived from Urwid example lazy directory browser / tree view:
#    Copyright (C) 2004-2009  <NAME>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

"""
Urwid tree view

Features:
- custom selectable widgets for trees
- custom list walker for displaying widgets in a tree fashion
"""


import urwid

import os


class TreeWidgetError(RuntimeError):
    pass


class TreeWidget(urwid.WidgetWrap):
    """A widget representing something in the file tree."""
    indent_cols = 3

    def __init__(self, node):
        self._node = node
        self._innerwidget = None
        self.selected = False

        widget = self.get_indented_widget()

        w = urwid.AttrWrap(widget, None)
        self.__super.__init__(w)
        self.update_w()
    
    def get_indented_widget(self):
        leftmargin = urwid.Text("")
        widgetlist = [self.get_inner_widget()]
        indent_cols = self.get_indent_cols()
        if indent_cols > 0:
            widgetlist.insert(0, ('fixed', indent_cols, leftmargin))
        return urwid.Columns(widgetlist)

    def get_indent_cols(self):
        return self.indent_cols * self.get_node().get_depth()

    def get_inner_widget(self):
        if self._innerwidget is None:
            self._innerwidget = self.load_inner_widget()
        return self._innerwidget

    def load_inner_widget(self):
        return urwid.Text(self.get_display_text())
        
    def get_node(self):
        return self._node

    def get_display_text(self):
        return (self.get_node().get_key() + ": " + 
                str(self.get_node().get_value()))

    def selectable(self):
        return True
    
    def is_selected(self):
        return self.selected

    def set_selected(self, value=True):
        self.selected = value

    def keypress(self, size, key):
        """allow subclasses to intercept keystrokes"""
        try:
            key = self._w.keypress(size, key)
        except AttributeError:
            # no biggie...we'll just handle the keypress here
            pass
        if key:
            key = self.unhandled_keys(size, key)
        return key

    def unhandled_keys(self, size, key):
        """
        Override this method to intercept keystrokes in subclasses.
        Default behavior: Toggle selected on space, ignore other keys.
        """
        if key == " ":
            self.selected = not self.selected
            self.update_w()
        else:
            return key

    def update_w(self):
        """Update the attributes of self.widget based on self.selected.
        """
        if self.selected:
            self._w.attr = 'selected'
            self._w.focus_attr = 'selected focus'
        else:
            self._w.attr = 'body'
            self._w.focus_attr = 'focus'

    def next_inorder(self):
        """Return the next TreeWidget depth first from this one."""
        # first check if there's a child widget
        firstchild = self.first_child()
        if firstchild is not None:
            return firstchild

        # now we need to hunt for the next sibling
        thisnode = self.get_node()
        nextnode = thisnode.next_sibling()
        depth = thisnode.get_depth()
        while nextnode is None and depth > 0:
            # keep going up the tree until we find an ancestor next sibling
            thisnode = thisnode.get_parent()
            nextnode = thisnode.next_sibling()
            depth -= 1
            assert depth == thisnode.get_depth()
        if nextnode is None:
            # we're at the end of the tree
{'fraction_non_alphanumeric': 0.08527896525265811, 'fraction_numerical': 0.007892140743176587, 'mean_word_length': 2.2655690765926986, 'pattern_counts': {'":': 4, '<': 8, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3580466', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2514, 'n_words': 861}