Esempio n. 1
0
def test_get_config_wrong(monkeypatch: pytest.MonkeyPatch):
    def wrong_config(*args, **kargs):
        return {"diskord_token": "0000"}

    monkeypatch.setattr(yaml, "load", wrong_config)
    with pytest.raises(TypeError) as excinfo:
        utils.get_config()
    assert "unexpected keyword argument 'diskord_token'" in str(excinfo.value)
Esempio n. 2
0
def test_get_config_missing(monkeypatch: pytest.MonkeyPatch):
    def missing_config(*args, **kargs):
        return {"discord_token": "0000", "imgur_client_id": "0000"}

    monkeypatch.setattr(yaml, "load", missing_config)
    with pytest.raises(TypeError) as excinfo:
        utils.get_config()
    assert "missing 1 required positional argument: 'mtgjson_path'" in str(
        excinfo.value
    )
Esempio n. 3
0
def generator() -> MtgPackGenerator:
    config = get_config()
    return MtgPackGenerator(
        path_to_mtgjson=config.mtgjson_path,
        path_to_jmp=config.jmp_decklists_path,
        jmp_arena=True,
    )
Esempio n. 4
0
def test_get_config(
    monkeypatch: pytest.MonkeyPatch, temp_config: utils.Config
):
    def config_dict(*args, **kargs):
        return dataclasses.asdict(temp_config)

    monkeypatch.setattr(yaml, "load", config_dict)
    config = utils.get_config()
    assert config.discord_token == "0000"
    assert config.imgur_client_id == "0000"
    assert config.mtgjson_path.endswith("AllPrintings.json")
    assert config.jmp_decklists_path and config.jmp_decklists_path.endswith(
        "JMP"
    )
    assert config.set_img_path is None
    assert config.command_prefix == "!"
    assert config.logging_level == 20  # logging.INFO
Esempio n. 5
0
    size: str = "large",
    rarity: str = "M",
) -> None:
    if local_path.is_dir():
        print(f"Set images will be downloaded in: {local_path.as_posix()}")
        generator = MtgPackGenerator(
            path_to_mtgjson=path_to_mtgjson.as_posix())
        for set in generator.sets_with_boosters:
            set = set.lower()
            try:
                im = imageio.imread(
                    IMAGE_URL.format(size=size, rarity=rarity, code=set))
                print(f"{set}\tOK")
                imageio.imwrite((local_path / f"{set}.png").as_posix(), im)
            except ValueError:
                print(f"{set}\tX")
    else:
        print(f"The directory {local_path.as_posix()} does not exist.")


if __name__ == "__main__":
    print("Reading config...")
    config = get_config()
    if config.set_img_path:
        main(
            local_path=Path(config.set_img_path),
            path_to_mtgjson=Path(config.mtgjson_path),
        )
    else:
        print("Config does not contain set_img_path setting, doing nothing.")
Esempio n. 6
0
def main():
    parser = argparse.ArgumentParser(
        prog="boostertutor",
        description=(
            "A Discord bot to generate 'Magic: the Gathering' boosters and "
            "sealed pools."),
        epilog=(
            "Run without subcommands to run the Discord bot, or add one of "
            "the subcommands to run the downloader subutils (some subcommand "
            "might have specific arguments, check subcommands' help for them)."
        ),
    )
    parser.add_argument(
        "--config",
        help="config file path (default: ./config.yaml)",
        default="config.yaml",
    )
    subparsers = parser.add_subparsers(
        title="Donwloaders",
        description=(
            "Utilities to download helpful data. When a downloader is run, "
            "the Discord bot is not started."),
        dest="downloader",
    )
    subparsers.add_parser(
        "symbols",
        help="Download set symbols",
        description="Download set symbols",
    )
    parser_mtgjson_downloader = subparsers.add_parser(
        "mtgjson",
        help="Download MTGJSON data",
        description="Download MTGJSON data",
    )
    parser_mtgjson_downloader.add_argument("--jmp",
                                           help="download JMP decks",
                                           action="store_true")
    parser_mtgjson_downloader.add_argument("--jmp-backup",
                                           help="backup old JMP decks",
                                           action="store_true")
    args = parser.parse_args()
    config = get_config(Path(args.config))
    file_handler = RotatingFileHandler("boostertutor.log",
                                       maxBytes=1024 * 1024 * 50)
    console_handler = logging.StreamHandler()
    logging.basicConfig(
        level=config.logging_level,
        format="%(asctime)s:%(levelname)s:%(name)s: %(message)s",
        handlers=[file_handler, console_handler],
    )

    if args.downloader == "symbols":
        if config.set_img_path:
            symbols.main(
                local_path=Path(config.set_img_path),
                path_to_mtgjson=Path(config.mtgjson_path),
            )
        else:
            logging.error(
                "Config does not contain set_img_path setting, doing nothing.")
    elif args.downloader == "mtgjson":
        mtgjson.main(config, jmp=args.jmp, jmp_backup=args.jmp_backup)
    else:
        bot = DiscordBot(config)
        bot.run(config.discord_token)
            download_jmp_decks(
                dir=config.jmp_decklists_path,  # type: ignore
                temp_dir=Path(config.jmp_decklists_path  # type: ignore
                              ).parent.as_posix(),
                backup=jmp_backup,
            )
    else:
        print(f"The directory {Path(config.mtgjson_path).parent.as_posix()} "
              "does not exist.")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Downloads MTGJSON data.")
    parser.add_argument(
        "--config",
        help="config file path (default: ./config.yaml)",
        default="config.yaml",
    )
    parser.add_argument("--jmp",
                        help="download JMP decks",
                        action="store_true")
    parser.add_argument("--jmp-backup",
                        help="backup old JMP decks",
                        action="store_true")
    args = parser.parse_args()

    print("Reading config...")
    config = get_config(args.config)

    main(config, args.jmp, args.jmp_backup)