<commit_before># Copyright (C) 2012-2015  <NAME>
# Copyright (C) 2016 Cuckoo Foundation.
# Copyright (C) 2016 <NAME>.
import logging
import re
import socket
import sys
import time
import types
import xml.parsers.expat
from adb import ADB
from adapter import Adapter
from viewclient_utils import _nd, _nh, _ns, obtainPxPy, obtainVwVh, obtainVxVy, Window

VIEW_SERVER_HOST = 'localhost'
VIEW_SERVER_PORT = 4939

# some constants for the attributes
VIEW_CLIENT_TOUCH_WORKAROUND_ENABLED = False
ID_PROPERTY = 'mID'
ID_PROPERTY_UI_AUTOMATOR = 'uniqueId'
TEXT_PROPERTY = 'text:mText'
TEXT_PROPERTY_API_10 = 'mText'
TEXT_PROPERTY_UI_AUTOMATOR = 'text'
WS = u"\xfe"  # the whitespace replacement char for TEXT_PROPERTY
TAG_PROPERTY = 'getTag()'
LEFT_PROPERTY = 'layout:mLeft'
LEFT_PROPERTY_API_8 = 'mLeft'
TOP_PROPERTY = 'layout:mTop'
TOP_PROPERTY_API_8 = 'mTop'
WIDTH_PROPERTY = 'layout:getWidth()'
WIDTH_PROPERTY_API_8 = 'getWidth()'
HEIGHT_PROPERTY = 'layout:getHeight()'
HEIGHT_PROPERTY_API_8 = 'getHeight()'
GET_VISIBILITY_PROPERTY = 'getVisibility()'
LAYOUT_TOP_MARGIN_PROPERTY = 'layout:layout_topMargin'
IS_FOCUSED_PROPERTY_UI_AUTOMATOR = 'focused'
IS_FOCUSED_PROPERTY = 'focus:isFocused()'
IS_ENABLED_PROPERTY_UI_AUTOMATOR = 'enabled'
IS_ENABLED_PROPERTY = 'isEnabled()'

# visibility
VISIBLE = 0x0
INVISIBLE = 0x4
GONE = 0x8

ID_RE = re.compile('id/([^/]*)(/(\d+))?')


class View:
    """
    View class
    """

    @classmethod
    def __copy(cls, view):
        """
        Copy constructor
        """
        return cls(view.map, view.version, view.windowId)

    def __init__(self, attributes, device, useUiAutomator, windowId=None):
        """
        Constructor
        @type attributes: map
        @param attributes: the map containing the (attribute, value) pairs
        @type device: Device
        @param device: the device containing this View
        @type useUiAutomator: boolean
        @param useUiAutomator: whether the view is dumped using UIAutomator
        """
        self.logger = logging.getLogger("ViewClient.View")
        self.attributes = attributes
        """ The map that contains the C{attr},C{value} pairs """
        self.adb = device.adb
        """ adb connection to device """
        # """ viewclient connected to device """
        self.children = []
        """ The children of this View """
        self.parent = None
        """ The parent of this View """
        self.currentFocus = None
        """ The current focus """
        self.windowId = windowId
        """ The window this view resides """
        version = device.get_sdk_version()
        self.version = version
        """ API version number """
        self.uiScrollable = None
        """ If this is a scrollable View this keeps the L{UiScrollable} object """
        self.target = False
        """ Is this a touch target zone """
        self.useUiAutomator = useUiAutomator
        """ Whether to use UIAutomator or ViewServer """

        self.idProperty = ID_PROPERTY
        self.textProperty = TEXT_PROPERTY
        self.tagProperty = TAG_PROPERTY
        self.leftProperty = LEFT_PROPERTY
        self.topProperty = TOP_PROPERTY
        self.widthProperty = WIDTH_PROPERTY
        self.heightProperty = HEIGHT_PROPERTY
        self.isFocusedProperty = IS_FOCUSED_PROPERTY
        self.isEnabledProperty = IS_ENABLED_PROPERTY

        if version >= 16 and self.useUiAutomator:
            self.idProperty = ID_PROPERTY_UI_AUTOMATOR
            self.textProperty = TEXT_PROPERTY_UI_AUTOMATOR
            self.isFocusedProperty = IS_FOCUSED_PROPERTY_UI_AUTOMATOR
            self.isEnabledProperty = IS_ENABLED_PROPERTY_UI_AUTOMATOR
        elif version == 10:
            self.textProperty = TEXT_PROPERTY_API_10
        elif 7 <= version < 10:
            self.textProperty = TEXT_PROPERTY_API_10
            self.leftProperty = LEFT_PROPERTY_API_8
            self.topProperty = TOP_PROPERTY_API_8
            self.widthProperty = WIDTH_PROPERTY_API_8
            self.heightProperty = HEIGHT_PROPERTY_API_8
        elif 0 < version < 7:
            self.textProperty = TEXT_PROPERTY_API_10

        try:
            if self.isScrollable():
                self.uiScrollable = UiScrollable(self)
        except AttributeError:
            pass

    def __getitem__(self, key):
        return self.attributes[key]

    def __getattr__(self, name):
        # NOTE:
        # I should try to see if 'name' is a defined method
        # but it seems that if I call locals() here an infinite loop is entered

        if self.attributes.has_key(name):
            r = self.attributes[name]
        elif self.attributes.has_key(name + '()'):
            # the method names are stored in the map with their trailing '()'
            r = self.attributes[name + '()']
        elif name.count("_") > 0:
            mangledList = self.allPossibleNamesWithColon(name)
            mangledName = self.intersection(mangledList, self.attributes.keys())
            if len(mangledName) > 0 and self.attributes.has_key(mangledName[0]):
                r = self.attributes[mangledName[0]]
            else:
                # Default behavior
                raise AttributeError, name
        elif name.startswith('is'):
            # try removing 'is' prefix
            suffix = name[2:].lower()
            if self.attributes.has_key(suffix):
                r = self.attributes[suffix]
            else:
                # Default behavior
                raise AttributeError, name
        elif name.startswith('get'):
            # try removing 'get' prefix
            suffix = name[3:].lower()
            if self.attributes.has_key(suffix):
                r = self.attributes[suffix]
            else:
                # Default behavior
                raise AttributeError, name
        elif name == 'getResourceId':
            if self.attributes.has_key('resource-id'):
                r = self.attributes['resource-id']
            else:
                # Default behavior
                raise AttributeError, name
        else:
            # Default behavior
            raise AttributeError, name
