<commit_before>#coding=utf-8

import os

import tkinter as tk
from tkinter import ttk
import tkFileDialog as tkfd
import tkMessageBox as tkmb

from .frames.match import *
from .frames.network import *
from .frames.team import *
from .frames.translations import *
from .frames.visual import *

from ..data import PlayoffData
from ..db import PlayoffDB

class PlayoffTab(ttk.Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.frame = ttk.Frame(self)
        self.frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.renderContent(self.frame)

    @property
    def title(self):
        pass

    def renderContent(self, container):
        pass

class MainSettingsTab(PlayoffTab):
    @property
    def title(self):
        return 'Główne ustawienia'

    def _chooseOutputPath(self):
        currentPath = self.outputPath.get()
        filename = tkfd.asksaveasfilename(
            initialdir=os.path.dirname(currentPath) if currentPath else '.',
            title='Wybierz plik wyjściowy',
            filetypes=(('HTML files', '*.html'),))
        if filename:
            if not filename.lower().endswith('.html'):
                filename = filename + '.html'
            self.outputPath.set(filename)

    def _updateRefreshFields(self):
        self.refreshInterval.configure(
            state=tk.NORMAL if self.refresh.get() else tk.DISABLED)

    def renderContent(self, container):
        (ttk.Label(container, text='Plik wynikowy:')).grid(
            row=0, column=0, sticky=tk.E, pady=2)
        outputPath = tk.Frame(container)
        outputPath.grid(row=0, column=1, sticky=tk.E+tk.W, pady=2)
        self.outputPath = tk.StringVar()
        (ttk.Entry(outputPath, width=60, textvariable=self.outputPath)).grid(
            row=0, column=0, sticky=tk.W+tk.E)
        (ttk.Button(
            outputPath,
            text='wybierz...', command=self._chooseOutputPath)).grid(
                row=0, column=1)
        outputPath.columnconfigure(0, weight=1)

        (ttk.Separator(container, orient=tk.HORIZONTAL)).grid(
            row=1, column=0, columnspan=2, sticky=tk.E+tk.W, pady=2)

        pageSettings = ttk.LabelFrame(
            container, text='Ustawienia strony')
        pageSettings.grid(
            row=2, column=0, columnspan=2, sticky=tk.W+tk.E+tk.N+tk.S, pady=5)

        (ttk.Label(pageSettings, text='Tytuł:')).grid(
            row=0, column=0, sticky=tk.E, pady=2)
        self.pageTitle = tk.StringVar()
        (tk.Entry(pageSettings, textvariable=self.pageTitle)).grid(
            row=0, column=1, sticky=tk.W+tk.E, pady=2)
        (ttk.Label(pageSettings, text='Logoh:')).grid(
            row=1, column=0, sticky=tk.E+tk.N, pady=2)
        self.pageLogoh = tk.Text(pageSettings, width=45, height=10)
        self.pageLogoh.grid(
            row=1, column=1,
            sticky=tk.W+tk.N+tk.E+tk.S, pady=2)

        (ttk.Label(pageSettings, text='Odświeżaj:')).grid(
            row=2, column=0, sticky=tk.E, pady=2)
        refreshPanel = tk.Frame(pageSettings)
        refreshPanel.grid(row=2, column=1, sticky=tk.W+tk.E, pady=2)
        self.refresh = tk.IntVar()
        (ttk.Checkbutton(
            refreshPanel,
            command=self._updateRefreshFields, variable=self.refresh)).grid(
                row=0, column=0)
        (ttk.Label(refreshPanel, text='co:')).grid(row=0, column=1)
        self.refreshInterval = tk.Spinbox(
            refreshPanel, from_=30, to=3600, width=5, justify=tk.RIGHT)
        self.refreshInterval.grid(row=0, column=2)
        (ttk.Label(refreshPanel, text='sekund')).grid(row=0, column=3)
        self._updateRefreshFields()

        container.columnconfigure(1, weight=1)
        container.rowconfigure(4, weight=1)

class TeamsTab(PlayoffTab):
    @property
    def title(self):
        return 'Uczestnicy'

    def renderContent(self, container):
        leftFrame = tk.Frame(container)
        leftFrame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.settingsFrame = TeamSettingsFrame(
            leftFrame, vertical=True, padx=5, pady=5)
        self.settingsFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        (ttk.Separator(
            leftFrame, orient=tk.HORIZONTAL)).pack(
                side=tk.TOP, fill=tk.X)
        self.aliasFrame = TeamAliasFrame(leftFrame, vertical=True)
        self.aliasFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        self.previewFrame = TeamPreviewFrame(container, vertical=True)
        self.previewFrame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

        self._teamList = []
        self._teamListFetcher = None

        self.winfo_toplevel().bind(
            '<<TeamSettingsChanged>>', self.onTeamSettingsChange, add='+')

    def onTeamSettingsChange(self, event):
        if self._teamListFetcher is not None:
            self.after_cancel(self._teamListFetcher)
        self._teamListFetcher = self.after(500, self._fetchTeamList)

    def _fetchTeamList(self):
        config = self.collectConfig()
        dbConfig = self.winfo_toplevel().getDbConfig()
        if dbConfig is not None:
            config['database'] = dbConfig
        data = PlayoffData()
        db = None
        try:
            db = PlayoffDB(dbConfig)
        except Exception:
            pass
        self._teamList = data.fetch_team_list(config['teams'], db)
        self.winfo_toplevel().event_generate(
            '<<TeamListChanged>>', when='tail')

    def getTeams(self):
        return self._teamList

    def collectConfig(self):
        config = {
            'teams': self.settingsFrame.getConfig(),
            'team_aliases': self.aliasFrame.getConfig()
        }
        tieConfig = self.previewFrame.getTieConfig()
        if tieConfig is not None and isinstance(config['teams'], dict):
            config['teams']['ties'] = tieConfig
        return config

class MatchesTab(PlayoffTab):
    @property
    def title(self):
        return 'Mecze'

    def renderContent(self, container):
        self.phase = MatchPhaseFrame(container, vertical=True)
        self.phase.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    def getMatches(self):
        return [w for w in self.phase.matches.widgets
                if isinstance(w, MatchSettingsFrame)]

class SwissesTab(PlayoffTab):
    @property<commit_msg>Multi-phase GUI<commit_after>#coding=utf-8

import os

import tkinter as tk
from tkinter import ttk
import tkFileDialog as tkfd
import tkMessageBox as tkmb

from .frames.match import *
from .frames.network import *
from .frames.team import *
from .frames.translations import *
from .frames.visual import *

from ..data import PlayoffData
from ..db import PlayoffDB

class PlayoffTab(ttk.Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.frame = ttk.Frame(self)
        self.frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.renderContent(self.frame)

    @property
    def title(self):
        pass

    def renderContent(self, container):
        pass

class MainSettingsTab(PlayoffTab):
    @property
    def title(self):
        return 'Główne ustawienia'

    def _chooseOutputPath(self):
        currentPath = self.outputPath.get()
        filename = tkfd.asksaveasfilename(
            initialdir=os.path.dirname(currentPath) if currentPath else '.',
            title='Wybierz plik wyjściowy',
            filetypes=(('HTML files', '*.html'),))
        if filename:
            if not filename.lower().endswith('.html'):
                filename = filename + '.html'
            self.outputPath.set(filename)

    def _updateRefreshFields(self):
        self.refreshInterval.configure(
            state=tk.NORMAL if self.refresh.get() else tk.DISABLED)

    def renderContent(self, container):
        (ttk.Label(container, text='Plik wynikowy:')).grid(
            row=0, column=0, sticky=tk.E, pady=2)
        outputPath = tk.Frame(container)
        outputPath.grid(row=0, column=1, sticky=tk.E+tk.W, pady=2)
        self.outputPath = tk.StringVar()
        (ttk.Entry(outputPath, width=60, textvariable=self.outputPath)).grid(
            row=0, column=0, sticky=tk.W+tk.E)
        (ttk.Button(
            outputPath,
            text='wybierz...', command=self._chooseOutputPath)).grid(
                row=0, column=1)
        outputPath.columnconfigure(0, weight=1)

        (ttk.Separator(container, orient=tk.HORIZONTAL)).grid(
            row=1, column=0, columnspan=2, sticky=tk.E+tk.W, pady=2)

        pageSettings = ttk.LabelFrame(
            container, text='Ustawienia strony')
        pageSettings.grid(
            row=2, column=0, columnspan=2, sticky=tk.W+tk.E+tk.N+tk.S, pady=5)

        (ttk.Label(pageSettings, text='Tytuł:')).grid(
            row=0, column=0, sticky=tk.E, pady=2)
        self.pageTitle = tk.StringVar()
        (tk.Entry(pageSettings, textvariable=self.pageTitle)).grid(
            row=0, column=1, sticky=tk.W+tk.E, pady=2)
        (ttk.Label(pageSettings, text='Logoh:')).grid(
            row=1, column=0, sticky=tk.E+tk.N, pady=2)
        self.pageLogoh = tk.Text(pageSettings, width=45, height=10)
        self.pageLogoh.grid(
            row=1, column=1,
            sticky=tk.W+tk.N+tk.E+tk.S, pady=2)

        (ttk.Label(pageSettings, text='Odświeżaj:')).grid(
            row=2, column=0, sticky=tk.E, pady=2)
        refreshPanel = tk.Frame(pageSettings)
        refreshPanel.grid(row=2, column=1, sticky=tk.W+tk.E, pady=2)
        self.refresh = tk.IntVar()
        (ttk.Checkbutton(
            refreshPanel,
            command=self._updateRefreshFields, variable=self.refresh)).grid(
                row=0, column=0)
        (ttk.Label(refreshPanel, text='co:')).grid(row=0, column=1)
        self.refreshInterval = tk.Spinbox(
            refreshPanel, from_=30, to=3600, width=5, justify=tk.RIGHT)
        self.refreshInterval.grid(row=0, column=2)
        (ttk.Label(refreshPanel, text='sekund')).grid(row=0, column=3)
        self._updateRefreshFields()

        container.columnconfigure(1, weight=1)
        container.rowconfigure(4, weight=1)

class TeamsTab(PlayoffTab):
    @property
    def title(self):
        return 'Uczestnicy'

    def renderContent(self, container):
        leftFrame = tk.Frame(container)
        leftFrame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.settingsFrame = TeamSettingsFrame(
            leftFrame, vertical=True, padx=5, pady=5)
        self.settingsFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        (ttk.Separator(
            leftFrame, orient=tk.HORIZONTAL)).pack(
                side=tk.TOP, fill=tk.X)
        self.aliasFrame = TeamAliasFrame(leftFrame, vertical=True)
        self.aliasFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        self.previewFrame = TeamPreviewFrame(container, vertical=True)
        self.previewFrame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

        self._teamList = []
        self._teamListFetcher = None

        self.winfo_toplevel().bind(
            '<<TeamSettingsChanged>>', self.onTeamSettingsChange, add='+')

    def onTeamSettingsChange(self, event):
        if self._teamListFetcher is not None:
            self.after_cancel(self._teamListFetcher)
        self._teamListFetcher = self.after(500, self._fetchTeamList)

    def _fetchTeamList(self):
        config = self.collectConfig()
        dbConfig = self.winfo_toplevel().getDbConfig()
        if dbConfig is not None:
            config['database'] = dbConfig
        data = PlayoffData()
        db = None
        try:
            db = PlayoffDB(dbConfig)
        except Exception:
            pass
        self._teamList = data.fetch_team_list(config['teams'], db)
        self.winfo_toplevel().event_generate(
            '<<TeamListChanged>>', when='tail')

    def getTeams(self):
        return self._teamList

    def collectConfig(self):
        config = {
            'teams': self.settingsFrame.getConfig(),
            'team_aliases': self.aliasFrame.getConfig()
        }
        tieConfig = self.previewFrame.getTieConfig()
        if tieConfig is not None and isinstance(config['teams'], dict):
            config['teams']['ties'] = tieConfig
        return config

class MatchesTab(PlayoffTab):
    @property
    def title(self):
        return 'Mecze'

    def addPhase(self):
        phase = MatchPhaseFrame(
            self.phaseFrame, vertical=True, padx=10, pady=10)
        newPhase = max(self.phases.keys()) + 1 if len(self.phases) else 1
        self.phaseFrame.add(phase, text='Faza #%d' % (newPhase))
        self.phases[newPhase] = phase

    def removePhase(self):
        selected = self.phaseFrame.select()
        if selected:
            self.phaseFrame.forget(selected)
            key_to_delete = None
            for key, tab in self.phases.iteritems():
                if str(selected) == str(tab):
                    key_to_delete = key
                    break
            if key_to_delete:
                self.phases.pop(key_to_delete)

    def renderContent(self, container):
        container.columnconfigure(1, weight=1)
        container.rowconfigure(2, weight=1)
        (ttk.Label(container, text='Fazy rozgrywek:')).grid(
            row=0, column=0, columnspan=2, sticky=tk.W)
        (ttk.Button(
            container, text='+', command=self.addPhase, width=5)).grid(
                row=1, column=0, sticky=tk.W)
        (ttk.Button(
            container, text='-', command=self.removePhase, width=5)).grid(
                row=1, column=1, sticky=tk.W)
        self.phases = {}
        self.phaseFrame = ttk.Notebook(container)
        self.phaseFrame.grid(
            row=2, column=0, columnspan=2, sticky=tk.W+tk.E+tk.N+tk.S)

    def getMatches(self):
        matches = []
        for phase in self.phases.values():
            matches +=  [w for w in phase.matches.widgets
                         if isinstance(w, MatchSettingsFrame)]
        return matches

class SwissesTab(PlayoffTab):
    @property
{'fraction_non_alphanumeric': 0.1267314008282165, 'fraction_numerical': 0.011709267456804227, 'mean_word_length': 2.497378277153558, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1743841', 'n_tokens_mistral': 4550, 'n_tokens_neox': 4470, 'n_words': 816}