<commit_before>#!/usr/bin/env python3

import re
import signal
import subprocess
import sys
import tempfile
import threading
import time
import os
import random
from urllib.parse import urlparse
import psutil

# asyncio was introduced in version 3.5
if sys.version_info >= (3, 5):
    try:
        import asyncio_helper
        _ASYNCIO_AVAILABLE = True
    except ImportError:
        # This is to deal with python versions below 3.5
        _ASYNCIO_AVAILABLE = False
else:
    _ASYNCIO_AVAILABLE = False

try:
    # This fails when the code is executed directly and not as a part of python package installation,
    # I definitely need a better way to handle this.
    from adbe.adb_helper import (get_adb_shell_property, execute_adb_command2,
            execute_adb_shell_command, execute_adb_shell_command2,
            execute_file_related_adb_shell_command, get_package,
            root_required_to_access_file, get_device_android_api_version, toggle_screen)
    from adbe.output_helper import print_message, print_error, print_error_and_exit, print_verbose
except ImportError:
    # This works when the code is executed directly.
    from adb_helper import (get_adb_shell_property, execute_adb_command2,
            execute_adb_shell_command, execute_adb_shell_command2,
            execute_file_related_adb_shell_command, get_package,
            root_required_to_access_file, get_device_android_api_version, toggle_screen)
    from output_helper import print_message, print_error, print_error_and_exit, print_verbose


_KEYCODE_BACK = 4
_MIN_API_FOR_RUNTIME_PERMISSIONS = 23

# Value to be return as 'on' to the user
_USER_PRINT_VALUE_ON = 'on'
# Value to be return as 'partially on' to the user
_USER_PRINT_VALUE_PARTIALLY_ON = 'partially on'
# Value to be return as 'off' to the user
_USER_PRINT_VALUE_OFF = 'off'
# Value to be return as 'unknown' to the user
_USER_PRINT_VALUE_UNKNOWN = 'unknown'

SCREEN_ON = 1
SCREEN_OFF = 2
SCREEN_TOGGLE = 3


def _ensure_package_exists(package_name):
    """
    Don't call this directly. Instead consider decorating your method with
    @ensure_package_exists or @ensure_package_exists2
    :return: True if package_name package is installed on the device
    """
    if not _package_exists(package_name):
        print_error_and_exit("Package %s does not exist" % package_name)


# A decorator to ensure package exists
def ensure_package_exists(func):
    def func_wrapper(package_name):
        _ensure_package_exists(package_name)
        return func(package_name)
    return func_wrapper


# A decorator to ensure package exists with one more argument
def ensure_package_exists2(func):
    def func_wrapper(package_name, arg2):
        _ensure_package_exists(package_name)
        return func(package_name, arg2)
    return func_wrapper


# A decorator to ensure package exists with two more arguments
def ensure_package_exists3(func):
    def func_wrapper(package_name, arg2, arg3):
        _ensure_package_exists(package_name)
        return func(package_name, arg2, arg3)
    return func_wrapper


def _package_exists(package_name):
    cmd = 'pm path %s' % package_name
    return_code, response, _ = execute_adb_shell_command2(cmd)
    return return_code == 0 and response is not None and len(response.strip()) != 0


# Source:
# https://github.com/dhelleberg/android-scripts/blob/master/src/devtools.groovy
def handle_gfx(value):
    if value == 'on':
        cmd = 'setprop debug.hwui.profile visual_bars'
    elif value == 'off':
        cmd = 'setprop debug.hwui.profile false'
    elif value == 'lines':
        cmd = 'setprop debug.hwui.profile visual_lines'
    else:
        print_error_and_exit('Unexpected value for gfx %s' % value)
        return

    execute_adb_shell_command_and_poke_activity_service(cmd)


