From 8d4e27805b7bbe70a100e8fe41f45939f1a80c05 Mon Sep 17 00:00:00 2001 From: y0z Date: Thu, 16 May 2024 19:05:08 +0900 Subject: [PATCH] Remove config file --- optunahub/_conf.py | 83 ++-------------------------------------------- optunahub/hub.py | 69 +++++--------------------------------- pyproject.toml | 1 - 3 files changed, 10 insertions(+), 143 deletions(-) diff --git a/optunahub/_conf.py b/optunahub/_conf.py index 81c8c1a..43dced8 100644 --- a/optunahub/_conf.py +++ b/optunahub/_conf.py @@ -2,77 +2,6 @@ import os import platform -from typing import Any - -import toml - - -def config_file() -> str: - """Return the path to the configuration file. - - Returns: - The path to the configuration file. - """ - if platform.system() == "Windows": # NOTE: unverified - return os.path.join( - os.getenv("APPDATA", os.path.join(os.path.expanduser("~"), "AppData", "Roaming")), - "optunahub", - "config.toml", - ) - else: # UNIX-like - return os.path.join( - os.getenv("XDG_CONFIG_HOME", os.path.join(os.path.expanduser("~"), ".config")), - "optunahub", - "config.toml", - ) - - -def get_config_value(key: str) -> Any: - """Return the value of a configuration key. - - Args: - key: - The configuration key. - - Returns: - The value of the configuration key. - """ - cf = config_file() - if os.path.isfile(cf): - with open(cf, "r") as f: - conf = toml.load(f) - if key in conf: - return conf[key] - return None - - -def set_config_value(key: str, value: Any) -> bool: - """Set the value of a configuration key. - - Args: - key: - The configuration key. - value: - The value of the configuration key. - - Returns: - `True` if the value was set or updated, `False` if the value was not updated. - """ - - conf = {} - cf = config_file() - if os.path.isfile(cf): - with open(cf, "r") as f: - conf = toml.load(f) - - if key in conf and conf[key] == value: - return False - - conf[key] = value - os.makedirs(os.path.dirname(cf), exist_ok=True) - with open(cf, "w") as f: - toml.dump(conf, f) - return True def cache_home() -> str: @@ -86,10 +15,6 @@ def cache_home() -> str: if optunahub_cache_home_env is not None: return optunahub_cache_home_env - conf_value = get_config_value("cache_home") - if conf_value is not None: - return conf_value - if platform.system() == "Windows": # NOTE: unverified return os.path.join( os.getenv( @@ -106,15 +31,11 @@ def cache_home() -> str: ) -def no_analytics() -> bool | None: +def is_no_analytics() -> bool | None: """Return whether the analytics is disabled. Returns: `True` if the analytics is disabled, `False` if the analytics is enabled, or `None` if the configuration is not set. """ - no_analytics_env = os.getenv("OPTUNAHUB_NO_ANALYTICS") - if no_analytics_env is not None: - return no_analytics_env == "1" - - return get_config_value("no_analytics") + return os.getenv("OPTUNAHUB_NO_ANALYTICS", "0") == "1" diff --git a/optunahub/hub.py b/optunahub/hub.py index 1053509..d39b26a 100644 --- a/optunahub/hub.py +++ b/optunahub/hub.py @@ -109,56 +109,9 @@ def _import_github_dir( return module, use_cache -def _check_analytics_optout() -> bool: - """Check if the user has opted out of analytics. - - The analytics can be disabled in the following ways: - 1. setting the environment variable `OPTUNAHUB_NO_ANALYTICS` to "1", - 2. setting the value of `no_analytics` to `true` in the config file. - - Returns: - `True` if the user has opted out of analytics. - """ - - optunahub_no_analytics_env = os.getenv("OPTUNAHUB_NO_ANALYTICS") - if optunahub_no_analytics_env is not None: - return optunahub_no_analytics_env == "1" - - is_no_analytics = _conf.get_config_value("no_analytics") - if is_no_analytics is not None: - return is_no_analytics - - print( - "Would you allow OptunaHub to collect anonymous usage statistics?\n" - "This information helps the Optuna team improve OptunaHub. [Yes/No]" - ) - while is_no_analytics is None: - ans = input() - if ans.lower() in ["yes", "y"]: - is_no_analytics = False - elif ans.lower() in ["no", "n"]: - is_no_analytics = True - else: - print("Please answer Yes (Y/yes/y) or No (N/no/n).") - - _conf.set_config_value("no_analytics", is_no_analytics) - _conf.set_config_value("no_analytics_version", optunahub.__version__) - print( - f'Your preference is saved to "{_conf.config_file()}".\n' - 'You can change the setting later by editing the "no_analytics" value ' - "in the config file." - ) - - return is_no_analytics - - def _report_stats( package: str, - repo_owner: str, - repo_name: str, - registry_root: str, ref: str | None, - base_url: str, ) -> None: """Report statistics to Google Analytics. @@ -177,18 +130,6 @@ def _report_stats( base_url: The base URL for the GitHub API. """ - - # Statistics are collected only for the official registry. - if ( - repo_owner != "optuna" - or repo_name != "optunahub-registry" - or base_url != "https://api.github.com" - ): - return - - if _check_analytics_optout(): - return - ga = GtagMP( measurement_id="xxx", api_secret="xxx", @@ -267,8 +208,14 @@ def load_module( auth=auth, ) - if not is_cache: - _report_stats(package, repo_owner, repo_name, registry_root, ref, base_url) + # Statistics are collected only for the official registry. + is_official_registry = ( + repo_owner == "optuna" + and repo_name == "optunahub-registry" + and base_url == "https://api.github.com" + ) + if not _conf.is_no_analytics() and not is_cache and is_official_registry: + _report_stats(package, ref) return module diff --git a/pyproject.toml b/pyproject.toml index f7ed790..0b73713 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,6 @@ dependencies = [ "ga4mp", "optuna", "PyGithub", - "toml", ] dynamic = ["version"]