_cli.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import argparse
  2. import os
  3. import pathlib
  4. import sys
  5. import yaml
  6. import tooncher
  7. if ( # pylint: disable=consider-ternary-expression; bulky code format after black
  8. sys.platform == "darwin"
  9. ):
  10. _TOONTOWN_ENGINE_DEFAULT_PATH = os.path.join(
  11. os.path.expanduser("~"),
  12. "Library",
  13. "Application Support",
  14. "Toontown Rewritten",
  15. "Toontown Rewritten",
  16. )
  17. else:
  18. _TOONTOWN_ENGINE_DEFAULT_PATH = None
  19. def run(
  20. username,
  21. config_path,
  22. engine_path=None,
  23. validate_ssl_certs=True,
  24. cpu_limit_percent=None,
  25. ) -> None:
  26. if os.path.exists(config_path):
  27. with open(config_path, encoding="utf8") as config_file:
  28. config = yaml.safe_load(config_file.read())
  29. else:
  30. config = {}
  31. if engine_path is None:
  32. if "engine_path" in config:
  33. engine_path = config["engine_path"]
  34. else:
  35. engine_path = _TOONTOWN_ENGINE_DEFAULT_PATH
  36. if engine_path is None:
  37. raise ValueError(
  38. "missing path to toontown engine\n"
  39. + "pass --engine-path, set $TOONCHER_ENGINE_PATH, or add to config file"
  40. )
  41. accounts = [a for a in config.get("accounts", []) if a["username"] == username]
  42. if not accounts:
  43. raise ValueError(f"username {username!r} was not found in config file")
  44. if len(accounts) > 1:
  45. raise ValueError(f"multiple entries for username {username!r} in config file")
  46. tooncher.launch(
  47. engine_path=pathlib.Path(engine_path),
  48. username=accounts[0]["username"],
  49. password=accounts[0]["password"],
  50. validate_ssl_certs=validate_ssl_certs,
  51. cpu_limit_percent=cpu_limit_percent,
  52. )
  53. class _EnvDefaultArgparser(argparse.ArgumentParser):
  54. def add_argument(self, *args, envvar=None, **kwargs):
  55. # pylint: disable=arguments-differ; using *args & **kwargs to catch all
  56. if envvar:
  57. envvar_value = os.environ.get(envvar, None)
  58. if envvar_value:
  59. kwargs["required"] = False
  60. kwargs["default"] = envvar_value
  61. super().add_argument(*args, **kwargs)
  62. def _init_argparser() -> _EnvDefaultArgparser:
  63. argparser = _EnvDefaultArgparser(description=None)
  64. argparser.add_argument("username")
  65. argparser.add_argument(
  66. "--config",
  67. "-c",
  68. metavar="path",
  69. dest="config_path",
  70. help="path to config file (default: %(default)s)",
  71. default=os.path.join(os.path.expanduser("~"), ".tooncher"),
  72. )
  73. argparser.add_argument(
  74. "--engine-path",
  75. "-e",
  76. metavar="path",
  77. dest="engine_path",
  78. envvar="TOONCHER_ENGINE_PATH",
  79. default=None,
  80. help="path to toontown engine (overrides path in config file, "
  81. + "may also be set via env var $TOONCHER_ENGINE_PATH)",
  82. )
  83. argparser.add_argument(
  84. "--no-ssl-cert-validation",
  85. "-k",
  86. dest="validate_ssl_certs",
  87. help="do not validate ssl certificates",
  88. action="store_false",
  89. )
  90. argparser.add_argument(
  91. "--cpu-limit",
  92. dest="cpu_limit_percent",
  93. type=int,
  94. default=None,
  95. help="maximally allowed cpu usage in percent"
  96. + " (requires cpulimit command, default: %(default)s)",
  97. )
  98. return argparser
  99. def main() -> None:
  100. argparser = _init_argparser()
  101. args = argparser.parse_args()
  102. run(**vars(args))