Skip to content

Commit

Permalink
Merge pull request #14 from optuna/feature/remove_config_file
Browse files Browse the repository at this point in the history
Remove config file
  • Loading branch information
y0z authored May 16, 2024
2 parents d4b01ab + 8d4e278 commit e802268
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 143 deletions.
83 changes: 2 additions & 81 deletions optunahub/_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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"
69 changes: 8 additions & 61 deletions optunahub/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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",
Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies = [
"ga4mp",
"optuna",
"PyGithub",
"toml",
]
dynamic = ["version"]

Expand Down

0 comments on commit e802268

Please sign in to comment.