Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Black formatter across source #362

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v2
Expand Down
48 changes: 42 additions & 6 deletions androidtv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,62 @@ def setup(

"""
if device_class == "androidtv":
atv = AndroidTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
atv.adb_connect(log_errors=log_errors, auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)
atv = AndroidTVSync(
host,
port,
adbkey,
adb_server_ip,
adb_server_port,
state_detection_rules,
signer,
)
atv.adb_connect(
log_errors=log_errors,
auth_timeout_s=auth_timeout_s,
transport_timeout_s=transport_timeout_s,
)
atv.get_device_properties()
atv.get_installed_apps()
return atv

if device_class == "firetv":
ftv = FireTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
ftv.adb_connect(log_errors=log_errors, auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)
ftv = FireTVSync(
host,
port,
adbkey,
adb_server_ip,
adb_server_port,
state_detection_rules,
signer,
)
ftv.adb_connect(
log_errors=log_errors,
auth_timeout_s=auth_timeout_s,
transport_timeout_s=transport_timeout_s,
)
ftv.get_device_properties()
ftv.get_installed_apps()
return ftv

if device_class != "auto":
raise ValueError("`device_class` must be 'androidtv', 'firetv', or 'auto'.")

aftv = BaseTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
aftv = BaseTVSync(
host,
port,
adbkey,
adb_server_ip,
adb_server_port,
state_detection_rules,
signer,
)

# establish the ADB connection
aftv.adb_connect(log_errors=log_errors, auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)
aftv.adb_connect(
log_errors=log_errors,
auth_timeout_s=auth_timeout_s,
transport_timeout_s=transport_timeout_s,
)

# get device properties
aftv.device_properties = aftv.get_device_properties()
Expand Down
114 changes: 92 additions & 22 deletions androidtv/adb_manager/adb_manager_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
class AdbDeviceUsbAsync:
"""An async wrapper for the adb-shell ``AdbDeviceUsb`` class."""

def __init__(self, serial=None, port_path=None, default_transport_timeout_s=None, banner=None):
def __init__(
self, serial=None, port_path=None, default_transport_timeout_s=None, banner=None
):
self._adb = AdbDeviceUsb(serial, port_path, default_transport_timeout_s, banner)

@property
Expand All @@ -53,7 +55,13 @@ async def connect(
):
"""Establish an ADB connection to the device."""
return await asyncio.get_running_loop().run_in_executor(
None, self._adb.connect, rsa_keys, transport_timeout_s, auth_timeout_s, read_timeout_s, auth_callback
None,
self._adb.connect,
rsa_keys,
transport_timeout_s,
auth_timeout_s,
read_timeout_s,
auth_callback,
)

async def pull(
Expand All @@ -66,7 +74,13 @@ async def pull(
):
"""Pull a file from the device."""
await asyncio.get_running_loop().run_in_executor(
None, self._adb.pull, device_path, local_path, progress_callback, transport_timeout_s, read_timeout_s
None,
self._adb.pull,
device_path,
local_path,
progress_callback,
transport_timeout_s,
read_timeout_s,
)

async def push(
Expand All @@ -93,11 +107,22 @@ async def push(
)

async def shell(
self, command, transport_timeout_s=None, read_timeout_s=DEFAULT_READ_TIMEOUT_S, timeout_s=None, decode=True
self,
command,
transport_timeout_s=None,
read_timeout_s=DEFAULT_READ_TIMEOUT_S,
timeout_s=None,
decode=True,
):
"""Send an ADB shell command to the device."""
return await asyncio.get_running_loop().run_in_executor(
None, self._adb.shell, command, transport_timeout_s, read_timeout_s, timeout_s, decode
None,
self._adb.shell,
command,
transport_timeout_s,
read_timeout_s,
timeout_s,
decode,
)


Expand All @@ -109,19 +134,27 @@ def __init__(self, device):

async def pull(self, device_path, local_path):
"""Download a file."""
return await asyncio.get_running_loop().run_in_executor(None, self._device.pull, device_path, local_path)
return await asyncio.get_running_loop().run_in_executor(
None, self._device.pull, device_path, local_path
)

async def push(self, local_path, device_path):
"""Upload a file."""
return await asyncio.get_running_loop().run_in_executor(None, self._device.push, local_path, device_path)
return await asyncio.get_running_loop().run_in_executor(
None, self._device.push, local_path, device_path
)

async def screencap(self):
"""Take a screencap."""
return await asyncio.get_running_loop().run_in_executor(None, self._device.screencap)
return await asyncio.get_running_loop().run_in_executor(
None, self._device.screencap
)

async def shell(self, cmd):
"""Send a shell command."""
return await asyncio.get_running_loop().run_in_executor(None, self._device.shell, cmd)
return await asyncio.get_running_loop().run_in_executor(
None, self._device.shell, cmd
)


# pylint: disable=too-few-public-methods
Expand All @@ -133,7 +166,9 @@ def __init__(self, host, port):

async def device(self, serial):
"""Get a ``DeviceAsync`` instance."""
dev = await asyncio.get_running_loop().run_in_executor(None, self._client.device, serial)
dev = await asyncio.get_running_loop().run_in_executor(
None, self._client.device, serial
)
if dev:
return DeviceAsync(dev)
return None
Expand Down Expand Up @@ -201,10 +236,14 @@ def __init__(self, host, port, adbkey="", signer=None):

if host:
self._adb = AdbDeviceTcpAsync(
host=self.host, port=self.port, default_transport_timeout_s=DEFAULT_ADB_TIMEOUT_S
host=self.host,
port=self.port,
default_transport_timeout_s=DEFAULT_ADB_TIMEOUT_S,
)
else:
self._adb = AdbDeviceUsbAsync(default_transport_timeout_s=DEFAULT_ADB_TIMEOUT_S)
self._adb = AdbDeviceUsbAsync(
default_transport_timeout_s=DEFAULT_ADB_TIMEOUT_S
)

self._signer = signer

Expand Down Expand Up @@ -267,10 +306,17 @@ async def connect(

# Connect without authentication
else:
await self._adb.connect(transport_timeout_s=transport_timeout_s, auth_timeout_s=auth_timeout_s)
await self._adb.connect(
transport_timeout_s=transport_timeout_s,
auth_timeout_s=auth_timeout_s,
)

# ADB connection successfully established
_LOGGER.debug("ADB connection to %s:%d successfully established", self.host, self.port)
_LOGGER.debug(
"ADB connection to %s:%d successfully established",
self.host,
self.port,
)
return True

except OSError as exc:
Expand All @@ -292,15 +338,23 @@ async def connect(
except Exception as exc: # pylint: disable=broad-except
if log_errors:
_LOGGER.warning(
"Couldn't connect to %s:%d. %s: %s", self.host, self.port, exc.__class__.__name__, exc
"Couldn't connect to %s:%d. %s: %s",
self.host,
self.port,
exc.__class__.__name__,
exc,
)

# ADB connection attempt failed
await self.close()
return False

except LockNotAcquiredException:
_LOGGER.warning("Couldn't connect to %s:%d because adb-shell lock not acquired.", self.host, self.port)
_LOGGER.warning(
"Couldn't connect to %s:%d because adb-shell lock not acquired.",
self.host,
self.port,
)
await self.close()
return False

Expand Down Expand Up @@ -355,7 +409,11 @@ async def pull(self, local_path, device_path):

async with _acquire(self._adb_lock):
_LOGGER.debug(
"Sending command to %s:%d via adb-shell: pull(%s, %s)", self.host, self.port, local_path, device_path
"Sending command to %s:%d via adb-shell: pull(%s, %s)",
self.host,
self.port,
local_path,
device_path,
)
await self._adb.pull(device_path, local_path)
return
Expand Down Expand Up @@ -383,7 +441,11 @@ async def push(self, local_path, device_path):

async with _acquire(self._adb_lock):
_LOGGER.debug(
"Sending command to %s:%d via adb-shell: push(%s, %s)", self.host, self.port, local_path, device_path
"Sending command to %s:%d via adb-shell: push(%s, %s)",
self.host,
self.port,
local_path,
device_path,
)
await self._adb.push(local_path, device_path)
return
Expand All @@ -406,7 +468,9 @@ async def screencap(self):
return None

async with _acquire(self._adb_lock):
_LOGGER.debug("Taking screencap from %s:%d via adb-shell", self.host, self.port)
_LOGGER.debug(
"Taking screencap from %s:%d via adb-shell", self.host, self.port
)
result = await self._adb.shell("screencap -p", decode=False)
if result and result[5:6] == b"\r":
return result.replace(b"\r\n", b"\n")
Expand Down Expand Up @@ -436,7 +500,9 @@ async def shell(self, cmd):
return None

async with _acquire(self._adb_lock):
_LOGGER.debug("Sending command to %s:%d via adb-shell: %s", self.host, self.port, cmd)
_LOGGER.debug(
"Sending command to %s:%d via adb-shell: %s", self.host, self.port, cmd
)
return await self._adb.shell(cmd)


Expand Down Expand Up @@ -511,8 +577,12 @@ async def connect(self, log_errors=True):
async with _acquire(self._adb_lock):
# Catch exceptions
try:
self._adb_client = ClientAsync(host=self.adb_server_ip, port=self.adb_server_port)
self._adb_device = await self._adb_client.device("{}:{}".format(self.host, self.port))
self._adb_client = ClientAsync(
host=self.adb_server_ip, port=self.adb_server_port
)
self._adb_device = await self._adb_client.device(
"{}:{}".format(self.host, self.port)
)

# ADB connection successfully established
if self._adb_device:
Expand Down
Loading
Loading