[Core V3] Make the bot data path configurable (#879)

* Initial commit

* Fix sentry

* Make cog manager install path work relative to the bot's dir

* Fix downloader to save data relative to the defined data folder

* Fix sentry test

* Fix downloader tests

* Change logfile location

* Add another line to codeowners

* Basic tests

* Fix versioning

* Add in FutureWarning for config file changes

* Add reference to issue
This commit is contained in:
Will
2017-08-20 15:49:51 -04:00
committed by GitHub
parent b7f1d9ed1a
commit 3d76f3a787
13 changed files with 160 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
from core.config import Config
from subprocess import run, PIPE
from collections import namedtuple
from main import determine_main_folder
__all__ = ["Config", "__version__"]
version_info = namedtuple("VersionInfo", "major minor patch")
@@ -9,10 +10,12 @@ BASE_VERSION = version_info(3, 0, 0)
def get_latest_version():
main_folder = determine_main_folder()
try:
p = run(
"git describe --abbrev=0 --tags".split(),
stdout=PIPE
stdout=PIPE,
cwd=str(main_folder)
)
except FileNotFoundError:
# No git

View File

@@ -102,6 +102,9 @@ def parse_cli_flags():
parser.add_argument("--dev",
action="store_true",
help="Enables developer mode")
parser.add_argument("config",
nargs='?',
help="Path to config generated on initial setup.")
args = parser.parse_args()

View File

@@ -270,6 +270,8 @@ class CogManagerUI:
No installed cogs will be transferred in the process.
"""
if path:
if not path.is_absolute():
path = (ctx.bot.main_dir / path).resolve()
try:
await ctx.bot.cog_mgr.set_install_path(path)
except ValueError:

View File

@@ -8,6 +8,7 @@ from copy import deepcopy
from pathlib import Path
from .drivers.red_json import JSON as JSONDriver
from core.data_manager import cog_data_path, core_data_path
log = logging.getLogger("red.config")
@@ -359,6 +360,7 @@ class MemberGroup(Group):
return guild_member.get(self.identifiers[-2], {})
class Config:
"""
You should always use :func:`get_conf` or :func:`get_core_conf` to initialize a Config object.
@@ -431,10 +433,11 @@ class Config:
:return:
A new config object.
"""
cog_name = cog_instance.__class__.__name__
cog_path_override = cog_data_path(cog_instance)
cog_name = cog_path_override.stem
uuid = str(hash(identifier))
spawner = JSONDriver(cog_name)
spawner = JSONDriver(cog_name, data_path_override=cog_path_override)
return cls(cog_name=cog_name, unique_identifier=uuid,
force_registration=force_registration,
driver_spawn=spawner)
@@ -451,8 +454,7 @@ class Config:
See :py:attr:`force_registration`
:type force_registration: Optional[bool]
"""
core_data_path = Path.cwd() / 'core' / '.data'
driver_spawn = JSONDriver("Core", data_path_override=core_data_path)
driver_spawn = JSONDriver("Core", data_path_override=core_data_path())
return cls(cog_name="Core", driver_spawn=driver_spawn,
unique_identifier='0',
force_registration=force_registration)

61
core/data_manager.py Normal file
View File

@@ -0,0 +1,61 @@
from pathlib import Path
from core.json_io import JsonIO
jsonio = None
basic_config = None
basic_config_default = {
"DATA_PATH": None,
"COG_PATH_APPEND": "cogs",
"CORE_PATH_APPEND": "core"
}
def load_basic_configuration(path: Path):
global jsonio
global basic_config
jsonio = JsonIO(path)
basic_config = jsonio._load_json()
def _base_data_path() -> Path:
if basic_config is None:
raise RuntimeError("You must load the basic config before you"
" can get the base data path.")
path = basic_config['DATA_PATH']
return Path(path).resolve()
def cog_data_path(cog_instance=None) -> Path:
"""
Gets the base cog data path. If you want to get the folder with
which to store your own cog's data please pass in an instance
of your cog class.
:param cog_instance:
:return:
"""
try:
base_data_path = Path(_base_data_path())
except RuntimeError as e:
raise RuntimeError("You must load the basic config before you"
" can get the cog data path.") from e
cog_path = base_data_path / basic_config['COG_PATH_APPEND']
if cog_instance:
cog_path = cog_path / cog_instance.__class__.__name__
cog_path.mkdir(exist_ok=True, parents=True)
return cog_path.resolve()
def core_data_path() -> Path:
try:
base_data_path = Path(_base_data_path())
except RuntimeError as e:
raise RuntimeError("You must load the basic config before you"
" can get the core data path.") from e
core_path = base_data_path / basic_config['CORE_PATH_APPEND']
core_path.mkdir(exist_ok=True, parents=True)
return core_path.resolve()

View File

@@ -27,12 +27,12 @@ include_paths = (
client = None
def init_sentry_logging(logger):
def init_sentry_logging(bot, logger):
global client
client = Client(
dsn=("https://27f3915ba0144725a53ea5a99c9ae6f3:87913fb5d0894251821dcf06e5e9cfe6@"
"sentry.telemetry.red/19?verify_ssl=0"),
release=fetch_git_sha(str(Path.cwd()))
release=fetch_git_sha(str(bot.main_dir))
)
breadcrumbs.ignore_logger("websockets")