<commit_before>import wx
import os.path
import os
import glob
import sys
import subprocess
import pdb

# TODO:  fix tab order through controls
# TODO:  fix alignment in text field, ideally would be right aligned, but may not be able to do that on OSX
# TODO: limit default allowed_extensions to .fits/.fits.gz  (rather than .fits/.gz)
# TODO: fix that is currently hardwired to .fits/.gz file endings and allow calling code to specify other options

# TODO: implement a None option? or that None is italixiszed in text box and dropdown menu as a special thing?

class Error(Exception):
    pass


class FilePicker(wx.Panel):
    def __init__(self, parent,
                 is_files_not_dirs=True,  # True to look for files, False to look for dirs
                 max_history_items=20,
                 history=None,  # pre-load items into history
                 sticky_history=None,  # force one or more history items to be 'sticky' and remain at the top of the list
                 allowed_extensions="FITS files (.fits)|.fits|gzipped files (.gz)|.gz", # ignored in the dir case
                 title=None,  # None leads to a default, for NO title, use:  ''
                 assumed_prefix=None, #  can add a prefix to the file/dir name that will not be shown
                                      # assumed_prefix can be changed with:   self.set_assumed_prefix
                                      # Note that if an entry starts with /, then abspath is assumed and assumed_prefix is ignored
                                      # Basically, the interface just "hides" assumed_prefix from the user in the GUI
                 allow_glob_matching=False,  # if true, then entries can be, e.g.:   image*_[0-9][0-9][0-9].fits
                                             # allow_glob_matching is NOT compatible with is_files_not_dirs=False
                 default_entry=None,
                 maintain_default_entry_in_recents=False,  # can be True, or an integer to indicate where to put in list (e.g. 0, 1, -1)
                 ):
        wx.Panel.__init__(self, parent, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
        self.is_files_not_dirs = is_files_not_dirs
        self.max_history_items = max_history_items
        if history is None:
            self.history = []
        else:
            self.history = [a + '/' if (os.path.isdir(a) and not a.endswith('/')) else a for a in history]
        if sticky_history is None:
            self.sticky_history = []
        else:
            self.sticky_history = [a + '/' if (os.path.isdir(a) and not a.endswith('/')) else a for a in sticky_history]
        self.allowed_extensions = allowed_extensions
        if title is None:
            if self.is_files_not_dirs:
                title = 'File:'
            else:
                title = 'Dir:'
        self.assumed_prefix = assumed_prefix
        self.allow_glob_matching = allow_glob_matching
        if self.allow_glob_matching and not self.is_files_not_dirs:
            raise Error("allow_glob_matching=True and is_files_not_dirs=False is not compatible")
        if default_entry is None:
            default_entry = os.path.expanduser('~') + '/'
        self.default_entry = default_entry
        self.maintain_default_entry_in_recents = maintain_default_entry_in_recents
        self.title = title
        self.last_valid_entry = ''
        self.reset_auto_completion_info()
        self.current_textctrl_mode = 'ok'
        self.pause_on_current_textctrl_changed = False

        h_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.recent_choice = wx.Choice(self, -1, size=(24, -1), choices=self.get_history())
        self.Bind(wx.EVT_CHOICE, self.on_recent_choice, self.recent_choice)
        textentry_font = wx.Font(14, wx.FONTFAMILY_MODERN, wx.NORMAL, wx.FONTWEIGHT_LIGHT, False)
        self.current_textctrl = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize,
                                            wx.TE_PROCESS_ENTER | wx.TE_RIGHT)
        # note that wx.TE_RIGHT has no effect on Mac OSX, but is worthwhile leaving in for other systems
        self.current_textctrl.SetFont(textentry_font)
        self.current_textctrl.Bind(wx.EVT_TEXT, self.on_current_textctrl_changed)
        self.current_textctrl.Bind(wx.EVT_TEXT_ENTER, self.on_current_textctrl_entered)
        self.current_textctrl.Bind(wx.EVT_CHAR, self.on_key_press_textctrl, self.current_textctrl)
        h_sizer.Add(wx.StaticText(self, -1, title), 0, wx.ALIGN_CENTER_VERTICAL)
        h_sizer.Add(self.current_textctrl, 1, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL)
        h_sizer.AddSpacer((2, 0), 0)
        h_sizer.Add(self.recent_choice, 0, wx.ALIGN_CENTER_VERTICAL)
        h_sizer.AddSpacer((2, 0), 0)
        self.browser_button = wx.Button(self, -1, '...', style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.on_browser_button, self.browser_button)
        h_sizer.Add(self.browser_button, 0, wx.ALIGN_CENTER_VERTICAL)
        self.SetSizer(h_sizer)
        self.current_textctrl_SetValue(self.default_entry)
        self._on_load(self.current_textctrl_GetValue())

    def on_load(self, new_entry):
        # this is really a placeholder method, to be replaced when this class is implemented with a method that actually does something
        print("on_load called for {}".format(new_entry))

    def set_assumed_prefix(self, new_prefix):
        self.assumed_prefix = new_prefix
        self.update_recent_choice()

    def strip_assumed_prefix(self, entry):
        if self.assumed_prefix is not None and entry.startswith(self.assumed_prefix):
            return entry.replace(self.assumed_prefix, '', 1)
        else:
            return entry

    def addback_assumed_prefix(self, entry):
        if not entry.startswith('/') and not entry.startswith('~') and self.assumed_prefix is not None:
            return os.path.join(self.assumed_prefix, entry)
        else:
            return entry

    def current_textctrl_SetValue(self, entry):
        self.current_textctrl.SetValue(self.strip_assumed_prefix(entry))

    def current_textctrl_GetValue(self):
        return self.addback_assumed_prefix(self.current_textctrl.GetValue())

    def set_current_entry(self, new_entry):
        """
        This provides an external hook to call from external code to load an entry.
        It is not used internally.
        """
        self.current_textctrl_SetValue(new_entry)
        self.prepend_to_history(new_entry)
        cur_history = self.update_recent_choice()
        sys.stderr.write("\n\nnew_entry = {}\n\n".format(new_entry))
        sys.stderr.write("\n\ncur_history = {}\n\n".format(cur_history))
        self.recent_choice.SetSelection(cur_history.index(new_entry))

    def update_recent_choice(self):
        self.recent_choice.Clear()
        cur_history = self.get_history()
        self.recent_choice.AppendItems(cur_history)
        return cur_history

    def prepend_to_history(self, new_entry):
        if new_entry in self.history:  # pop if necessary so we can reinsert at start of list
            self.history.pop(self.history.index(new_entry))
        self.history.insert(0, new_entry)
        self.update_recent_choice()

    def _on_load(self, new_entry):
        """
        this is a private method that just updates the internals and then calls self.on_load
        """
        self.last_valid_entry = new_entry
        if not self.is_files_not_dirs and not self.last_valid_entry.endswith('/'):
            self.last_valid_entry += '/'
        self.current_textctrl_SetValue(self.last_valid_entry)<commit_msg>removed some leftover debugging code<commit_after>import wx
