<commit_before>
import re
import json
import os.path

import sublime
import sublime_plugin

try:
    from Utils.packages import find_all_packages
    from Utils.touch import add_event_handler_async
except ImportError:
    try:
        # Allow for repository name
        from sublime_utils.packages import find_all_packages
        from sublime_utils.touch import add_event_handler_async
    except ImportError:
        print('[ColorSchemes] Please install the sublime_utils plugin (Utils) also.')
        print('It can be found here: https://github.com/sligodave/sublime_utils/')
        find_all_packages = add_event_handler_async = None


COLOR_SCHEMES_INSTANCE = None


base_tmTheme = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict><key>name</key><string>Color Schemes Theme</string>
<key>settings</key><array><dict><key>settings</key><dict><key>background</key>
<string>#ffffff</string><key>caret</key><string>#ffffff</string><key>foreground</key>
<string>#000000</string></dict></dict><dict><key>name</key><string>button</string>
<key>scope</key><string>button</string><key>settings</key><dict><key>background</key>
<string>#bbbbbb</string><key>foreground</key><string>#000000</string><key>caret</key>
<string>#bbbbbb</string></dict></dict>
<dict><key>name</key><string>horizontal_rule</string>
<key>scope</key><string>horizontal_rule</string><key>settings</key><dict><key>background</key>
<string>#cccccc</string><key>foreground</key><string>#cccccd</string><key>caret</key>
<string>#cccccc</string></dict></dict>%s</array><key>uuid</key>
<string>905FEC5A-2E43-934F-EC9C-A3982DFDED3F</string></dict></plist>"""

base_color = """<dict><key>name</key><string>%s</string><key>scope</key>
<string>%s</string><key>settings</key><dict><key>background</key>
<string>%s</string><key>foreground</key><string>%s</string>
<key>caret</key><string>%s</string></dict></dict>"""


class ColorSchemes:
    def __init__(self, window):
        global COLOR_SCHEMES_INSTANCE
        self.view = window.new_file()
        self.change_layout()
        self.get_color_scheme()
        self.update_view()
        COLOR_SCHEMES_INSTANCE = self

    def update_view(self):
        data = '\n Close and Revert Layout \n Restore Color Scheme \n\n'

        color_schemes = []
        regions = []
        package_and_file_names = []
        packages = find_all_packages(contents=True, extensions='.tmTheme')
        for package_name, package in packages.items():
            for file_name in package['files']:
                if not file_name.endswith('.tmTheme') or file_name == 'color_schemes.tmTheme':
                    continue

                content = package['contents'][file_name]
                start = re.search('<dict>\s*<key>settings</key>', content)
                if start:
                    start = start.start()
                    background_color = '#bbbbbb'
                    background = re.search(
                        '<key>background</key>\s*<string>([^<]+)</string>', content[start:])
                    if background:
                        background_color = background.group(1)
                        if background_color.lower() == '#ffffff':
                            background_color = '#fffffe'
                    foreground_color = '#000000'
                    foreground = re.search(
                        '<key>foreground</key>\s*<string>([^<]+)</string>', content[start:])
                    if foreground:
                        foreground_color = foreground.group(1)
                    caret_color = '#bbbbbb'
                    caret = re.search(
                        '<key>caret</key>\s*<string>([^<]+)</string>', content[start:])
                    if caret:
                        caret_color = caret.group(1)
                    color_schemes.append(
                        base_color % (
                            file_name[:-8],
                            file_name[:-8],
                            background_color,
                            foreground_color,
                            caret_color
                        )
                    )
                package_and_file_names.append([package_name, file_name])

        package_and_file_names.sort(key=lambda x: x[1][:-8].title())

        for package_name, file_name in package_and_file_names:
            def handler(package_name, file_name):
                def handler(view, region, point):
                    with open(self.user_prefs_path, 'r') as user_prefs_file:
                        user_prefs = json.load(user_prefs_file)
                    user_prefs['color_scheme'] = 'Packages/%s/%s' % (
                        package_name, file_name)
                    with open(self.user_prefs_path, 'w') as user_prefs_file:
                        json.dump(user_prefs, user_prefs_file, indent='\t')
                return handler
            handler = handler(package_name, file_name)

            text = ' %s \n' % (file_name[:-8].title())
            regions.append(
                [
                    text.strip(),
                    [sublime.Region(len(data), len(data) + len(text) - 1)],
                    file_name[:-8],
                    '',
                    sublime.DRAW_NO_OUTLINE,
                    handler,
                ]
            )
            data += text

        ######################################################
        # Set up view
        ######################################################

        color_scheme = base_tmTheme % ''.join(color_schemes)
        color_scheme_path = os.path.join(
            os.path.dirname(__file__),
            'color_schemes.tmTheme'
        )
        with open(color_scheme_path, 'w') as color_scheme_file:
            color_scheme_file.write(color_scheme)

        self.view.set_name('Color Schemes')
        self.view.set_read_only(True)
        self.view.set_scratch(True)
        settings = {
            'rulers': [],
            'highlight_line': False,
            'fade_fold_buttons': True,
            'caret_style': 'solid',
            'line_numbers': False,
            'draw_white_space': 'none',
            'gutter': False,
            'word_wrap': False,
            'indent_guide_options': [],
            'line_padding_top': 5,
            'line_padding_bottom': 5,
            'draw_centered': True,
            'color_scheme': 'Packages/ColorSchemes/color_schemes.tmTheme',
        }
        for name, value in settings.items():
            self.view.settings().set(name, value)
        self.view.run_command(
            'utils_edit_view',
            {
                'start': 0,
                'data': data
            }
        )
        for region in regions:
            self.view.add_regions(*region[:5])
            add_event_handler_async(self.view, region[1][0], region[5])
        region1 = sublime.Region(1, 26)<commit_msg>Updated to not reply on the install path to this package. Now writes the color scheme file to the User directory instead.<commit_after>
import re
import json
import os.path

import sublime
import sublime_plugin

try:
    from Utils.packages import find_all_packages
    from Utils.touch import add_event_handler_async
except ImportError:
    try:
        # Allow for repository name
        from sublime_utils.packages import find_all_packages
        from sublime_utils.touch import add_event_handler_async
    except ImportError:
        print('[ColorSchemes] Please install the sublime_utils plugin (Utils) also.')
        print('It can be found here: https://github.com/sligodave/sublime_utils/')
        find_all_packages = add_event_handler_async = None


COLOR_SCHEMES_INSTANCE = None


base_tmTheme = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict><key>name</key><string>Color Schemes Theme</string>
<key>settings</key><array><dict><key>settings</key><dict><key>background</key>
<string>#ffffff</string><key>caret</key><string>#ffffff</string><key>foreground</key>
<string>#000000</string></dict></dict><dict><key>name</key><string>button</string>
<key>scope</key><string>button</string><key>settings</key><dict><key>background</key>
<string>#bbbbbb</string><key>foreground</key><string>#000000</string><key>caret</key>
<string>#bbbbbb</string></dict></dict>
<dict><key>name</key><string>horizontal_rule</string>
<key>scope</key><string>horizontal_rule</string><key>settings</key><dict><key>background</key>
<string>#cccccc</string><key>foreground</key><string>#cccccd</string><key>caret</key>
<string>#cccccc</string></dict></dict>%s</array><key>uuid</key>
<string>905FEC5A-2E43-934F-EC9C-A3982DFDED3F</string></dict></plist>"""

