<commit_before># Copyright (C) 2017 <NAME>
#
# This file is part of Pyrtist.
#   Pyrtist is free software: you can redistribute it and/or modify it
#   under the terms of the GNU Lesser General Public License as published
#   by the Free Software Foundation, either version 2.1 of the License, or
#   (at your option) any later version.
#
#   Pyrtist is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with Pyrtist.  If not, see <http://www.gnu.org/licenses/>.

'''Functionality to communicate with the Pyrtist GUI from scripts.'''

__all__ = ('gui',)

from .cairo_cmd_exec import CairoCmdExecutor
from .core_types import Point, Tri, View

import numpy


class GUIPointAttrs(object):
    '''Attributes for points created and managed by the Pyrtist GUI.'''

    def __init__(self, name, old_value=None):
        self.name = name
        self.old_value = old_value


class GUIGate(object):
    '''Object which allows communicating with the Pyrtist GUI.'''

    def __init__(self):
        self._gui_tx_pipe = None
        self._full_view = None
        self._size = None
        self._zoom_window = None
        self._img_filename = None
        self._points = {'old': {}, 'new': {}}
        self._tris = {}

    def connect(self, gui_tx_pipe=None, startup_cmds=None):
        '''Method invoked by the Pyrtist GUI to allow communicating back
        with it.
        '''
        assert self._gui_tx_pipe is None, 'GUIGate object already connected'
        self._gui_tx_pipe = gui_tx_pipe
        self._parse_cmds(startup_cmds)

    def _parse_cmds(self, cmds):
        if cmds is None:
            return
        for cmd in cmds:
            if len(cmd) < 1:
                continue
            cmd_name = '_rx_cmd_{}'.format(cmd[0])
            method = getattr(self, cmd_name, None)
            if method is not None:
                method(*cmd[1:])

    def _rx_cmd_point(self, tag, name, x, y):
        ps = self._points[tag]
        ps[name] = Point(x, y)

    def _rx_cmd_tri(self, tag, name, x, y, lhs_name, rhs_name):
        ps = self._points[tag]
        ps[name] = Tri(ps.get(lhs_name), Point(x, y), ps.get(rhs_name))
        self._tris[name] = (lhs_name, name, rhs_name)

    def _rx_cmd_full_view(self, size_x, size_y):
        self._full_view = True
        self._size = (size_x, size_y)

    def _rx_cmd_zoomed_view(self, size_x, size_y, *view_args):
        self._full_view = False
        self._size = (size_x, size_y)<commit_msg>Add gui.is_connected method<commit_after># Copyright (C) 2017 <NAME>
#
# This file is part of Pyrtist.
#   Pyrtist is free software: you can redistribute it and/or modify it
#   under the terms of the GNU Lesser General Public License as published
#   by the Free Software Foundation, either version 2.1 of the License, or
#   (at your option) any later version.
#
#   Pyrtist is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with Pyrtist.  If not, see <http://www.gnu.org/licenses/>.

'''Functionality to communicate with the Pyrtist GUI from scripts.'''

__all__ = ('gui',)

from .cairo_cmd_exec import CairoCmdExecutor
from .core_types import Point, Tri, View

import numpy


class GUIPointAttrs(object):
    '''Attributes for points created and managed by the Pyrtist GUI.'''

    def __init__(self, name, old_value=None):
        self.name = name
        self.old_value = old_value


class GUIGate(object):
    '''Object which allows communicating with the Pyrtist GUI.'''

    def __init__(self):
        self._gui_tx_pipe = None
        self._full_view = None
        self._size = None
        self._zoom_window = None
        self._img_filename = None
        self._points = {'old': {}, 'new': {}}
        self._tris = {}

    def connect(self, gui_tx_pipe=None, startup_cmds=None):
        '''Method invoked by the Pyrtist GUI to allow communicating back
        with it.
        '''
        assert self._gui_tx_pipe is None, 'GUIGate object already connected'
        self._gui_tx_pipe = gui_tx_pipe
        self._parse_cmds(startup_cmds)

    def is_connected(self):
        '''Return a boolean indicating whether the Pyrtist UI is connected.

        This is true when running the script in the Pyrtist UI and is false when
        running the script from the command line.
        '''
        return self._gui_tx_pipe is not None

    def _parse_cmds(self, cmds):
        if cmds is None:
            return
        for cmd in cmds:
            if len(cmd) < 1:
                continue
            cmd_name = '_rx_cmd_{}'.format(cmd[0])
            method = getattr(self, cmd_name, None)
            if method is not None:
                method(*cmd[1:])

    def _rx_cmd_point(self, tag, name, x, y):
        ps = self._points[tag]
        ps[name] = Point(x, y)

    def _rx_cmd_tri(self, tag, name, x, y, lhs_name, rhs_name):
        ps = self._points[tag]
        ps[name] = Tri(ps.get(lhs_name), Point(x, y), ps.get(rhs_name))
        self._tris[name] = (lhs_name, name, rhs_name)

    def _rx_cmd_full_view(self, size_x, size_y):
        self._full_view = True
        self._size = (size_x, size_y)

    def _rx_cmd_zoomed_view(self, size_x, size_y, *view_args):
        self._full_view = False
        self._size = (size_x, size_y)
{'fraction_non_alphanumeric': 0.09291420632163946, 'fraction_numerical': 0.003126085446335533, 'mean_word_length': 2.647245091830272, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7182118', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1770, 'n_words': 606}