tooncher 3.4 KB

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