<commit_before>import os
from cudatext import *
import cudax_lib as apx
import cuda_addonman
import urllib.request
import tempfile

URL_DB = 'https://raw.githubusercontent.com/Alexey-T/CudaText-registry/master/multi_inst/db.py'
COLUMN_LEN = 20
COLUMN_W = 240

def str_to_bool(s):
    return s == '1'
def bool_to_str(v):
    return '1' if v else '0'

def is_file_html(fn):
    if os.path.exists(fn):
        with open(fn, 'r', encoding='cp437') as f:
            s = f.readline(10).lower()
            return s.startswith('<html>')
    return False


def make_plugin_group(regex, name):

    apx.set_opt('plugin_groups/'+regex, name)


class Command:

    def load_repo(self):

        fn = os.path.join(tempfile.gettempdir(), 'cuda_multi_installer_db.py')
        cuda_addonman.work_remote.get_url(URL_DB, fn, True)

        if not os.path.exists(fn):
            self.packets = []
            self.installed_list = []
            return

        exec(open(fn).read())
        os.remove(fn)

        self.packets = cuda_addonman.work_remote.get_remote_addons_list(cuda_addonman.opt.ch_def+cuda_addonman.opt.ch_user)
        self.installed_list = cuda_addonman.work_local.get_installed_list()

    def get_module(self,kind,name):

        k = TYPE_TO_KIND.get(kind)
        if not k:
            return ''
        for i in self.packets:
            if i['kind']==k and i['name']==name:
                return i.get('module','')
        return ''

    def get_url(self,kind,name):

        k = TYPE_TO_KIND.get(kind)
        for i in self.packets:
            if i['kind']==k and i['name']==name:
                return (i['url'], i['v'])
        return ('', '')

    def is_installed(self,kind,name):

        if kind==T_LEXER:
            d = os.path.join(app_path(APP_DIR_DATA), 'lexlib')
            lst = os.listdir(d)
            lst = [i.lower() for i in lst]
            name_file = name.lower().replace('_', ' ')+'.lcf'
            res = name_file in lst
            #if res:
            #    print('lexer installed:', name)
            return res

        m = self.get_module(kind,name)
        res = m and m in self.installed_list
        #if res:
        #    print('installed:', kind,name)
        return res

    def install(self,kind,name):

        url, version = self.get_url(kind,name)
        if not url:
            print('Not found: %s %s'%(kind, name))
            self.error_count+=1
            return

        state='Installing: %s %s'%(kind,name)
        print(state)
        self.show_progress()

        fn = cuda_addonman.work_remote.get_plugin_zip(url)

        # handle SF.net HTML error file
        if is_file_html(fn):
            os.remove(fn)

        if not os.path.isfile(fn):
            self.error_count += 1
            return

        ok = file_open(fn, options='/silent')
        if ok:
            self.ok_count += 1
            cuda_addonman.work_local.do_save_version(url, fn, version)


    def open_menu(self):

        self.load_repo()
        if not self.packets:
            msg_status('Multi Installer: cannot download list')
            return

        langs = list(PLUGINS.keys())
        langs.sort()
        h=app_proc(PROC_GET_GUI_HEIGHT,'check')

        to_install = {}
        for i in CLASSES:
            to_install[i] = []

        RES_LIST = 2
        RES_GR_WEB = 3
        RES_GR_PY = 4
        RES_GR_XML = 5
        RES_NEXT = 7
        
        res = dlg_custom('CudaText Multi Installer', 360, 460, '\n'.join([
            '\1'.join(['type=label','pos=5,5,350,0','cap=Select language(s) needed for you.']),
            '\1'.join(['type=label','pos=5,25,350,0','cap=Next steps will suggest add-ons for them.']),
            '\1'.join(['type=checklistbox','pos=5,48,355,330','items='+
                '\t'.join(langs)
                ]),
            '\1'.join(['type=check', 'pos=5,335,300,0', 'cap=Make menu group "Web" for HTML/CSS/JS/PHP']),
            '\1'.join(['type=check', 'pos=5,365,300,0', 'cap=Make menu group "Python"']),
            '\1'.join(['type=check', 'pos=5,395,300,0', 'cap=Make menu group "XML"']),
            '\1'.join(['type=button','pos=5,425,85,0','cap=Cancel']),
            '\1'.join(['type=button','pos=275,425,355,0','cap=Next']),
            ]),
            get_dict=True
            )
        if res is None:
            return
        if res['clicked']!=RES_NEXT:
            return

        if res[RES_GR_WEB]=='1':
            make_plugin_group('HTML.+', 'Web')
            make_plugin_group('CSS.+', 'Web')
            make_plugin_group('JS.+', 'Web')
            make_plugin_group('PHP.+', 'Web')
            
        if res[RES_GR_PY]=='1':
            make_plugin_group('Python.+', 'Python')
            
        if res[RES_GR_XML]=='1':
            make_plugin_group('XML.+', 'XML')

        res_list = res[RES_LIST].split(';')[1].split(',')
        res_list = list(map(str_to_bool,res_list))
        step_count = sum(map(int,res_list))+1
        step_index = 2

        for i,f in enumerate(res_list):
            if f:
                cl = 0
                line = 0
                UI = []
                UI_reg = [()]
                for curr_class in CLASSES:
                        pls = PLUGINS[langs[i]].setdefault(curr_class)
                        if pls:
                            if line in (COLUMN_LEN,COLUMN_LEN-1):
                                cl+=1
                                line = 0
                            UI.append('\1'.join([<commit_msg>MultiInst: remove Group checkboxes<commit_after>import os
from cudatext import *
import cudax_lib as apx
import cuda_addonman
import urllib.request
import tempfile

URL_DB = 'https://raw.githubusercontent.com/Alexey-T/CudaText-registry/master/multi_inst/db.py'
COLUMN_LEN = 20
COLUMN_W = 240

def str_to_bool(s):
    return s == '1'
def bool_to_str(v):
    return '1' if v else '0'

def is_file_html(fn):
    if os.path.exists(fn):
        with open(fn, 'r', encoding='cp437') as f:
            s = f.readline(10).lower()
            return s.startswith('<html>')
    return False


def make_plugin_group(regex, name):

    apx.set_opt('plugin_groups/'+regex, name)


class Command:

    def load_repo(self):

        fn = os.path.join(tempfile.gettempdir(), 'cuda_multi_installer_db.py')
        cuda_addonman.work_remote.get_url(URL_DB, fn, True)

        if not os.path.exists(fn):
            self.packets = []
            self.installed_list = []
            return

        exec(open(fn).read())
        os.remove(fn)

        self.packets = cuda_addonman.work_remote.get_remote_addons_list(cuda_addonman.opt.ch_def+cuda_addonman.opt.ch_user)
        self.installed_list = cuda_addonman.work_local.get_installed_list()

    def get_module(self,kind,name):

        k = TYPE_TO_KIND.get(kind)
        if not k:
            return ''
        for i in self.packets:
            if i['kind']==k and i['name']==name:
                return i.get('module','')
        return ''

    def get_url(self,kind,name):

        k = TYPE_TO_KIND.get(kind)
        for i in self.packets:
            if i['kind']==k and i['name']==name:
                return (i['url'], i['v'])
        return ('', '')

    def is_installed(self,kind,name):

        if kind==T_LEXER:
            d = os.path.join(app_path(APP_DIR_DATA), 'lexlib')
            lst = os.listdir(d)
            lst = [i.lower() for i in lst]
            name_file = name.lower().replace('_', ' ')+'.lcf'
            res = name_file in lst
            #if res:
            #    print('lexer installed:', name)
            return res

        m = self.get_module(kind,name)
        res = m and m in self.installed_list
        #if res:
        #    print('installed:', kind,name)
        return res

    def install(self,kind,name):

        url, version = self.get_url(kind,name)
        if not url:
            print('Not found: %s %s'%(kind, name))
            self.error_count+=1
            return

        state='Installing: %s %s'%(kind,name)
        print(state)
        self.show_progress()

        fn = cuda_addonman.work_remote.get_plugin_zip(url)

        # handle SF.net HTML error file
        if is_file_html(fn):
            os.remove(fn)

        if not os.path.isfile(fn):
            self.error_count += 1
            return

        ok = file_open(fn, options='/silent')
        if ok:
            self.ok_count += 1
            cuda_addonman.work_local.do_save_version(url, fn, version)


    def open_menu(self):

        self.load_repo()
        if not self.packets:
            msg_status('Multi Installer: cannot download list')
            return

        langs = list(PLUGINS.keys())
        langs.sort()
        h=app_proc(PROC_GET_GUI_HEIGHT,'check')

        to_install = {}
        for i in CLASSES:
            to_install[i] = []

        RES_LIST = 2
        RES_NEXT = 4
        
        res = dlg_custom('CudaText Multi Installer', 360, 410, '\n'.join([
            '\1'.join(['type=label','pos=5,5,350,0','cap=Select language(s) needed for you.']),
            '\1'.join(['type=label','pos=5,25,350,0','cap=Next steps will suggest add-ons for them.']),
            '\1'.join(['type=checklistbox','pos=5,48,425,370','items='+
                '\t'.join(langs)
                ]),
            '\1'.join(['type=button','pos=5,375,85,0','cap=Cancel']),
            '\1'.join(['type=button','pos=275,375,355,0','cap=Next']),
            ]),
            get_dict=True
            )
        if res is None:
            return
        if res['clicked']!=RES_NEXT:
            return

        #if res[RES_GR_WEB]=='1':
        #    make_plugin_group('HTML.+', 'Web')
        #    make_plugin_group('CSS.+', 'Web')
        #    make_plugin_group('JS.+', 'Web')
        #    make_plugin_group('PHP.+', 'Web')
        #    
        #if res[RES_GR_PY]=='1':
        #    make_plugin_group('Python.+', 'Python')
        #    
        #if res[RES_GR_XML]=='1':
        #    make_plugin_group('XML.+', 'XML')

        res_list = res[RES_LIST].split(';')[1].split(',')
        res_list = list(map(str_to_bool,res_list))
        step_count = sum(map(int,res_list))+1
        step_index = 2

        for i,f in enumerate(res_list):
            if f:
                cl = 0
                line = 0
                UI = []
                UI_reg = [()]
                for curr_class in CLASSES:
                        pls = PLUGINS[langs[i]].setdefault(curr_class)
                        if pls:
                            if line in (COLUMN_LEN,COLUMN_LEN-1):
                                cl+=1
                                line = 0
                            UI.append('\1'.join([
{'fraction_non_alphanumeric': 0.14249483374037197, 'fraction_numerical': 0.01784707871501033, 'mean_word_length': 1.7988958990536277, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5549542', 'n_tokens_mistral': 3721, 'n_tokens_neox': 3486, 'n_words': 656}