Skip to content

Commit

Permalink
replace click.echo with logger (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkasprzyk authored May 31, 2022
1 parent 07368a7 commit d0ba931
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 200 deletions.
14 changes: 7 additions & 7 deletions neptune/new/attributes/atoms/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
#
import typing

import click

from neptune.new.attributes.atoms.copiable_atom import CopiableAtom
from neptune.new.internal.container_type import ContainerType
from neptune.new.internal.operation import AssignString
from neptune.new.internal.utils.logger import logger
from neptune.new.internal.utils.paths import path_to_str
from neptune.new.types.atoms.string import String as StringVal

Expand Down Expand Up @@ -60,11 +59,12 @@ def assign(self, value: typing.Union[StringVal, str], wait: bool = False):
if not self._value_truncation_occurred:
# the first truncation
self._value_truncation_occurred = True
click.echo(
f"Warning: string '{path_to_str(self._path)}' value was "
f"longer than {String.MAX_VALUE_LENGTH} characters and was truncated. "
f"This warning is printed only once.",
err=True,
logger.warning(
"Warning: string '%s' value was"
" longer than %s characters and was truncated."
" This warning is printed only once.",
path_to_str(self._path),
String.MAX_VALUE_LENGTH,
)

with self._container.lock():
Expand Down
8 changes: 2 additions & 6 deletions neptune/new/attributes/series/float_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#
from typing import Iterable, List, Optional, Union

import click

from neptune.new.attributes.series.fetchable_series import FetchableSeries
from neptune.new.attributes.series.series import Series
from neptune.new.internal.backends.api_model import FloatSeriesValues
Expand All @@ -27,6 +25,7 @@
Operation,
)
from neptune.new.internal.utils import verify_type
from neptune.new.internal.utils.logger import logger
from neptune.new.types.series.float_series import FloatSeries as FloatSeriesVal
from neptune.utils import split_to_chunks

Expand Down Expand Up @@ -64,10 +63,7 @@ def _get_config_operation_from_value(self, value: Val) -> Optional[Operation]:

def _data_to_value(self, values: Iterable, **kwargs) -> Val:
if kwargs:
click.echo(
"Warning: unexpected arguments ({kwargs}) in FloatSeries".format(kwargs=kwargs),
err=True,
)
logger.warning("Warning: unexpected arguments (%s) in FloatSeries", kwargs)
return FloatSeriesVal(values)

def _is_value_type(self, value) -> bool:
Expand Down
19 changes: 8 additions & 11 deletions neptune/new/attributes/series/string_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
#
from typing import TYPE_CHECKING, Iterable, List, Optional

import click

from neptune.new.attributes.series.fetchable_series import FetchableSeries
from neptune.new.attributes.series.series import Series
from neptune.new.internal.backends.api_model import StringSeriesValues
from neptune.new.internal.operation import ClearStringLog, LogStrings, Operation
from neptune.new.internal.utils.logger import logger
from neptune.new.internal.utils.paths import path_to_str
from neptune.new.types.series.string_series import StringSeries as StringSeriesVal
from neptune.utils import split_to_chunks
Expand Down Expand Up @@ -48,11 +47,12 @@ def _get_log_operations_from_value(
):
# the first truncation
self._value_truncation_occurred = True
click.echo(
f"Warning: string series '{ path_to_str(self._path)}' value was "
f"longer than {MAX_STRING_SERIES_VALUE_LENGTH} characters and was truncated. "
f"This warning is printed only once per series.",
err=True,
logger.warning(
"Warning: string series '%s' value was"
" longer than %s characters and was truncated."
" This warning is printed only once per series.",
path_to_str(self._path),
MAX_STRING_SERIES_VALUE_LENGTH,
)

values = [LogStrings.ValueType(val, step=step, ts=timestamp) for val in values]
Expand All @@ -63,10 +63,7 @@ def _get_clear_operation(self) -> Operation:

def _data_to_value(self, values: Iterable, **kwargs) -> Val:
if kwargs:
click.echo(
"Warning: unexpected arguments ({kwargs}) in StringSeries".format(kwargs=kwargs),
err=True,
)
logger.warning("Warning: unexpected arguments (%s) in StringSeries", kwargs)
return StringSeriesVal(values)

def _is_value_type(self, value) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions neptune/new/internal/backends/hosted_file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from typing import AnyStr, Dict, Iterable, List, Optional, Set, Union
from urllib.parse import urlencode

import click
from bravado.exception import HTTPPaymentRequired, HTTPUnprocessableEntity
from bravado.requests_client import RequestsClient
from requests import Request, Response
Expand Down Expand Up @@ -60,6 +59,7 @@
with_api_exceptions_handler,
)
from neptune.new.internal.utils import get_absolute_paths, get_common_root
from neptune.new.internal.utils.logger import logger

DEFAULT_CHUNK_SIZE = 5 * BYTES_IN_ONE_MB
DEFAULT_UPLOAD_CONFIG = AttributeUploadConfiguration(chunk_size=DEFAULT_CHUNK_SIZE)
Expand Down Expand Up @@ -315,7 +315,7 @@ def _multichunk_upload_with_retry(
upload_entry, swagger_client, query_params, multipart_config, urlset
)
except UploadedFileChanged as e:
click.echo(e)
logger.error(str(e))


def _multichunk_upload(
Expand Down
26 changes: 12 additions & 14 deletions neptune/new/internal/backends/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
import logging
import os
import socket
import sys
import time
from functools import lru_cache, wraps
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Mapping, Optional, Text
from urllib.parse import urljoin, urlparse

import click
import requests
import urllib3
from bravado.client import SwaggerClient
Expand Down Expand Up @@ -69,6 +67,7 @@
from neptune.new.internal.backends.swagger_client_wrapper import SwaggerClientWrapper
from neptune.new.internal.operation import CopyAttribute, Operation
from neptune.new.internal.utils import replace_patch_version
from neptune.new.internal.utils.logger import logger

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -197,11 +196,10 @@ def verify_client_version(client_config: ClientConfig, version: Version):
client_config.version_info.min_recommended
and client_config.version_info.min_recommended > version
):
click.echo(
"WARNING: We recommend an upgrade to a new version of neptune-client - {} (installed - {}).".format(
client_config.version_info.min_recommended, version
),
sys.stderr,
logger.warning(
"WARNING: We recommend an upgrade to a new version of neptune-client - %s (installed - %s).",
client_config.version_info.min_recommended,
version,
)


Expand All @@ -210,7 +208,7 @@ def update_session_proxies(session: Session, proxies: Optional[Dict[str, str]]):
try:
session.proxies.update(proxies)
except (TypeError, ValueError):
raise ValueError("Wrong proxies format: {}".format(proxies))
raise ValueError(f"Wrong proxies format: {proxies}")


def build_operation_url(base_api: str, operation_url: str) -> str:
Expand All @@ -225,13 +223,13 @@ def handle_server_raw_response_messages(response: Response):
try:
info = response.headers.get("X-Server-Info")
if info:
click.echo(info)
logger.info(info)
warning = response.headers.get("X-Server-Warning")
if warning:
click.echo(warning)
logger.warning(warning)
error = response.headers.get("X-Server-Error")
if error:
click.echo(message=error, err=True)
logger.error(error)
return response
except Exception:
# any issues with printing server messages should not cause code to fail
Expand All @@ -258,13 +256,13 @@ def _handle_response(self):
try:
info = self._delegate.headers.get("X-Server-Info")
if info:
click.echo(info)
logger.info(info)
warning = self._delegate.headers.get("X-Server-Warning")
if warning:
click.echo(warning)
logger.warning(warning)
error = self._delegate.headers.get("X-Server-Error")
if error:
click.echo(message=error, err=True)
logger.error(error)
except Exception:
# any issues with printing server messages should not cause code to fail
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from time import monotonic, time
from typing import List, Optional

import click

from neptune.new.internal.backends.neptune_backend import NeptuneBackend
from neptune.new.internal.container_type import ContainerType
from neptune.new.internal.disk_queue import DiskQueue
Expand All @@ -31,6 +29,7 @@
OperationProcessor,
)
from neptune.new.internal.threading.daemon import Daemon
from neptune.new.internal.utils.logger import logger

# pylint: disable=protected-access

Expand Down Expand Up @@ -103,19 +102,19 @@ def _wait_for_queue_empty(self, initial_queue_size: int, seconds: Optional[float
)
if initial_queue_size > 0:
if self._consumer.last_backoff_time > 0:
click.echo(
f"We have been experiencing connection interruptions during your run. "
f"Neptune client will now try to resume connection and sync data for the next "
f"{max_reconnect_wait_time} seconds. "
f"You can also kill this process and synchronize your data manually later "
f"using `neptune sync` command.",
sys.stderr,
logger.warning(
"We have been experiencing connection interruptions during your run."
" Neptune client will now try to resume connection and sync data for the next"
" %s seconds."
" You can also kill this process and synchronize your data manually later"
" using `neptune sync` command.",
max_reconnect_wait_time,
)
else:
click.echo(
f"Waiting for the remaining {initial_queue_size} operations to synchronize with Neptune. "
f"Do not kill this process.",
sys.stderr,
logger.warning(
"Waiting for the remaining %s operations to synchronize with Neptune."
" Do not kill this process.",
initial_queue_size,
)

while True:
Expand All @@ -136,32 +135,34 @@ def _wait_for_queue_empty(self, initial_queue_size: int, seconds: Optional[float
(already_synced / initial_queue_size) * 100 if initial_queue_size else 100
)
if size_remaining == 0:
click.echo(f"All {initial_queue_size} operations synced, thanks for waiting!")
logger.info("All %s operations synced, thanks for waiting!", initial_queue_size)
return

time_elapsed = monotonic() - waiting_start
if self._consumer.last_backoff_time > 0 and time_elapsed >= max_reconnect_wait_time:
click.echo(
f"Failed to reconnect with Neptune in {max_reconnect_wait_time} seconds."
f" You have {size_remaining} operations saved on disk that can be manually synced"
f" using `neptune sync` command.",
sys.stderr,
logger.warning(
"Failed to reconnect with Neptune in %s seconds."
" You have %s operations saved on disk that can be manually synced"
" using `neptune sync` command.",
max_reconnect_wait_time,
size_remaining,
)
return

if seconds is not None and wait_time == 0:
click.echo(
f"Failed to sync all operations in {seconds} seconds."
f" You have {size_remaining} operations saved on disk that can be manually synced"
f" using `neptune sync` command.",
sys.stderr,
logger.warning(
"Failed to sync all operations in %s seconds."
" You have %s operations saved on disk that can be manually synced"
" using `neptune sync` command.",
seconds,
size_remaining,
)
return

click.echo(
f"Still waiting for the remaining {size_remaining} operations "
f"({already_synced_proc:.2f}% done). Please wait.",
sys.stderr,
logger.warning(
"Still waiting for the remaining %s operations" " (%.2f%% done). Please wait.",
size_remaining,
already_synced_proc,
)

def stop(self, seconds: Optional[float] = None):
Expand Down
22 changes: 10 additions & 12 deletions neptune/new/internal/threading/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
#
import abc
import functools
import sys
import threading
import time

import click

from neptune.new.exceptions import NeptuneConnectionLostException
from neptune.new.internal.utils.logger import logger


class Daemon(threading.Thread):
Expand Down Expand Up @@ -77,15 +75,15 @@ def wrapper(self_: Daemon, *args, **kwargs):
result = func(self_, *args, **kwargs)
if self_.last_backoff_time > 0:
self_.last_backoff_time = 0
click.echo("Communication with Neptune restored!", sys.stderr)
logger.info("Communication with Neptune restored!")
return result
except NeptuneConnectionLostException as e:
if self_.last_backoff_time == 0:
click.echo(
"Experiencing connection interruptions. "
"Will try to reestablish communication with Neptune. "
f"Internal exception was: {e.cause.__class__.__name__}",
sys.stderr,
logger.warning(
"Experiencing connection interruptions."
" Will try to reestablish communication with Neptune."
" Internal exception was: %s",
e.cause.__class__.__name__,
)
self_.last_backoff_time = self.INITIAL_RETRY_BACKOFF
else:
Expand All @@ -94,9 +92,9 @@ def wrapper(self_: Daemon, *args, **kwargs):
)
time.sleep(self_.last_backoff_time)
except Exception:
click.echo(
f"Unexpected error occurred in Neptune background thread: {self.kill_message}",
sys.stderr,
logger.error(
"Unexpected error occurred in Neptune background thread: %s",
self.kill_message,
)
raise

Expand Down
Loading

0 comments on commit d0ba931

Please sign in to comment.