tooncher 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python3
  2. # PYTHON_ARGCOMPLETE_OK
  3. import os
  4. import sys
  5. import tooncher
  6. import tooncher.controls
  7. import yaml
  8. def run(username, config_path, engine_path=None, validate_ssl_certs=True,
  9. cpu_limit_percent=None, enable_extended_keyboard_controls=False,
  10. extended_keyboard_control_toggle_keysym_name=None):
  11. if os.path.exists(config_path):
  12. with open(config_path) as f:
  13. config = yaml.load(f.read())
  14. else:
  15. config = {}
  16. if engine_path is None:
  17. if 'engine_path' in config:
  18. engine_path = config['engine_path']
  19. else:
  20. raise Exception('missing path to toontown engine')
  21. accounts = config['accounts'] if 'accounts' in config else []
  22. for account in accounts:
  23. if account['username'] == username:
  24. tooncher.launch(
  25. engine_path=engine_path,
  26. username=account['username'],
  27. password=account['password'],
  28. validate_ssl_certs=validate_ssl_certs,
  29. cpu_limit_percent=cpu_limit_percent,
  30. enable_extended_keyboard_controls=enable_extended_keyboard_controls,
  31. extended_keyboard_control_toggle_keysym_name=extended_keyboard_control_toggle_keysym_name,
  32. )
  33. def _init_argparser():
  34. import argparse
  35. argparser = argparse.ArgumentParser(description=None)
  36. argparser.add_argument('username')
  37. argparser.add_argument(
  38. '--config',
  39. '-c',
  40. metavar='path',
  41. dest='config_path',
  42. help='path to config file (default: %(default)s)',
  43. default=os.path.join(os.path.expanduser('~'), '.tooncher'),
  44. )
  45. argparser.add_argument(
  46. '--engine-path',
  47. '-e',
  48. metavar='path',
  49. dest='engine_path',
  50. default=tooncher.TOONTOWN_ENGINE_DEFAULT_PATH,
  51. help='\n'.join([
  52. 'path to toontown engine.',
  53. 'this overrides the one specified in config file',
  54. '(default: %(default)s)',
  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. argparser.add_argument(
  73. '--extended-controls',
  74. dest='enable_extended_keyboard_controls',
  75. action='store_true',
  76. help='enable extended keyboard controls'
  77. + ', e.g. walk with WASD'
  78. + ' (requires xlib for python, default: %(default)s)',
  79. )
  80. argparser.add_argument(
  81. '--extended-controls-toggle',
  82. metavar='KEYSYM_NAME',
  83. dest='extended_keyboard_control_toggle_keysym_name',
  84. default=tooncher.controls.EXTENDED_CONTROLS_DEFAULT_TOGGLE_KEYSYM_NAME,
  85. help='key to turn extended keyboard controls on / off.'
  86. + ' any keysym name may be used'
  87. + ' (see XStringToKeysym & X11/keysymdef.h, '
  88. + ' default: %(default)s)',
  89. )
  90. return argparser
  91. def main(argv):
  92. argparser = _init_argparser()
  93. try:
  94. import argcomplete
  95. argcomplete.autocomplete(argparser)
  96. except ImportError:
  97. pass
  98. args = argparser.parse_args(argv)
  99. run(**vars(args))
  100. return 0
  101. if __name__ == "__main__":
  102. sys.exit(main(sys.argv[1:]))