mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 10:17:59 -05:00
[V3] Update code standards (black code format pass) (#1650)
* ran black: code formatter against `redbot/` with `-l 99` * badge
This commit is contained in:
125
redbot/setup.py
125
redbot/setup.py
@@ -20,7 +20,7 @@ from redbot.core.drivers.red_json import JSON
|
||||
|
||||
config_dir = None
|
||||
appdir = appdirs.AppDirs("Red-DiscordBot")
|
||||
if sys.platform == 'linux':
|
||||
if sys.platform == "linux":
|
||||
if 0 < os.getuid() < 1000:
|
||||
config_dir = Path(appdir.site_data_dir)
|
||||
if not config_dir:
|
||||
@@ -28,27 +28,17 @@ if not config_dir:
|
||||
try:
|
||||
config_dir.mkdir(parents=True, exist_ok=True)
|
||||
except PermissionError:
|
||||
print(
|
||||
"You don't have permission to write to "
|
||||
"'{}'\nExiting...".format(config_dir))
|
||||
print("You don't have permission to write to " "'{}'\nExiting...".format(config_dir))
|
||||
sys.exit(1)
|
||||
config_file = config_dir / 'config.json'
|
||||
config_file = config_dir / "config.json"
|
||||
|
||||
|
||||
def parse_cli_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Red - Discord Bot's instance manager (V3)"
|
||||
)
|
||||
parser = argparse.ArgumentParser(description="Red - Discord Bot's instance manager (V3)")
|
||||
parser.add_argument(
|
||||
"--delete", "-d",
|
||||
help="Interactively delete an instance",
|
||||
action="store_true"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--edit", "-e",
|
||||
help="Interactively edit an instance",
|
||||
action="store_true"
|
||||
"--delete", "-d", help="Interactively delete an instance", action="store_true"
|
||||
)
|
||||
parser.add_argument("--edit", "-e", help="Interactively edit an instance", action="store_true")
|
||||
return parser.parse_known_args()
|
||||
|
||||
|
||||
@@ -79,18 +69,20 @@ def save_config(name, data, remove=False):
|
||||
def get_data_dir():
|
||||
default_data_dir = Path(appdir.user_data_dir)
|
||||
|
||||
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. 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(
|
||||
"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. 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))
|
||||
|
||||
new_path = input('> ')
|
||||
new_path = input("> ")
|
||||
|
||||
if new_path != '':
|
||||
if new_path != "":
|
||||
new_path = Path(new_path)
|
||||
default_data_dir = new_path
|
||||
|
||||
@@ -98,13 +90,14 @@ def get_data_dir():
|
||||
try:
|
||||
default_data_dir.mkdir(parents=True, exist_ok=True)
|
||||
except OSError:
|
||||
print("We were unable to create your chosen directory."
|
||||
" You may need to restart this process with admin"
|
||||
" privileges.")
|
||||
print(
|
||||
"We were unable to create your chosen directory."
|
||||
" You may need to restart this process with admin"
|
||||
" privileges."
|
||||
)
|
||||
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(default_data_dir))
|
||||
if not confirm("Please confirm (y/n):"):
|
||||
print("Please start the process over.")
|
||||
sys.exit(0)
|
||||
@@ -112,10 +105,7 @@ def get_data_dir():
|
||||
|
||||
|
||||
def get_storage_type():
|
||||
storage_dict = {
|
||||
1: "JSON",
|
||||
2: "MongoDB"
|
||||
}
|
||||
storage_dict = {1: "JSON", 2: "MongoDB"}
|
||||
storage = None
|
||||
while storage is None:
|
||||
print()
|
||||
@@ -137,8 +127,10 @@ def get_name():
|
||||
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.")
|
||||
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."
|
||||
)
|
||||
name = input("> ")
|
||||
if " " in name:
|
||||
name = ""
|
||||
@@ -154,41 +146,40 @@ def basic_setup():
|
||||
default_data_dir = get_data_dir()
|
||||
|
||||
default_dirs = deepcopy(basic_config_default)
|
||||
default_dirs['DATA_PATH'] = str(default_data_dir.resolve())
|
||||
default_dirs["DATA_PATH"] = str(default_data_dir.resolve())
|
||||
|
||||
storage = get_storage_type()
|
||||
|
||||
storage_dict = {
|
||||
1: "JSON",
|
||||
2: "MongoDB"
|
||||
}
|
||||
default_dirs['STORAGE_TYPE'] = storage_dict.get(storage, 1)
|
||||
storage_dict = {1: "JSON", 2: "MongoDB"}
|
||||
default_dirs["STORAGE_TYPE"] = storage_dict.get(storage, 1)
|
||||
|
||||
if storage_dict.get(storage, 1) == "MongoDB":
|
||||
from redbot.core.drivers.red_mongo import get_config_details
|
||||
default_dirs['STORAGE_DETAILS'] = get_config_details()
|
||||
|
||||
default_dirs["STORAGE_DETAILS"] = get_config_details()
|
||||
else:
|
||||
default_dirs['STORAGE_DETAILS'] = {}
|
||||
default_dirs["STORAGE_DETAILS"] = {}
|
||||
|
||||
name = get_name()
|
||||
save_config(name, default_dirs)
|
||||
|
||||
print()
|
||||
print("Your basic configuration has been saved. Please run `redbot <name>` to"
|
||||
" continue your setup process and to run the bot.")
|
||||
print(
|
||||
"Your basic configuration has been saved. Please run `redbot <name>` to"
|
||||
" continue your setup process and to run the bot."
|
||||
)
|
||||
|
||||
|
||||
async def json_to_mongo(current_data_dir: Path, storage_details: dict):
|
||||
from redbot.core.drivers.red_mongo import Mongo
|
||||
|
||||
core_data_file = list(current_data_dir.glob("core/settings.json"))[0]
|
||||
m = Mongo("Core", "0", **storage_details)
|
||||
with core_data_file.open(mode="r") as f:
|
||||
core_data = json.loads(f.read())
|
||||
collection = m.get_collection()
|
||||
await collection.update_one(
|
||||
{'_id': m.unique_cog_identifier},
|
||||
update={"$set": core_data["0"]},
|
||||
upsert=True
|
||||
{"_id": m.unique_cog_identifier}, update={"$set": core_data["0"]}, upsert=True
|
||||
)
|
||||
for p in current_data_dir.glob("cogs/**/settings.json"):
|
||||
with p.open(mode="r") as f:
|
||||
@@ -200,14 +191,13 @@ async def json_to_mongo(current_data_dir: Path, storage_details: dict):
|
||||
cog_c = cog_m.get_collection()
|
||||
for ident in list(cog_data.keys()):
|
||||
await cog_c.update_one(
|
||||
{"_id": cog_m.unique_cog_identifier},
|
||||
update={"$set": cog_data[cog_i]},
|
||||
upsert=True
|
||||
{"_id": cog_m.unique_cog_identifier}, update={"$set": cog_data[cog_i]}, upsert=True
|
||||
)
|
||||
|
||||
|
||||
async def mongo_to_json(current_data_dir: Path, storage_details: dict):
|
||||
from redbot.core.drivers.red_mongo import Mongo
|
||||
|
||||
m = Mongo("Core", "0", **storage_details)
|
||||
db = m.db
|
||||
collection_names = await db.collection_names(include_system_collections=False)
|
||||
@@ -250,9 +240,7 @@ async def edit_instance():
|
||||
default_dirs = deepcopy(basic_config_default)
|
||||
|
||||
current_data_dir = Path(instance_data["DATA_PATH"])
|
||||
print(
|
||||
"You have selected '{}' as the instance to modify.".format(selected)
|
||||
)
|
||||
print("You have selected '{}' as the instance to modify.".format(selected))
|
||||
if not confirm("Please confirm (y/n):"):
|
||||
print("Ok, we will not continue then.")
|
||||
return
|
||||
@@ -273,13 +261,11 @@ async def edit_instance():
|
||||
if confirm("Would you like to change the storage type? (y/n):"):
|
||||
storage = get_storage_type()
|
||||
|
||||
storage_dict = {
|
||||
1: "JSON",
|
||||
2: "MongoDB"
|
||||
}
|
||||
storage_dict = {1: "JSON", 2: "MongoDB"}
|
||||
default_dirs["STORAGE_TYPE"] = storage_dict[storage]
|
||||
if storage_dict.get(storage, 1) == "MongoDB":
|
||||
from redbot.core.drivers.red_mongo import get_config_details
|
||||
|
||||
storage_details = get_config_details()
|
||||
default_dirs["STORAGE_DETAILS"] = storage_details
|
||||
|
||||
@@ -297,9 +283,7 @@ async def edit_instance():
|
||||
save_config(selected, {}, remove=True)
|
||||
save_config(name, default_dirs)
|
||||
|
||||
print(
|
||||
"Your basic configuration has been edited"
|
||||
)
|
||||
print("Your basic configuration has been edited")
|
||||
|
||||
|
||||
async def create_backup(selected, instance_data):
|
||||
@@ -317,10 +301,8 @@ async def create_backup(selected, instance_data):
|
||||
os.chdir(str(pth.parent))
|
||||
with tarfile.open(str(backup_file), "w:gz") as tar:
|
||||
tar.add(pth.stem)
|
||||
print("A backup of {} has been made. It is at {}".format(
|
||||
selected, backup_file
|
||||
))
|
||||
|
||||
print("A backup of {} has been made. It is at {}".format(selected, backup_file))
|
||||
|
||||
else:
|
||||
print("Backing up the instance's data...")
|
||||
backup_filename = "redv3-{}-{}.tar.gz".format(
|
||||
@@ -333,12 +315,8 @@ async def create_backup(selected, instance_data):
|
||||
os.chdir(str(pth.parent)) # str is used here because 3.5 support
|
||||
with tarfile.open(str(backup_file), "w:gz") as tar:
|
||||
tar.add(pth.stem) # add all files in that directory
|
||||
print(
|
||||
"A backup of {} has been made. It is at {}".format(
|
||||
selected, backup_file
|
||||
)
|
||||
)
|
||||
|
||||
print("A backup of {} has been made. It is at {}".format(selected, backup_file))
|
||||
|
||||
|
||||
async def remove_instance(selected, instance_data):
|
||||
instance_list = load_existing_config()
|
||||
@@ -370,12 +348,12 @@ async def remove_instance_interaction():
|
||||
print("{}\n".format(instance))
|
||||
print("Please select one of the above by entering its name")
|
||||
selected = input("> ")
|
||||
|
||||
|
||||
if selected not in instance_list.keys():
|
||||
print("That isn't a valid instance!")
|
||||
return
|
||||
instance_data = instance_list[selected]
|
||||
|
||||
|
||||
await create_backup(selected, instance_data)
|
||||
await remove_instance(selected, instance_data)
|
||||
|
||||
@@ -390,6 +368,7 @@ def main():
|
||||
else:
|
||||
basic_setup()
|
||||
|
||||
|
||||
args, _ = parse_cli_args()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user