# Source: https://github.com/dhelleberg/android-scripts/blob/master/src/devtools.groovy
# https://plus.google.com/+AladinQ/posts/dpidzto1b8B
def handle_overdraw(value):
    version = get_device_android_api_version()

    if version < 19:
        if value is 'on':
            cmd = 'setprop debug.hwui.show_overdraw true'
        elif value is 'off':
            cmd = 'setprop debug.hwui.show_overdraw false'
        elif value is 'deut':
            print_error_and_exit(
                'deut mode is available only on API 19 and above, your Android API version is %d' % version)
            return
        else:
            print_error_and_exit('Unexpected value for overdraw %s' % value)
            return
    else:
        if value is 'on':
            cmd = 'setprop debug.hwui.overdraw show'
        elif value is 'off':
            cmd = 'setprop debug.hwui.overdraw false'
        elif value is 'deut':
            cmd = 'setprop debug.hwui.overdraw show_deuteranomaly'
        else:
            print_error_and_exit('Unexpected value for overdraw %s' % value)
            return

    execute_adb_shell_command_and_poke_activity_service(cmd)


# Perform screen rotation. Accepts four direction types - left, right, portrait, and landscape.
# Source:
# https://stackoverflow.com/questions/25864385/changing-android-device-orientation-with-adb
def handle_rotate(direction):
    disable_acceleration = 'put system accelerometer_rotation 0'
    execute_adb_shell_settings_command(disable_acceleration)

    if direction is 'portrait':
        new_direction = 0
    elif direction is 'landscape':
        new_direction = 1
    elif direction is 'left':
        current_direction = get_current_rotation_direction()
        print_verbose("Current direction: %s" % current_direction)
        if current_direction is None:
            return
        new_direction = (current_direction + 1) % 4
    elif direction is 'right':
        current_direction = get_current_rotation_direction()
        print_verbose("Current direction: %s" % current_direction)
        if current_direction is None:
            return
        new_direction = (current_direction - 1) % 4
    else:
        print_error_and_exit('Unexpected direction %s' % direction)
        return

    cmd = 'put system user_rotation %s' % new_direction
    execute_adb_shell_settings_command(cmd)


def get_current_rotation_direction():
    cmd = 'get system user_rotation'
    direction = execute_adb_shell_settings_command(cmd)
    print_verbose("Return value is %s" % direction)
    if not direction or direction == 'null':
        return 0  # default direction is 0, vertical straight
    try:
        return int(direction)<commit_msg>Use == instead of 'is' with literal<commit_after>#!/usr/bin/env python3

import re
import signal
import subprocess
import sys
import tempfile
import threading
import time
import os
import random
from urllib.parse import urlparse
import psutil

# asyncio was introduced in version 3.5
if sys.version_info >= (3, 5):
    try:
        import asyncio_helper
        _ASYNCIO_AVAILABLE = True
    except ImportError:
        # This is to deal with python versions below 3.5
        _ASYNCIO_AVAILABLE = False
else:
    _ASYNCIO_AVAILABLE = False

try:
    # This fails when the code is executed directly and not as a part of python package installation,
    # I definitely need a better way to handle this.
    from adbe.adb_helper import (get_adb_shell_property, execute_adb_command2,
            execute_adb_shell_command, execute_adb_shell_command2,
            execute_file_related_adb_shell_command, get_package,
            root_required_to_access_file, get_device_android_api_version, toggle_screen)
    from adbe.output_helper import print_message, print_error, print_error_and_exit, print_verbose
except ImportError:
    # This works when the code is executed directly.
    from adb_helper import (get_adb_shell_property, execute_adb_command2,
            execute_adb_shell_command, execute_adb_shell_command2,
            execute_file_related_adb_shell_command, get_package,
            root_required_to_access_file, get_device_android_api_version, toggle_screen)
    from output_helper import print_message, print_error, print_error_and_exit, print_verbose


_KEYCODE_BACK = 4
_MIN_API_FOR_RUNTIME_PERMISSIONS = 23

# Value to be return as 'on' to the user
_USER_PRINT_VALUE_ON = 'on'
# Value to be return as 'partially on' to the user
_USER_PRINT_VALUE_PARTIALLY_ON = 'partially on'
# Value to be return as 'off' to the user
_USER_PRINT_VALUE_OFF = 'off'
# Value to be return as 'unknown' to the user
_USER_PRINT_VALUE_UNKNOWN = 'unknown'

SCREEN_ON = 1
SCREEN_OFF = 2
SCREEN_TOGGLE = 3


def _ensure_package_exists(package_name):
    """
    Don't call this directly. Instead consider decorating your method with
    @ensure_package_exists or @ensure_package_exists2
    :return: True if package_name package is installed on the device
    """
    if not _package_exists(package_name):
        print_error_and_exit("Package %s does not exist" % package_name)


