_cli.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import argparse
  3. import yaml
  4. import tooncher
  5. def run(
  6. username,
  7. config_path,
  8. engine_path=None,
  9. validate_ssl_certs=True,
  10. cpu_limit_percent=None,
  11. ):
  12. if os.path.exists(config_path):
  13. with open(config_path) as config_file:
  14. config = yaml.safe_load(config_file.read())
  15. else:
  16. config = {}
  17. if engine_path is None:
  18. if "engine_path" in config:
  19. engine_path = config["engine_path"]
  20. else:
  21. raise Exception("missing path to toontown engine")
  22. accounts = config["accounts"] if "accounts" in config else []
  23. for account in accounts:
  24. if account["username"] == username:
  25. tooncher.launch(
  26. engine_path=engine_path,
  27. username=account["username"],
  28. password=account["password"],
  29. validate_ssl_certs=validate_ssl_certs,
  30. cpu_limit_percent=cpu_limit_percent,
  31. )
  32. def _init_argparser():
  33. argparser = argparse.ArgumentParser(description=None)
  34. argparser.add_argument("username")
  35. argparser.add_argument(
  36. "--config",
  37. "-c",
  38. metavar="path",
  39. dest="config_path",
  40. help="path to config file (default: %(default)s)",
  41. default=os.path.join(os.path.expanduser("~"), ".tooncher"),
  42. )
  43. argparser.add_argument(
  44. "--engine-path",
  45. "-e",
  46. metavar="path",
  47. dest="engine_path",
  48. default=tooncher.TOONTOWN_ENGINE_DEFAULT_PATH,
  49. help="\n".join(
  50. [
  51. "path to toontown engine.",
  52. "this overrides the one specified in config file",
  53. "(default: %(default)s)",
  54. ]
  55. ),
  56. )
  57. argparser.add_argument(
  58. "--no-ssl-cert-validation",
  59. "-k",
  60. dest="validate_ssl_certs",
  61. help="do not validate ssl certificates",
  62. action="store_false",
  63. )
  64. argparser.add_argument(
  65. "--cpu-limit",
  66. dest="cpu_limit_percent",
  67. type=int,
  68. default=None,
  69. help="maximally allowed cpu usage in percent"
  70. + " (requires cpulimit command, default: %(default)s)",
  71. )
  72. return argparser
  73. def main() -> None:
  74. argparser = _init_argparser()
  75. args = argparser.parse_args()
  76. run(**vars(args))