tksn.adb.device module

Implementation of tksn.adb.device.Device class

copyright:
  1. 2016 by tksn
license:

MIT

class tksn.adb.device.AsyncResult(proc, save_as_func=None)[source]

Accessor to an asynchronous device call result

AsyncResult provides an access to a previously started ADB process and its result. It includes, a way to wait for completion of the process, a way to kill the process, and a way to get the result after the process is completed or killed.

save_as(filepath)[source]

Save result as a file

Parameters:filepath (str) – where to save the result data
stop()[source]

Stop the process immediately

Equivalent to wait(kill=True)

Returns:self object
Return type:AsyncResult
wait(timeout=None, kill=False)[source]

Wait for completion of the process

If the process does not terminate after timeout seconds, raise a TimeoutExpired exception.

Parameters:
  • timeout (int) – timeout in seconds. Infinite if None.
  • kill (bool) – True if kill the process immediately.
Returns:

self object

Return type:

AsyncResult

class tksn.adb.device.BugreportAccessor(call_async)[source]

Bugreport accessor class, which can be used to start and stop bugreport and to retrieve its result.

start(out=None, err=None)[source]

Invoke bugreport

Start bugreport process and return immediately. You can wait for the completion, and can get the result of the bugreport process by AsyncResult object which is returned by this method.

Parameters:
  • out (file) – file object to receive stdout of bugreport. Can be None if you retrieve stdout via AsyncResult.save_as.
  • err (file) – file object to receive stderr of bugreport. Can be None if you don’t want stderr.
Returns:

AsyncResult object which can be used to wait completion and to obtain result

Return type:

AsyncResult

class tksn.adb.device.Device(serialno=None)[source]

Android device class

Device class provides various methods which wrap ADB commands, such as logcat, screencap, screenrecord, etc.

bugreport

Bugreport accessor

Returns:bugreport accessor object
Return type:BugreportAccessor
imei

IMEI property

Valid only if the device is capable of connecting to cellular network.

Returns:The devices’s IMEI
Return type:str
key(code)[source]

Get hardware key accessor

Parameters:code (int or str) – key event code, or, event name such as ‘KEY_VOLUMEDOWN’
Returns:Key accessor object which provides up/down method.
Return type:KeyAccessor
logcat

Logcat accessor

Returns:logcat accessor object
Return type:LogcatAccessor
phone_number

Phone number property

Valid only if the device is a phone, and is activated on the cellular network.

Returns:The devices’s phone number
Return type:str
pull(from_path, to_path)[source]

Pull a file from the Android device

Parameters:
  • from_path (str) – source path on the deivce
  • to_path (str) – destination path
Returns:

(stdout, stderr) pair from adb pull command

Return type:

tuple

push(from_path, to_path)[source]

Push a file to the Android device

Parameters:
  • from_path (str) – source path
  • to_path (str) – destination path on the device
Returns:

(stdout, stderr) pair from adb push command

Return type:

tuple

screencap

Screencap accessor

Returns:screencap accessor object
Return type:ScreencapAccessor
screenrecord

Screenrecord accessor

Returns:screenrecord accessor object
Return type:ScreenrecordAccessor
serialno

Serial number of the Android device

Returns:Serial number
Return type:str
exception tksn.adb.device.DeviceNotFoundError[source]

Device not found error

Thrown when the device is not reachable.

class tksn.adb.device.KeyAccessor(hwkey, code)[source]

Hardware key accessor class

KeyAccessor provides key down(press) and up(release) method.

press(auto_release=True)[source]

Press key

Parameters:auto_release (bool) – True to do press-and-release at once
Returns:self object
Return type:KeyAccessor
release()[source]

Release key

Returns:self object
Return type:KeyAccessor
class tksn.adb.device.LogcatAccessor(call_sync, call_async)[source]

Logcat accessor class, which can be used to start and stop logcat and to retrieve its result.

clear()[source]

Clear the device’s log buffer

Returns:(stdout, stderr) pair from adb logcat command
Return type:tuple
start(out=None, err=None, buffer=None, format=None, filterspec=None)[source]

Invoke logcat

Start logcat process and return immediately. You can wait for the completion, and can get the result of the logcat process by AsyncResult object which is returned by this method.

Parameters:
  • out (file) – file object to receive stdout of logcat. Can be None if you retrieve stdout via AsyncResult.save_as.
  • err (file) – file object to receive stderr of logcat. Can be None if you don’t want stderr.
  • buffer (tuple) – tuple contains name of buffers from which you get logs. Default is (‘main’, ‘system’, ‘events’, ‘radio’). Name of a buffer is what you can secify with ‘-b’ option of adb logcat command.
  • format (str) – format specifier. Default is ‘time’. Format is what you can secify with ‘-v’ option of adb logcat command.
  • filterspec (tuple) – FILTERSPEC to adb logcat command, if any.
Returns:

AsyncResult object which can be used to wait completion and to obtain result

Return type:

AsyncResult

class tksn.adb.device.ScreencapAccessor(pull, call_async)[source]

Screencap accessor class, which can be used to start and stop screencap and to retrieve its result.

as_bytes()[source]

Get screen capture as bytes

Returns:screencapture data. Useful when you want to create PIL.Image object via PIL.Image.frombytes()
Return type:bytes
save_as(filepath)[source]

Save screen capture as a file

Equivalent to start().wait().save_as(filepath)

Parameters:filepath (str) – destination file path
start(filepath_in_device='/sdcard/screen.png')[source]

Invoke screencap

Start screenrecord process and returns immediately. You can wait for the completion, and can get the result of the screencap process by AsyncResult object which is returned by this method.

Parameters:filepath_in_device (str) – File path to screen capture (png) file in the device.
Returns:AsyncResult object which can be used to wait completion and to obtain result
Return type:AsyncResult
class tksn.adb.device.ScreenrecordAccessor(pull, call_async)[source]

Screenrecord accessor class, which can be used to start and stop screenrecord and to retrieve its result.

start(size=None, bitrate=1000000, rotate=False, filepath_in_device='/sdcard/screenrecord.mp4')[source]

Invoke screenrecord.

Start screenrecord process and returns immediately. You can wait for the completion, and can get the result of the screenrecord process by AsyncResult object which is returned by this method.

Parameters:
  • size (tuple) – (width, height) tuple which is passed to screenrecord with ‘–size’ option. None if you don’t want to specify ‘–size’ option. Default is None.
  • bitrate (int) – bit rate which is passed to screenrecord with –bit-rate option. Default is 1000000.
  • rotate (bool) – True if use ‘–rotate’ option of screenrecord. Default is False.
  • filepath_in_device (str) – File path to screen record (mp4) file in the device.
Returns:

AsyncResult object which can be used to wait completion and to obtain result

Return type:

AsyncResult