tooncher 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. if os.path.exists(config_path):
  9. with open(config_path) as f:
  10. config = yaml.load(f.read())
  11. else:
  12. config = {}
  13. if engine_path is None:
  14. if 'engine_path' in config:
  15. engine_path = config['engine_path']
  16. else:
  17. raise Exception('missing path to toontown engine')
  18. accounts = config['accounts'] if 'accounts' in config else []
  19. for account in accounts:
  20. if account['username'] == username:
  21. tooncher.launch(
  22. engine_path=engine_path,
  23. username=account['username'],
  24. password=account['password'],
  25. validate_ssl_certs=validate_ssl_certs,
  26. )
  27. def _init_argparser():
  28. import argparse
  29. argparser = argparse.ArgumentParser(description=None)
  30. argparser.add_argument('username')
  31. argparser.add_argument(
  32. '--config',
  33. '-c',
  34. metavar='path',
  35. dest='config_path',
  36. help='path to config file (default: %(default)s)',
  37. default=os.path.join(os.path.expanduser('~'), '.tooncher'),
  38. )
  39. argparser.add_argument(
  40. '--engine-path',
  41. '-e',
  42. metavar='path',
  43. dest='engine_path',
  44. default=tooncher.TOONTOWN_ENGINE_DEFAULT_PATH,
  45. help='\n'.join([
  46. 'path to toontown engine.',
  47. 'this overrides the one specified in config file',
  48. '(default: %(default)s)',
  49. ]),
  50. )
  51. argparser.add_argument(
  52. '--no-ssl-cert-validation',
  53. '-k',
  54. dest='validate_ssl_certs',
  55. help='do not validate ssl certificates',
  56. action='store_false',
  57. )
  58. return argparser
  59. def main(argv):
  60. argparser = _init_argparser()
  61. try:
  62. import argcomplete
  63. argcomplete.autocomplete(argparser)
  64. except ImportError:
  65. pass
  66. args = argparser.parse_args(argv)
  67. run(**vars(args))
  68. return 0
  69. if __name__ == "__main__":
  70. sys.exit(main(sys.argv[1:]))