<commit_before>import json
import os
import tempfile
import webbrowser
from io import open

import designer
from designer.uix.settings import SettingDict, SettingList
from designer.utils.utils import get_kd_data_dir, ignore_proj_watcher
from kivy.properties import ConfigParser, ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import (
    ContentPanel,
    InterfaceWithSidebar,
    MenuSidebar,
    Settings,
    SettingsPanel,
)
from pygments.lexers.configs import IniLexer


class SpecContentPanel(ContentPanel):

    def on_current_uid(self, *args):
        result = super(SpecContentPanel, self).on_current_uid(*args)
        if isinstance(self.current_panel, SpecCodeInput):
            self.current_panel.load_spec()
        return result


class SpecMenuSidebar(MenuSidebar):

    def on_selected_uid(self, *args):
        '''(internal) unselects any currently selected menu buttons, unless
        they represent the current panel.

        '''
        for button in self.buttons_layout.children:
            button.selected = button.uid == self.selected_uid


class SpecEditorInterface(InterfaceWithSidebar):

    def open_buildozer_docs(self, *args):
        webbrowser.open('http://buildozer.readthedocs.org')


class SpecSettingsPanel(SettingsPanel):

    def get_value(self, section, key):
        '''Return the value of the section/key from the :attr:`config`
        ConfigParser instance. This function is used by :class:`SettingItem` to
        get the value for a given section/key.

        If you don't want to use a ConfigParser instance, you might want to
        override this function.
        '''
        config = self.config
        if not config:
            return
        if config.has_option(section, key):
            return config.get(section, key)
        else:
            return ''

    def set_value(self, section, key, value):
        # some keys are not enabled by default on .spec. If the value is empty
        # and this key is not on .spec, so we don't need to save it
        if not value and not self.config.has_option(section, key):
            return False
        super(SpecSettingsPanel, self).set_value(section, key, value)


class SpecCodeInput(BoxLayout):

    text_input = ObjectProperty(None)
    '''CodeInput with buildozer.spec text.
     Instance of :class:`kivy.config.ObjectProperty` and defaults to None
    '''

    lbl_error = ObjectProperty(None)
    '''(internal) Label to display errors.
     Instance of :class:`kivy.config.ObjectProperty` and defaults to None
    '''

    spec_path = StringProperty('')
    '''buildozer.spec path.
    Instance of :class:`kivy.config.StringProperty` and defaults to ''
    '''

    __events__ = ('on_change', )

    def __init__(self, **kwargs):
        super(SpecCodeInput, self).__init__(**kwargs)
        self.text_input.lexer = IniLexer()

    def load_spec(self, *args):
        '''Read the buildozer.spec and update the CodeInput
        '''
        self.lbl_error.color = [0, 0, 0, 0]
        self.text_input.text = open(self.spec_path, 'r', encoding='utf-8').read()

    @ignore_proj_watcher
    def _save_spec(self, *args):
        '''Try to save the spec file. If there is a error, show the label.
        If not, save the file and dispatch on_change
        '''
        f = tempfile.NamedTemporaryFile()
        f.write(self.text_input.text)
        try:
            cfg = ConfigParser()
            cfg.read(f.name)
        except Exception:
            self.lbl_error.color = [1, 0, 0, 1]<commit_msg>fix style<commit_after>import json
import os
import tempfile
import webbrowser
from io import open

import designer
from designer.uix.settings import SettingDict, SettingList
from designer.utils.utils import get_kd_data_dir, ignore_proj_watcher
from kivy.properties import ConfigParser, ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import (
    ContentPanel,
    InterfaceWithSidebar,
    MenuSidebar,
    Settings,
    SettingsPanel,
)
from pygments.lexers.configs import IniLexer


class SpecContentPanel(ContentPanel):

    def on_current_uid(self, *args):
        result = super(SpecContentPanel, self).on_current_uid(*args)
        if isinstance(self.current_panel, SpecCodeInput):
            self.current_panel.load_spec()
        return result


class SpecMenuSidebar(MenuSidebar):

    def on_selected_uid(self, *args):
        '''(internal) unselects any currently selected menu buttons, unless
        they represent the current panel.

        '''
        for button in self.buttons_layout.children:
            button.selected = button.uid == self.selected_uid


class SpecEditorInterface(InterfaceWithSidebar):

    def open_buildozer_docs(self, *args):
        webbrowser.open('http://buildozer.readthedocs.org')


class SpecSettingsPanel(SettingsPanel):

    def get_value(self, section, key):
        '''Return the value of the section/key from the :attr:`config`
        ConfigParser instance. This function is used by :class:`SettingItem` to
        get the value for a given section/key.

        If you don't want to use a ConfigParser instance, you might want to
        override this function.
        '''
        config = self.config
        if not config:
            return
        if config.has_option(section, key):
            return config.get(section, key)
        else:
            return ''

    def set_value(self, section, key, value):
        # some keys are not enabled by default on .spec. If the value is empty
        # and this key is not on .spec, so we don't need to save it
        if not value and not self.config.has_option(section, key):
            return False
        super(SpecSettingsPanel, self).set_value(section, key, value)


class SpecCodeInput(BoxLayout):

    text_input = ObjectProperty(None)
    '''CodeInput with buildozer.spec text.
     Instance of :class:`kivy.config.ObjectProperty` and defaults to None
    '''

    lbl_error = ObjectProperty(None)
    '''(internal) Label to display errors.
     Instance of :class:`kivy.config.ObjectProperty` and defaults to None
    '''

    spec_path = StringProperty('')
    '''buildozer.spec path.
    Instance of :class:`kivy.config.StringProperty` and defaults to ''
    '''

    __events__ = ('on_change', )

    def __init__(self, **kwargs):
        super(SpecCodeInput, self).__init__(**kwargs)
        self.text_input.lexer = IniLexer()

    def load_spec(self, *args):
        '''Read the buildozer.spec and update the CodeInput
        '''
        self.lbl_error.color = [0, 0, 0, 0]
        self.text_input.text = open(self.spec_path, 'r',
                                    encoding='utf-8').read()

    @ignore_proj_watcher
    def _save_spec(self, *args):
        '''Try to save the spec file. If there is a error, show the label.
        If not, save the file and dispatch on_change
        '''
        f = tempfile.NamedTemporaryFile()
        f.write(self.text_input.text)
        try:
            cfg = ConfigParser()
            cfg.read(f.name)
        except Exception:
            self.lbl_error.color = [1, 0, 0, 1]
{'fraction_non_alphanumeric': 0.09195081050866406, 'fraction_numerical': 0.002515371716042482, 'mean_word_length': 2.78276955602537, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4032499', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2025, 'n_words': 610}