[Setup] Use instance name in default data path (#3171)

* enhance(setup): use instance name in default data path

* chore(changelog): add towncrier entries

* enhance(setup): tell user that instance name is case-sensitive
This commit is contained in:
jack1142
2020-01-02 14:59:22 +01:00
committed by Michael H
parent f3c57b6730
commit f3e7c2028c
5 changed files with 71 additions and 34 deletions

View File

@@ -4,6 +4,7 @@ import json
import logging
import os
import sys
import re
from copy import deepcopy
from pathlib import Path
from typing import Dict, Any, Optional
@@ -59,26 +60,35 @@ def save_config(name, data, remove=False):
json.dump(_config, fs, indent=4)
def get_data_dir():
default_data_dir = Path(appdir.user_data_dir)
def get_data_dir(instance_name: str):
data_path = Path(appdir.user_data_dir) / "data" / instance_name
print()
print(
"We've attempted to figure out a sane default data location which is printed below."
" If you don't want to change this default please press [ENTER],"
" otherwise input your desired data location."
)
print()
print("Default: {}".format(default_data_dir))
print("Default: {}".format(data_path))
new_path = input("> ")
data_path_input = input("> ")
if new_path != "":
new_path = Path(new_path)
default_data_dir = new_path
if data_path_input != "":
data_path = Path(data_path_input)
if not default_data_dir.exists():
try:
exists = data_path.exists()
except OSError:
print(
"We were unable to check your chosen directory."
" Provided path may contain an invalid character."
)
sys.exit(1)
if not exists:
try:
default_data_dir.mkdir(parents=True, exist_ok=True)
data_path.mkdir(parents=True, exist_ok=True)
except OSError:
print(
"We were unable to create your chosen directory."
@@ -87,11 +97,11 @@ def get_data_dir():
)
sys.exit(1)
print("You have chosen {} to be your data directory.".format(default_data_dir))
print("You have chosen {} to be your data directory.".format(data_path))
if not click.confirm("Please confirm", default=True):
print("Please start the process over.")
sys.exit(0)
return str(default_data_dir.resolve())
return str(data_path.resolve())
def get_storage_type():
@@ -113,16 +123,21 @@ def get_storage_type():
return storage
def get_name():
def get_name() -> str:
name = ""
while len(name) == 0:
print()
print(
"Please enter a name for your instance, this name cannot include spaces"
" and it will be used to run your bot from here on out."
"Please enter a name for your instance,"
" it will be used to run your bot from here on out.\n"
"This name is case-sensitive and can only include characters"
" A-z, numbers, underscores, and hyphens."
)
name = input("> ")
if " " in name:
if re.fullmatch(r"[a-zA-Z0-9_\-]*", name) is None:
print(
"ERROR: Instance name can only include"
" characters A-z, numbers, underscores, and hyphens!"
)
name = ""
return name
@@ -134,11 +149,11 @@ def basic_setup():
"""
print(
"Hello! Before we begin the full configuration process we need to"
" gather some initial information about where you'd like us"
" to store your bot's data."
"Hello! Before we begin, we need to gather some initial information for the new instance."
)
default_data_dir = get_data_dir()
name = get_name()
default_data_dir = get_data_dir(name)
default_dirs = deepcopy(data_manager.basic_config_default)
default_dirs["DATA_PATH"] = default_data_dir
@@ -151,7 +166,6 @@ def basic_setup():
driver_cls = drivers.get_driver_class(storage_type)
default_dirs["STORAGE_DETAILS"] = driver_cls.get_config_details()
name = get_name()
if name in instance_data:
print(
"WARNING: An instance already exists with this name. "