Skip to content

Commit

Permalink
Merge pull request #1335 from doronz88/refactor/reconnect
Browse files Browse the repository at this point in the history
cli: optimize `--reconnect` option implementation
  • Loading branch information
doronz88 authored Jan 13, 2025
2 parents 00f8d96 + c6ef8e4 commit 285f644
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions pymobiledevice3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,19 @@ def cli(reconnect: bool) -> None:
RECONNECT = reconnect


def main() -> None:
def invoke_cli_with_error_handling() -> bool:
"""
Invoke the command line interface and return `True` if the failure reason of the command was that the device was
disconnected.
"""
try:
cli()
except NoDeviceConnectedError:
logger.error('Device is not connected')
return True
except ConnectionAbortedError:
logger.error('Device was disconnected')
if RECONNECT:
lockdown = retry_create_using_usbmux(None)
lockdown.close()
cli()
return True
except NotPairedError:
logger.error('Device is not paired')
except UserDeniedPairingError:
Expand Down Expand Up @@ -258,6 +260,24 @@ def main() -> None:
except QuicProtocolNotSupportedError as e:
logger.error(str(e))

return False


def main() -> None:
# Retry to invoke the CLI
while invoke_cli_with_error_handling():
# If reached here, this means the failure reason was that the device is disconnected
if not RECONNECT:
# If not invoked with the `--reconnect` option, break here
break
try:
# Wait for the device to be available again
lockdown = retry_create_using_usbmux(None)
lockdown.close()
except KeyboardInterrupt:
print('Aborted.')
break


if __name__ == '__main__':
main()

0 comments on commit 285f644

Please sign in to comment.