import os.path
import os
import glob
import sys
import subprocess
import pdb

# TODO:  fix tab order through controls
# TODO:  fix alignment in text field, ideally would be right aligned, but may not be able to do that on OSX
# TODO: limit default allowed_extensions to .fits/.fits.gz  (rather than .fits/.gz)
# TODO: fix that is currently hardwired to .fits/.gz file endings and allow calling code to specify other options

# TODO: implement a None option? or that None is italixiszed in text box and dropdown menu as a special thing?

class Error(Exception):
    pass


class FilePicker(wx.Panel):
    def __init__(self, parent,
                 is_files_not_dirs=True,  # True to look for files, False to look for dirs
                 max_history_items=20,
                 history=None,  # pre-load items into history
                 sticky_history=None,  # force one or more history items to be 'sticky' and remain at the top of the list
                 allowed_extensions="FITS files (.fits)|.fits|gzipped files (.gz)|.gz", # ignored in the dir case
                 title=None,  # None leads to a default, for NO title, use:  ''
                 assumed_prefix=None, #  can add a prefix to the file/dir name that will not be shown
                                      # assumed_prefix can be changed with:   self.set_assumed_prefix
                                      # Note that if an entry starts with /, then abspath is assumed and assumed_prefix is ignored
                                      # Basically, the interface just "hides" assumed_prefix from the user in the GUI
                 allow_glob_matching=False,  # if true, then entries can be, e.g.:   image*_[0-9][0-9][0-9].fits
                                             # allow_glob_matching is NOT compatible with is_files_not_dirs=False
                 default_entry=None,
                 maintain_default_entry_in_recents=False,  # can be True, or an integer to indicate where to put in list (e.g. 0, 1, -1)
                 ):
        wx.Panel.__init__(self, parent, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
        self.is_files_not_dirs = is_files_not_dirs
        self.max_history_items = max_history_items
        if history is None:
            self.history = []
        else:
            self.history = [a + '/' if (os.path.isdir(a) and not a.endswith('/')) else a for a in history]
        if sticky_history is None:
            self.sticky_history = []
        else:
            self.sticky_history = [a + '/' if (os.path.isdir(a) and not a.endswith('/')) else a for a in sticky_history]
        self.allowed_extensions = allowed_extensions
        if title is None:
            if self.is_files_not_dirs:
                title = 'File:'
            else:
                title = 'Dir:'
        self.assumed_prefix = assumed_prefix
        self.allow_glob_matching = allow_glob_matching
        if self.allow_glob_matching and not self.is_files_not_dirs:
            raise Error("allow_glob_matching=True and is_files_not_dirs=False is not compatible")
        if default_entry is None:
            default_entry = os.path.expanduser('~') + '/'
        self.default_entry = default_entry
        self.maintain_default_entry_in_recents = maintain_default_entry_in_recents
        self.title = title
        self.last_valid_entry = ''
        self.reset_auto_completion_info()
        self.current_textctrl_mode = 'ok'
        self.pause_on_current_textctrl_changed = False

        h_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.recent_choice = wx.Choice(self, -1, size=(24, -1), choices=self.get_history())
        self.Bind(wx.EVT_CHOICE, self.on_recent_choice, self.recent_choice)
        textentry_font = wx.Font(14, wx.FONTFAMILY_MODERN, wx.NORMAL, wx.FONTWEIGHT_LIGHT, False)
        self.current_textctrl = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize,
                                            wx.TE_PROCESS_ENTER | wx.TE_RIGHT)
        # note that wx.TE_RIGHT has no effect on Mac OSX, but is worthwhile leaving in for other systems
        self.current_textctrl.SetFont(textentry_font)
        self.current_textctrl.Bind(wx.EVT_TEXT, self.on_current_textctrl_changed)
        self.current_textctrl.Bind(wx.EVT_TEXT_ENTER, self.on_current_textctrl_entered)
        self.current_textctrl.Bind(wx.EVT_CHAR, self.on_key_press_textctrl, self.current_textctrl)
        h_sizer.Add(wx.StaticText(self, -1, title), 0, wx.ALIGN_CENTER_VERTICAL)
        h_sizer.Add(self.current_textctrl, 1, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL)
        h_sizer.AddSpacer((2, 0), 0)
        h_sizer.Add(self.recent_choice, 0, wx.ALIGN_CENTER_VERTICAL)
        h_sizer.AddSpacer((2, 0), 0)
        self.browser_button = wx.Button(self, -1, '...', style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.on_browser_button, self.browser_button)
        h_sizer.Add(self.browser_button, 0, wx.ALIGN_CENTER_VERTICAL)
        self.SetSizer(h_sizer)
        self.current_textctrl_SetValue(self.default_entry)
        self._on_load(self.current_textctrl_GetValue())

    def on_load(self, new_entry):
        # this is really a placeholder method, to be replaced when this class is implemented with a method that actually does something
        print("on_load called for {}".format(new_entry))

    def set_assumed_prefix(self, new_prefix):
        self.assumed_prefix = new_prefix
        self.update_recent_choice()

    def strip_assumed_prefix(self, entry):
        if self.assumed_prefix is not None and entry.startswith(self.assumed_prefix):
            return entry.replace(self.assumed_prefix, '', 1)
        else:
            return entry

    def addback_assumed_prefix(self, entry):
        if not entry.startswith('/') and not entry.startswith('~') and self.assumed_prefix is not None:
            return os.path.join(self.assumed_prefix, entry)
        else:
            return entry

    def current_textctrl_SetValue(self, entry):
        self.current_textctrl.SetValue(self.strip_assumed_prefix(entry))

    def current_textctrl_GetValue(self):
        return self.addback_assumed_prefix(self.current_textctrl.GetValue())

    def set_current_entry(self, new_entry):
        """
        This provides an external hook to call from external code to load an entry.
        It is not used internally.
        """
        self.current_textctrl_SetValue(new_entry)
        self.prepend_to_history(new_entry)
        cur_history = self.update_recent_choice()
        self.recent_choice.SetSelection(cur_history.index(new_entry))

    def update_recent_choice(self):
        self.recent_choice.Clear()
        cur_history = self.get_history()
        self.recent_choice.AppendItems(cur_history)
        return cur_history

    def prepend_to_history(self, new_entry):
        if new_entry in self.history:  # pop if necessary so we can reinsert at start of list
            self.history.pop(self.history.index(new_entry))
        self.history.insert(0, new_entry)
        self.update_recent_choice()

    def _on_load(self, new_entry):
        """
        this is a private method that just updates the internals and then calls self.on_load
        """
        self.last_valid_entry = new_entry
        if not self.is_files_not_dirs and not self.last_valid_entry.endswith('/'):
            self.last_valid_entry += '/'
        self.current_textctrl_SetValue(self.last_valid_entry)
{'fraction_non_alphanumeric': 0.0843109962219129, 'fraction_numerical': 0.004109498243520912, 'mean_word_length': 2.6052568697729988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5857384', 'n_tokens_mistral': 4635, 'n_tokens_neox': 4493, 'n_words': 1145}