<commit_msg>Old style exceptions --> new style<commit_after># Copyright (C) 2012-2015  <NAME>
# Copyright (C) 2016 Cuckoo Foundation.
# Copyright (C) 2016 <NAME>.
import logging
import re
import socket
import sys
import time
import types
import xml.parsers.expat
from adb import ADB
from adapter import Adapter
from viewclient_utils import _nd, _nh, _ns, obtainPxPy, obtainVwVh, obtainVxVy, Window

VIEW_SERVER_HOST = 'localhost'
VIEW_SERVER_PORT = 4939

# some constants for the attributes
VIEW_CLIENT_TOUCH_WORKAROUND_ENABLED = False
ID_PROPERTY = 'mID'
ID_PROPERTY_UI_AUTOMATOR = 'uniqueId'
TEXT_PROPERTY = 'text:mText'
TEXT_PROPERTY_API_10 = 'mText'
TEXT_PROPERTY_UI_AUTOMATOR = 'text'
WS = u"\xfe"  # the whitespace replacement char for TEXT_PROPERTY
TAG_PROPERTY = 'getTag()'
LEFT_PROPERTY = 'layout:mLeft'
LEFT_PROPERTY_API_8 = 'mLeft'
TOP_PROPERTY = 'layout:mTop'
TOP_PROPERTY_API_8 = 'mTop'
WIDTH_PROPERTY = 'layout:getWidth()'
WIDTH_PROPERTY_API_8 = 'getWidth()'
HEIGHT_PROPERTY = 'layout:getHeight()'
HEIGHT_PROPERTY_API_8 = 'getHeight()'
GET_VISIBILITY_PROPERTY = 'getVisibility()'
LAYOUT_TOP_MARGIN_PROPERTY = 'layout:layout_topMargin'
IS_FOCUSED_PROPERTY_UI_AUTOMATOR = 'focused'
IS_FOCUSED_PROPERTY = 'focus:isFocused()'
IS_ENABLED_PROPERTY_UI_AUTOMATOR = 'enabled'
IS_ENABLED_PROPERTY = 'isEnabled()'

# visibility
VISIBLE = 0x0
INVISIBLE = 0x4
GONE = 0x8

ID_RE = re.compile('id/([^/]*)(/(\d+))?')


