async def json_to_mongo(current_data_dir: Path, storage_details: dict): from redbot.core.drivers.red_mongo import Mongo core_data_file = current_data_dir / "core" / "settings.json" driver = Mongo(cog_name="Core", identifier="0", **storage_details) with core_data_file.open(mode="r") as f: core_data = json.loads(f.read()) data = core_data.get("0", {}) for key, value in data.items(): await driver.set(key, value=value) for p in current_data_dir.glob("cogs/**/settings.json"): cog_name = p.parent.stem with p.open(mode="r") as f: cog_data = json.load(f) for identifier, data in cog_data.items(): driver = Mongo(cog_name, identifier, **storage_details) for key, value in data.items(): await driver.set(key, value=value)
async def remove_instance(selected, instance_data): if instance_data["STORAGE_TYPE"] == "MongoDB": from redbot.core.drivers.red_mongo import Mongo m = Mongo("Core", **instance_data["STORAGE_DETAILS"]) db = m.db collections = await db.collection_names( include_system_collections=False) for name in collections: collection = await db.get_collection(name) await collection.drop() else: pth = Path(instance_data["DATA_PATH"]) safe_delete(pth) save_config(selected, {}, remove=True) print("The instance {} has been removed\n".format(selected))
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) for c_name in collection_names: if c_name == "Core": c_data_path = current_data_dir / "core" else: c_data_path = current_data_dir / "cogs/{}".format(c_name) output = {} docs = await db[c_name].find().to_list(None) c_id = None for item in docs: item_id = item.pop("_id") if not c_id: c_id = str(hash(item_id)) output[item_id] = item target = JSON(c_name, c_id, data_path_override=c_data_path) await target.jsonIO._threadsafe_save_json(output)
async def mongo_to_json(instance): load_basic_configuration(instance) from redbot.core.drivers.red_mongo import Mongo m = Mongo("Core", "0", **storage_details()) db = m.db collection_names = await db.list_collection_names() for collection_name in collection_names: if "." in collection_name: # Fix for one of Zeph's problems continue elif collection_name == "Core": c_data_path = core_data_path() else: c_data_path = cog_data_path(raw_name=collection_name) c_data_path.mkdir(parents=True, exist_ok=True) # Every cog name has its own collection collection = db[collection_name] async for document in collection.find(): # Every cog has its own document. # This means if two cogs have the same name but different identifiers, they will # be two separate documents in the same collection cog_id = document.pop("_id") if not isinstance(cog_id, str): # Another garbage data check continue elif not str(cog_id).isdigit(): continue driver = JSON(collection_name, cog_id, data_path_override=c_data_path) for category, value in document.items(): ident_data = IdentifierData(str(cog_id), category, (), (), {}) await driver.set(ident_data, value=value) return {}
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.list_collection_names() for collection_name in collection_names: if collection_name == "Core": c_data_path = current_data_dir / "core" else: c_data_path = current_data_dir / "cogs" / collection_name c_data_path.mkdir(parents=True, exist_ok=True) # Every cog name has its own collection collection = db[collection_name] async for document in collection.find(): # Every cog has its own document. # This means if two cogs have the same name but different identifiers, they will # be two separate documents in the same collection cog_id = document.pop("_id") driver = JSON(collection_name, cog_id, data_path_override=c_data_path) for key, value in document.items(): await driver.set(key, value=value)
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) for p in current_data_dir.glob("cogs/**/settings.json"): with p.open(mode="r") as f: cog_data = json.loads(f.read()) cog_i = None for ident in list(cog_data.keys()): cog_i = str(hash(ident)) cog_m = Mongo(p.parent.stem, cog_i, **storage_details) 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)