Skip to content

Commit

Permalink
fix: implements dummy spinner for non-tty environments
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Aug 7, 2023
1 parent 1ed0506 commit 140e65f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
10 changes: 7 additions & 3 deletions riocli/deployment/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import typing

import click
from click_help_colors import HelpColorsCommand
from yaspin import kbi_safe_yaspin

if sys.stdout.isatty():
from yaspin import kbi_safe_yaspin as Spinner
else:
from riocli.utils.spinner import DummySpinner as Spinner

from riocli.constants import Colors
from riocli.deployment.util import name_to_guid, select_details
Expand Down Expand Up @@ -49,7 +53,7 @@ def execute_command(
try:
comp_id, exec_id, pod_name = select_details(deployment_guid, component_name, exec_name)

with kbi_safe_yaspin(text='Executing command `{}`...'.format(command)) as spinner:
with Spinner(text='Executing command `{}`...'.format(command)):
stdout, stderr = run_on_cloud(deployment_guid, comp_id, exec_id, pod_name, command)

if stderr:
Expand Down
9 changes: 7 additions & 2 deletions riocli/device/vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import typing

import click
from click_help_colors import HelpColorsCommand
from yaspin import kbi_safe_yaspin

if sys.stdout.isatty():
from yaspin import kbi_safe_yaspin as Spinner
else:
from riocli.utils.spinner import DummySpinner as Spinner

from riocli.config import new_client
from riocli.constants import Colors
Expand Down Expand Up @@ -78,7 +83,7 @@ def toggle_vpn(devices: typing.List, enable: bool,
click.echo("") # Echo an empty line

result = []
with kbi_safe_yaspin() as spinner:
with Spinner() as spinner:
for device in final:
spinner.text = 'Updating VPN state on device {}'.format(
click.style(device.name, bold=True, fg=Colors.CYAN))
Expand Down
9 changes: 7 additions & 2 deletions riocli/parameter/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import typing

import click
from click_help_colors import HelpColorsCommand
from yaspin import kbi_safe_yaspin

if sys.stdout.isatty():
from yaspin import kbi_safe_yaspin as Spinner
else:
from riocli.utils.spinner import DummySpinner as Spinner

from riocli.config import new_client
from riocli.constants import Colors, Symbols
Expand Down Expand Up @@ -79,7 +84,7 @@ def apply_configurations(
"Do you want to apply the configurations?",
default=True, abort=True)

with kbi_safe_yaspin(text='Applying parameters...') as spinner:
with Spinner(text='Applying parameters...'):
response = client.apply_parameters(
list(device_ids.keys()),
list(tree_names),
Expand Down
10 changes: 8 additions & 2 deletions riocli/parameter/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

import click
from click_help_colors import HelpColorsCommand
from rapyuta_io.utils.rest_client import HttpMethod
from yaspin import kbi_safe_yaspin

if sys.stdout.isatty():
from yaspin import kbi_safe_yaspin as Spinner
else:
from riocli.utils.spinner import DummySpinner as Spinner

from riocli.constants import Colors, Symbols
from riocli.parameter.utils import _api_call
Expand Down Expand Up @@ -43,7 +49,7 @@ def delete_configurations(
if not silent:
click.confirm('Do you want to proceed?', default=True, abort=True)

with kbi_safe_yaspin(text='Deleting...', timer=True) as spinner:
with Spinner(text='Deleting...', timer=True) as spinner:
try:
data = _api_call(HttpMethod.DELETE, name=tree)
if data.get('data') != 'ok':
Expand Down
9 changes: 6 additions & 3 deletions riocli/parameter/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import typing

import click
from click_help_colors import HelpColorsCommand
from yaspin import kbi_safe_yaspin

if sys.stdout.isatty():
from yaspin import kbi_safe_yaspin as Spinner
else:
from riocli.utils.spinner import DummySpinner as Spinner
from riocli.config import new_client
from riocli.constants import Colors, Symbols
from riocli.parameter.utils import filter_trees, display_trees
Expand Down Expand Up @@ -57,8 +61,7 @@ def upload_configurations(

client = new_client()

with kbi_safe_yaspin(text="Uploading configurations...",
timer=True) as spinner:
with Spinner(text="Uploading configurations...", timer=True) as spinner:
try:
client.upload_configurations(
rootdir=path,
Expand Down
42 changes: 41 additions & 1 deletion riocli/utils/spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,46 @@
# limitations under the License.

import functools
import sys

import click
from yaspin import kbi_safe_yaspin


class DummySpinner:
"""DummySpinner replaces the yaspin spinner with a dummy one
This class is useful for cases when the active stream is not a tty
"""

def __init__(self, *args, **kwargs):
self.text = ''

def write(self, text):
click.echo(text)

def hidden(self):
return self

def fail(self, text='FAIL'):
click.echo('{} {}'.format(text, self.text))

def ok(self, text='OK'):
click.echo('{} {}'.format(text, self.text))

def __getattr__(self, name):
return self

def __call__(self):
pass

def __enter__(self):
return self

def __exit__(self, *args, **kwargs):
return False


def with_spinner(**spin_kwargs):
"""
Decorator for wrapping your function with a spinner
Expand All @@ -42,7 +78,11 @@ def do_something(arg1, spinner=None):
def decorated(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with kbi_safe_yaspin(**spin_kwargs) as spinner:
spinner_class = kbi_safe_yaspin
if not sys.stdout.isatty():
spinner_class = DummySpinner

with spinner_class(**spin_kwargs) as spinner:
kwargs['spinner'] = spinner
return func(*args, **kwargs)

Expand Down

0 comments on commit 140e65f

Please sign in to comment.