class View:
    """
    View class
    """

    @classmethod
    def __copy(cls, view):
        """
        Copy constructor
        """
        return cls(view.map, view.version, view.windowId)

    def __init__(self, attributes, device, useUiAutomator, windowId=None):
        """
        Constructor
        @type attributes: map
        @param attributes: the map containing the (attribute, value) pairs
        @type device: Device
        @param device: the device containing this View
        @type useUiAutomator: boolean
        @param useUiAutomator: whether the view is dumped using UIAutomator
        """
        self.logger = logging.getLogger("ViewClient.View")
        self.attributes = attributes
        """ The map that contains the C{attr},C{value} pairs """
        self.adb = device.adb
        """ adb connection to device """
        # """ viewclient connected to device """
        self.children = []
        """ The children of this View """
        self.parent = None
        """ The parent of this View """
        self.currentFocus = None
        """ The current focus """
        self.windowId = windowId
        """ The window this view resides """
        version = device.get_sdk_version()
        self.version = version
        """ API version number """
        self.uiScrollable = None
        """ If this is a scrollable View this keeps the L{UiScrollable} object """
        self.target = False
        """ Is this a touch target zone """
        self.useUiAutomator = useUiAutomator
        """ Whether to use UIAutomator or ViewServer """

        self.idProperty = ID_PROPERTY
        self.textProperty = TEXT_PROPERTY
        self.tagProperty = TAG_PROPERTY
        self.leftProperty = LEFT_PROPERTY
        self.topProperty = TOP_PROPERTY
        self.widthProperty = WIDTH_PROPERTY
        self.heightProperty = HEIGHT_PROPERTY
        self.isFocusedProperty = IS_FOCUSED_PROPERTY
        self.isEnabledProperty = IS_ENABLED_PROPERTY

        if version >= 16 and self.useUiAutomator:
            self.idProperty = ID_PROPERTY_UI_AUTOMATOR
            self.textProperty = TEXT_PROPERTY_UI_AUTOMATOR
            self.isFocusedProperty = IS_FOCUSED_PROPERTY_UI_AUTOMATOR
            self.isEnabledProperty = IS_ENABLED_PROPERTY_UI_AUTOMATOR
        elif version == 10:
            self.textProperty = TEXT_PROPERTY_API_10
        elif 7 <= version < 10:
            self.textProperty = TEXT_PROPERTY_API_10
            self.leftProperty = LEFT_PROPERTY_API_8
            self.topProperty = TOP_PROPERTY_API_8
            self.widthProperty = WIDTH_PROPERTY_API_8
            self.heightProperty = HEIGHT_PROPERTY_API_8
        elif 0 < version < 7:
            self.textProperty = TEXT_PROPERTY_API_10

        try:
            if self.isScrollable():
                self.uiScrollable = UiScrollable(self)
        except AttributeError:
            pass

    def __getitem__(self, key):
        return self.attributes[key]

    def __getattr__(self, name):
        # NOTE:
        # I should try to see if 'name' is a defined method
        # but it seems that if I call locals() here an infinite loop is entered

        if self.attributes.has_key(name):
            r = self.attributes[name]
        elif self.attributes.has_key(name + '()'):
            # the method names are stored in the map with their trailing '()'
            r = self.attributes[name + '()']
        elif name.count("_") > 0:
            mangledList = self.allPossibleNamesWithColon(name)
            mangledName = self.intersection(mangledList, self.attributes.keys())
            if len(mangledName) > 0 and self.attributes.has_key(mangledName[0]):
                r = self.attributes[mangledName[0]]
            else:
                # Default behavior
                raise AttributeError(name)
        elif name.startswith('is'):
            # try removing 'is' prefix
            suffix = name[2:].lower()
            if self.attributes.has_key(suffix):
                r = self.attributes[suffix]
            else:
                # Default behavior
                raise AttributeError(name)
        elif name.startswith('get'):
            # try removing 'get' prefix
            suffix = name[3:].lower()
            if self.attributes.has_key(suffix):
                r = self.attributes[suffix]
            else:
                # Default behavior
                raise AttributeError(name)
        elif name == 'getResourceId':
            if self.attributes.has_key('resource-id'):
                r = self.attributes['resource-id']
            else:
                # Default behavior
                raise AttributeError(name)
        else:
            # Default behavior
            raise AttributeError(name)

{'fraction_non_alphanumeric': 0.08973210650598486, 'fraction_numerical': 0.009282631707515674, 'mean_word_length': 2.422123154081917, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3196905', 'n_tokens_mistral': 3563, 'n_tokens_neox': 3516, 'n_words': 887}