# A decorator to ensure package exists
def ensure_package_exists(func):
    def func_wrapper(package_name):
        _ensure_package_exists(package_name)
        return func(package_name)
    return func_wrapper


# A decorator to ensure package exists with one more argument
def ensure_package_exists2(func):
    def func_wrapper(package_name, arg2):
        _ensure_package_exists(package_name)
        return func(package_name, arg2)
    return func_wrapper


# A decorator to ensure package exists with two more arguments
def ensure_package_exists3(func):
    def func_wrapper(package_name, arg2, arg3):
        _ensure_package_exists(package_name)
        return func(package_name, arg2, arg3)
    return func_wrapper


def _package_exists(package_name):
    cmd = 'pm path %s' % package_name
    return_code, response, _ = execute_adb_shell_command2(cmd)
    return return_code == 0 and response is not None and len(response.strip()) != 0


# Source:
# https://github.com/dhelleberg/android-scripts/blob/master/src/devtools.groovy
def handle_gfx(value):
    if value == 'on':
        cmd = 'setprop debug.hwui.profile visual_bars'
    elif value == 'off':
        cmd = 'setprop debug.hwui.profile false'
    elif value == 'lines':
        cmd = 'setprop debug.hwui.profile visual_lines'
    else:
        print_error_and_exit('Unexpected value for gfx %s' % value)
        return

    execute_adb_shell_command_and_poke_activity_service(cmd)


# Source: https://github.com/dhelleberg/android-scripts/blob/master/src/devtools.groovy
# https://plus.google.com/+AladinQ/posts/dpidzto1b8B
def handle_overdraw(value):
    version = get_device_android_api_version()

    if version < 19:
        if value == 'on':
            cmd = 'setprop debug.hwui.show_overdraw true'
        elif value == 'off':
            cmd = 'setprop debug.hwui.show_overdraw false'
        elif value == 'deut':
            print_error_and_exit(
                'deut mode is available only on API 19 and above, your Android API version is %d' % version)
            return
        else:
            print_error_and_exit('Unexpected value for overdraw %s' % value)
            return
    else:
        if value == 'on':
            cmd = 'setprop debug.hwui.overdraw show'
        elif value == 'off':
            cmd = 'setprop debug.hwui.overdraw false'
        elif value == 'deut':
            cmd = 'setprop debug.hwui.overdraw show_deuteranomaly'
        else:
            print_error_and_exit('Unexpected value for overdraw %s' % value)
            return

    execute_adb_shell_command_and_poke_activity_service(cmd)


# Perform screen rotation. Accepts four direction types - left, right, portrait, and landscape.
# Source:
# https://stackoverflow.com/questions/25864385/changing-android-device-orientation-with-adb
def handle_rotate(direction):
    disable_acceleration = 'put system accelerometer_rotation 0'
    execute_adb_shell_settings_command(disable_acceleration)

    if direction == 'portrait':
        new_direction = 0
    elif direction == 'landscape':
        new_direction = 1
    elif direction == 'left':
        current_direction = get_current_rotation_direction()
        print_verbose("Current direction: %s" % current_direction)
        if current_direction is None:
            return
        new_direction = (current_direction + 1) % 4
    elif direction == 'right':
        current_direction = get_current_rotation_direction()
        print_verbose("Current direction: %s" % current_direction)
        if current_direction is None:
            return
        new_direction = (current_direction - 1) % 4
    else:
        print_error_and_exit('Unexpected direction %s' % direction)
        return

    cmd = 'put system user_rotation %s' % new_direction
    execute_adb_shell_settings_command(cmd)


def get_current_rotation_direction():
    cmd = 'get system user_rotation'
    direction = execute_adb_shell_settings_command(cmd)
    print_verbose("Return value is %s" % direction)
    if not direction or direction == 'null':
        return 0  # default direction is 0, vertical straight
    try:
        return int(direction)
{'fraction_non_alphanumeric': 0.07065886699507389, 'fraction_numerical': 0.008004926108374385, 'mean_word_length': 3.4239019407558735, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 8, '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': '6396188', 'n_tokens_mistral': 3951, 'n_tokens_neox': 3839, 'n_words': 1078}