<commit_before>import os
import glob
import re
import subprocess


import sublime
import sublime_plugin


class OpenSesameCommand(sublime_plugin.WindowCommand):

    def run(self, path = None):
        self.folders = find_folders(path)
        if self.folders:
            self.window.show_quick_panel(self.folders, self.on_done)

    def on_done(self, index):
        if index == -1:
            return

        folder = self.folders[index][1]
        folder_projects = glob.glob(folder + '/*.sublime-project')

        if len(folder_projects) == 1 and os.path.isfile(folder_projects[0]):
            subl_open_project_in_new_window(folder_projects[0])
        elif os.path.isdir(folder):
            subl_open_folder_in_new_window(folder)


class OpenSesameAddFolderCommand(sublime_plugin.WindowCommand):

    def run(self, path = None):
        # Exclude folders that already exist
        existing_folders = []
        project_data = self.window.project_data()
        if project_data:
            if 'folders' in project_data:
                for folder in project_data['folders']:
                    if folder['path']:
                        if folder['path'] not in existing_folders:
                            existing_folders.append(folder['path'])

        self.folders = []
        for folder in find_folders(path):
            if folder[1] not in existing_folders:
                self.folders.append(folder)

        if self.folders:
            self.window.show_quick_panel(self.folders, self.on_done)

    def on_done(self, index):
        if index == -1:
            return
        subl_add_folder(self.window, self.folders[index][1])


def find_folders(base_path = None):
    if not base_path:
        window = sublime.active_window()
        if window:
            view = window.active_view()
            if view:
                base_path = view.settings().get('open-sesame.projects_path')

    if not base_path:
        base_path = os.getenv('PROJECTS_PATH')

    if not base_path:
        return None


    paths = base_path.split(os.pathsep)
    paths = [os.path.expanduser(path) for path in paths]

    for path in paths:
        if not os.path.isdir(path):
            raise ValueError("{path} must be a valid directory".format(path=path))

    folders = glob_paths(paths)
    folders.sort()
    return folders


def flatten_once(array_of_arrays):
    return [item for array in array_of_arrays for item in array]


def glob_paths(paths):
    globs = [glob_path(path) for path in paths]
    folders = flatten_once(globs)
    return folders


def glob_path(base_path = None):
    folders = []

    for folder in glob.glob(base_path + '/*/*/'):
        folder_match_result = re.match('^.*\/([a-zA-Z0-9\._-]+\/[a-zA-Z0-9\._-]+)\/$', folder)
        if folder_match_result:
            folder_name = folder_match_result.group(1)
            folder_path = os.path.normpath(folder)
            folder_struct = [folder_name, folder_path]
            if folder_struct not in folders:
                folders.append(folder_struct)

    return folders


def subl_open_project_in_new_window(sublime_project_file):<commit_msg>Fix projects path glob (Windows)<commit_after>import os
import glob
import re
import subprocess


import sublime
import sublime_plugin


class OpenSesameCommand(sublime_plugin.WindowCommand):

    def run(self, path = None):
        self.folders = find_folders(path)
        if self.folders:
            self.window.show_quick_panel(self.folders, self.on_done)

    def on_done(self, index):
        if index == -1:
            return

        folder = self.folders[index][1]
        folder_projects = glob.glob(folder + '/*.sublime-project')

        if len(folder_projects) == 1 and os.path.isfile(folder_projects[0]):
            subl_open_project_in_new_window(folder_projects[0])
        elif os.path.isdir(folder):
            subl_open_folder_in_new_window(folder)


class OpenSesameAddFolderCommand(sublime_plugin.WindowCommand):

    def run(self, path = None):
        # Exclude folders that already exist
        existing_folders = []
        project_data = self.window.project_data()
        if project_data:
            if 'folders' in project_data:
                for folder in project_data['folders']:
                    if folder['path']:
                        if folder['path'] not in existing_folders:
                            existing_folders.append(folder['path'])

        self.folders = []
        for folder in find_folders(path):
            if folder[1] not in existing_folders:
                self.folders.append(folder)

        if self.folders:
            self.window.show_quick_panel(self.folders, self.on_done)

    def on_done(self, index):
        if index == -1:
            return
        subl_add_folder(self.window, self.folders[index][1])


def find_folders(base_path = None):
    if not base_path:
        window = sublime.active_window()
        if window:
            view = window.active_view()
            if view:
                base_path = view.settings().get('open-sesame.projects_path')

    if not base_path:
        base_path = os.getenv('PROJECTS_PATH')

    if not base_path:
        return None


    paths = base_path.split(os.pathsep)
    paths = [os.path.expanduser(path) for path in paths]

    for path in paths:
        if not os.path.isdir(path):
            raise ValueError("{path} must be a valid directory".format(path=path))

    folders = glob_paths(paths)
    folders.sort()
    return folders


def flatten_once(array_of_arrays):
    return [item for array in array_of_arrays for item in array]


def glob_paths(paths):
    globs = [glob_path(path) for path in paths]
    folders = flatten_once(globs)
    return folders


def glob_path(base_path = None):
    folders = []

    for folder in glob.glob(base_path + '/*/*/'):
        if sublime.platform() == 'windows':
            folder_match_result = re.match('^.*\\\\([a-zA-Z0-9\._-]+\\\\[a-zA-Z0-9\._-]+)\\\\$', folder)
        else:
            folder_match_result = re.match('^.*\/([a-zA-Z0-9\._-]+\/[a-zA-Z0-9\._-]+)\/$', folder)
        if folder_match_result:
            folder_name = folder_match_result.group(1)
            folder_path = os.path.normpath(folder)
            folder_struct = [folder_name, folder_path]
            if folder_struct not in folders:
                folders.append(folder_struct)

    return folders


def subl_open_project_in_new_window(sublime_project_file):
{'fraction_non_alphanumeric': 0.10640272193009588, 'fraction_numerical': 0.0046396535725332505, 'mean_word_length': 2.352514256091239, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1107017', 'n_tokens_mistral': 2076, 'n_tokens_neox': 1984, 'n_words': 426}