From 5976b5e4662f3d60a925b9fc8ef23012146f20a3 Mon Sep 17 00:00:00 2001 From: mindsbackyard Date: Sun, 14 Jul 2019 20:29:30 +0000 Subject: [PATCH] =?UTF-8?q?disable=20analytics=20warning=20message=20on=20?= =?UTF-8?q?init=20if=20disabled=20globally/system=E2=80=A6=20(#2268)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * disable analytics warning message on init if disabled globally/system-wide * rename Analytics._is_enabled Analytics.is_enabled * refactor trying reduce cognitive complexity metric * fix missing imports * fix mistaken reference of self --- dvc/analytics.py | 34 ++++++++++++++++++++-------------- dvc/repo/init.py | 20 +++++++++++--------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/dvc/analytics.py b/dvc/analytics.py index d9805bfd1..6643db768 100644 --- a/dvc/analytics.py +++ b/dvc/analytics.py @@ -213,10 +213,20 @@ class Analytics(object): return core.get(Config.SECTION_CORE_ANALYTICS, True) @staticmethod - def _is_enabled(cmd=None): + def _get_current_config(): from dvc.config import Config from dvc.repo import Repo from dvc.exceptions import NotDvcRepoError + + try: + dvc_dir = Repo.find_dvc_dir() + config = Config(dvc_dir) + except NotDvcRepoError: + config = Config(validate=False) + return config + + @staticmethod + def is_enabled(cmd=None): from dvc.command.daemon import CmdDaemonBase if os.getenv("DVC_TEST"): @@ -225,17 +235,13 @@ class Analytics(object): if isinstance(cmd, CmdDaemonBase): return False - if cmd is None or not hasattr(cmd, "config"): - try: - dvc_dir = Repo.find_dvc_dir() - config = Config(dvc_dir) - assert config is not None - except NotDvcRepoError: - config = Config(validate=False) - assert config is not None - else: - config = cmd.config - assert config is not None + config = ( + Analytics._get_current_config() + if cmd is None or not hasattr(cmd, "config") + else cmd.config + ) + + assert config is not None enabled = Analytics._is_enabled_config(config) logger.debug( @@ -253,7 +259,7 @@ class Analytics(object): """ from dvc.daemon import daemon - if not Analytics._is_enabled(cmd): + if not Analytics.is_enabled(cmd): return analytics = Analytics() @@ -264,7 +270,7 @@ class Analytics(object): """Collect and send analytics.""" import requests - if not self._is_enabled(): + if not self.is_enabled(): return self.collect() diff --git a/dvc/repo/init.py b/dvc/repo/init.py index 6fefbad8f..19dc20d1b 100644 --- a/dvc/repo/init.py +++ b/dvc/repo/init.py @@ -7,21 +7,23 @@ from dvc.scm import SCM, NoSCM from dvc.config import Config from dvc.exceptions import InitError from dvc.utils import boxify, relpath, remove +from dvc.analytics import Analytics logger = logging.getLogger(__name__) def _welcome_message(): - logger.info( - boxify( - "DVC has enabled anonymous aggregate usage analytics.\n" - "Read the analytics documentation (and how to opt-out) here:\n" - "{blue}https://dvc.org/doc/user-guide/analytics{nc}".format( - blue=colorama.Fore.BLUE, nc=colorama.Fore.RESET - ), - border_color="red", + if Analytics.is_enabled(): + logger.info( + boxify( + "DVC has enabled anonymous aggregate usage analytics.\n" + "Read the analytics documentation (and how to opt-out) here:\n" + "{blue}https://dvc.org/doc/user-guide/analytics{nc}".format( + blue=colorama.Fore.BLUE, nc=colorama.Fore.RESET + ), + border_color="red", + ) ) - ) msg = ( "{yellow}What's next?{nc}\n"