<commit_before># !/usr/bin/env python
# -*- coding: utf-8 -*-

#    Copyright (c) 2014 <NAME>.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

__author__ = '<EMAIL>'

import sys
import re

from Quartz import CoreGraphics as cg

# TODO: change to relative import.
from uisoup.interfaces.i_soup import ISoup
from uisoup.utils.mac_utils import MacUtils
from uisoup.mac_soup.element import MacElement
from uisoup import TooSaltyUISoupException


class MacSoup(ISoup):

    mouse = None  # TODO: add implementation.
    keyboard = None  # TODO: add implementation.
    _default_sys_encoding = sys.stdout.encoding

    def get_object_by_coordinates(self, x, y):

        window_handle = \
            MacUtils.ApplescriptExecutor.get_frontmost_window_name()
        window = self.get_window(window_handle)

        # Sorting by layer from big to small.
        sorted_objects = sorted(window.findall(),
                                lambda x, y: y._layer_num - x._layer_num)

        result = None
        cur_x, cur_y = self.mouse.get_position()
        for element in sorted_objects:
            x, y, w, h = element.acc_location()
            if x < cur_x < x + w and y < cur_y < y + h:
                result = element
                break

        return result

    def is_window_exists(self, obj_handle):
        try:
            self.get_window(obj_handle)
            return True
        except TooSaltyUISoupException:
            return False

    def get_window(self, obj_handle=None):
        filters = cg.kCGWindowListOptionOnScreenOnly | \
            cg.kCGWindowListExcludeDesktopElements * bool(obj_handle)

        obj_name = obj_handle if obj_handle else u'DesktopWindow Server'
        obj_name = \
            obj_name if type(obj_name) == unicode else obj_name.decode('utf-8')

        regex = MacUtils.convert_wildcard_to_regex(obj_name)

        win_list = cg.CGWindowListCopyWindowInfo(filters, cg.kCGNullWindowID)
<commit_msg>Updated condition for get_object_by_coordinates method.<commit_after># !/usr/bin/env python
# -*- coding: utf-8 -*-

#    Copyright (c) 2014 <NAME>.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

__author__ = '<EMAIL>'

import sys
import re

from Quartz import CoreGraphics as cg

# TODO: change to relative import.
from uisoup.interfaces.i_soup import ISoup
from uisoup.utils.mac_utils import MacUtils
from uisoup.mac_soup.element import MacElement
from uisoup import TooSaltyUISoupException


class MacSoup(ISoup):

    mouse = None  # TODO: add implementation.
    keyboard = None  # TODO: add implementation.
    _default_sys_encoding = sys.stdout.encoding

    def get_object_by_coordinates(self, x, y):
        window_handle = \
            MacUtils.ApplescriptExecutor.get_frontmost_window_name()
        window = self.get_window(window_handle)

        # Sorting by layer from big to small.
        sorted_objects = sorted(window.findall(),
                                lambda x, y: y._layer_num - x._layer_num)

        result = None
        cur_x, cur_y = self.mouse.get_position()
        for element in sorted_objects:
            x, y, w, h = element.acc_location()
            if x <= cur_x < x + w and y <= cur_y < y + h:
                result = element
                break

        return result

    def is_window_exists(self, obj_handle):
        try:
            self.get_window(obj_handle)
            return True
        except TooSaltyUISoupException:
            return False

    def get_window(self, obj_handle=None):
        filters = cg.kCGWindowListOptionOnScreenOnly | \
            cg.kCGWindowListExcludeDesktopElements * bool(obj_handle)

        obj_name = obj_handle if obj_handle else u'DesktopWindow Server'
        obj_name = \
            obj_name if type(obj_name) == unicode else obj_name.decode('utf-8')

        regex = MacUtils.convert_wildcard_to_regex(obj_name)

        win_list = cg.CGWindowListCopyWindowInfo(filters, cg.kCGNullWindowID)

{'fraction_non_alphanumeric': 0.07504502701620973, 'fraction_numerical': 0.0040024014408645186, 'mean_word_length': 2.640203932993445, 'pattern_counts': {'":': 0, '<': 15, '<?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': '746839', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1437, 'n_words': 448}