-
-
Notifications
You must be signed in to change notification settings - Fork 568
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
Revise error handling to be more consistent for library users #180
Changes from 2 commits
d64610f
c344147
9188b75
be17195
0a43d0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Click commons. | ||
|
||
This file contains common functions for cli tools. | ||
""" | ||
import click | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module level import not at top of file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module level import not at top of file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The printout has to be before trying to import |
||
import ipaddress | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module level import not at top of file |
||
import miio | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module level import not at top of file |
||
import logging | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module level import not at top of file |
||
|
||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def validate_ip(ctx, param, value): | ||
try: | ||
ipaddress.ip_address(value) | ||
return value | ||
except ValueError as ex: | ||
raise click.BadParameter("Invalid IP: %s" % ex) | ||
|
||
|
||
def validate_token(ctx, param, value): | ||
token_len = len(value) | ||
if token_len != 32: | ||
raise click.BadParameter("Token length != 32 chars: %s" % token_len) | ||
return value | ||
|
||
|
||
class ExceptionHandlerGroup(click.Group): | ||
def __call__(self, *args, **kwargs): | ||
try: | ||
return self.main(*args, **kwargs) | ||
except miio.DeviceException as ex: | ||
_LOGGER.debug("Exception: %s", ex, exc_info=True) | ||
click.echo(click.style("Error: %s" % ex, fg='red', bold=True)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
sys.exit(1) | ||
|
||
import miio # noqa: E402 | ||
from miio.click_common import (ExceptionHandlerGroup, validate_ip, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module level import not at top of file |
||
validate_token) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
pass_dev = click.make_pass_decorator(miio.PhilipsEyecare) | ||
|
@@ -36,22 +38,7 @@ def validate_scene(ctx, param, value): | |
return value | ||
|
||
|
||
def validate_ip(ctx, param, value): | ||
try: | ||
ipaddress.ip_address(value) | ||
return value | ||
except ValueError as ex: | ||
raise click.BadParameter("Invalid IP: %s" % ex) | ||
|
||
|
||
def validate_token(ctx, param, value): | ||
token_len = len(value) | ||
if token_len != 32: | ||
raise click.BadParameter("Token length != 32 chars: %s" % token_len) | ||
return value | ||
|
||
|
||
@click.group(invoke_without_command=True) | ||
@click.group(invoke_without_command=True, cls=ExceptionHandlerGroup) | ||
@click.option('--ip', envvar="DEVICE_IP", callback=validate_ip) | ||
@click.option('--token', envvar="DEVICE_TOKEN", callback=validate_token) | ||
@click.option('-d', '--debug', default=False, count=True) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
import click | ||
import ast | ||
import sys | ||
import ipaddress | ||
from typing import Any # noqa: F401 | ||
|
||
if sys.version_info < (3, 4): | ||
|
@@ -12,27 +11,14 @@ | |
sys.exit(1) | ||
|
||
import miio # noqa: E402 | ||
from miio.click_common import (ExceptionHandlerGroup, validate_ip, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module level import not at top of file |
||
validate_token) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
pass_dev = click.make_pass_decorator(miio.Plug) | ||
|
||
|
||
def validate_ip(ctx, param, value): | ||
try: | ||
ipaddress.ip_address(value) | ||
return value | ||
except ValueError as ex: | ||
raise click.BadParameter("Invalid IP: %s" % ex) | ||
|
||
|
||
def validate_token(ctx, param, value): | ||
token_len = len(value) | ||
if token_len != 32: | ||
raise click.BadParameter("Token length != 32 chars: %s" % token_len) | ||
return value | ||
|
||
|
||
@click.group(invoke_without_command=True) | ||
@click.group(invoke_without_command=True, cls=ExceptionHandlerGroup) | ||
@click.option('--ip', envvar="DEVICE_IP", callback=validate_ip) | ||
@click.option('--token', envvar="DEVICE_TOKEN", callback=validate_token) | ||
@click.option('-d', '--debug', default=False, count=True) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
import ast | ||
import sys | ||
import json | ||
import ipaddress | ||
import time | ||
import pathlib | ||
from appdirs import user_cache_dir | ||
|
@@ -19,31 +18,14 @@ | |
sys.exit(1) | ||
|
||
import miio # noqa: E402 | ||
from miio.click_common import (ExceptionHandlerGroup, validate_ip, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module level import not at top of file |
||
validate_token) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
pass_dev = click.make_pass_decorator(miio.Device, ensure=True) | ||
|
||
|
||
def validate_ip(ctx, param, value): | ||
if value is None: | ||
return value | ||
try: | ||
ipaddress.ip_address(value) | ||
return value | ||
except ValueError as ex: | ||
raise click.BadParameter("Invalid IP: %s" % ex) | ||
|
||
|
||
def validate_token(ctx, param, value): | ||
if value is None: | ||
return value | ||
token_len = len(value) | ||
if token_len != 32: | ||
raise click.BadParameter("Token length != 32 chars: %s" % token_len) | ||
return value | ||
|
||
|
||
@click.group(invoke_without_command=True) | ||
@click.group(invoke_without_command=True, cls=ExceptionHandlerGroup) | ||
@click.option('--ip', envvar="MIROBO_IP", callback=validate_ip) | ||
@click.option('--token', envvar="MIROBO_TOKEN", callback=validate_token) | ||
@click.option('-d', '--debug', default=False, count=True) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module level import not at top of file