base_color = """<dict><key>name</key><string>%s</string><key>scope</key>
<string>%s</string><key>settings</key><dict><key>background</key>
<string>%s</string><key>foreground</key><string>%s</string>
<key>caret</key><string>%s</string></dict></dict>"""


class ColorSchemes:
    def __init__(self, window):
        global COLOR_SCHEMES_INSTANCE
        self.view = window.new_file()
        self.change_layout()
        self.get_color_scheme()
        self.update_view()
        COLOR_SCHEMES_INSTANCE = self

    def update_view(self):
        data = '\n Close and Revert Layout \n Restore Color Scheme \n\n'

        color_schemes = []
        regions = []
        package_and_file_names = []
        packages = find_all_packages(contents=True, extensions='.tmTheme')
        for package_name, package in packages.items():
            for file_name in package['files']:
                if not file_name.endswith('.tmTheme') or file_name == 'color_schemes.tmTheme':
                    continue

                content = package['contents'][file_name]
                start = re.search('<dict>\s*<key>settings</key>', content)
                if start:
                    start = start.start()
                    background_color = '#bbbbbb'
                    background = re.search(
                        '<key>background</key>\s*<string>([^<]+)</string>', content[start:])
                    if background:
                        background_color = background.group(1)
                        if background_color.lower() == '#ffffff':
                            background_color = '#fffffe'
                    foreground_color = '#000000'
                    foreground = re.search(
                        '<key>foreground</key>\s*<string>([^<]+)</string>', content[start:])
                    if foreground:
                        foreground_color = foreground.group(1)
                    caret_color = '#bbbbbb'
                    caret = re.search(
                        '<key>caret</key>\s*<string>([^<]+)</string>', content[start:])
                    if caret:
                        caret_color = caret.group(1)
                    color_schemes.append(
                        base_color % (
                            file_name[:-8],
                            file_name[:-8],
                            background_color,
                            foreground_color,
                            caret_color
                        )
                    )
                package_and_file_names.append([package_name, file_name])

        package_and_file_names.sort(key=lambda x: x[1][:-8].title())

        for package_name, file_name in package_and_file_names:
            def handler(package_name, file_name):
                def handler(view, region, point):
                    with open(self.user_prefs_path, 'r') as user_prefs_file:
                        user_prefs = json.load(user_prefs_file)
                    user_prefs['color_scheme'] = 'Packages/%s/%s' % (
                        package_name, file_name)
                    with open(self.user_prefs_path, 'w') as user_prefs_file:
                        json.dump(user_prefs, user_prefs_file, indent='\t')
                return handler
            handler = handler(package_name, file_name)

            text = ' %s \n' % (file_name[:-8].title())
            regions.append(
                [
                    text.strip(),
                    [sublime.Region(len(data), len(data) + len(text) - 1)],
                    file_name[:-8],
                    '',
                    sublime.DRAW_NO_OUTLINE,
                    handler,
                ]
            )
            data += text

        ######################################################
        # Set up view
        ######################################################

        color_scheme = base_tmTheme % ''.join(color_schemes)
        color_scheme_path = os.path.join(sublime.packages_path(), 'User', 'color_schemes.tmTheme')
        with open(color_scheme_path, 'w') as color_scheme_file:
            color_scheme_file.write(color_scheme)

        self.view.set_name('Color Schemes')
        self.view.set_read_only(True)
        self.view.set_scratch(True)
        settings = {
            'rulers': [],
            'highlight_line': False,
            'fade_fold_buttons': True,
            'caret_style': 'solid',
            'line_numbers': False,
            'draw_white_space': 'none',
            'gutter': False,
            'word_wrap': False,
            'indent_guide_options': [],
            'line_padding_top': 5,
            'line_padding_bottom': 5,
            'draw_centered': True,
            'color_scheme': 'Packages/User/color_schemes.tmTheme',
        }
        for name, value in settings.items():
            self.view.settings().set(name, value)
        self.view.run_command(
            'utils_edit_view',
            {
                'start': 0,
                'data': data
            }
        )
        for region in regions:
            self.view.add_regions(*region[:5])
            add_event_handler_async(self.view, region[1][0], region[5])
        region1 = sublime.Region(1, 26)
{'fraction_non_alphanumeric': 0.14758522727272727, 'fraction_numerical': 0.00909090909090909, 'mean_word_length': 2.053122289679098, 'pattern_counts': {'":': 0, '<': 267, '<?xml version=': 2, '>': 261, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2434719', 'n_tokens_mistral': 4202, 'n_tokens_neox': 4000, 'n_words': 646}