interact.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # File: interact.py
  2. # Library: DOPAL - DO Python Azureus Library
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; version 2 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details ( see the COPYING file ).
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. '''
  17. Interactive Python application which initialises DOPAL to connect with a chosen
  18. Azureus server.
  19. '''
  20. def main():
  21. '''Function to invoke this application.'''
  22. # Get host and port.
  23. connection_details = {}
  24. connection_details['host'] = raw_input('Enter host: ')
  25. port_text = raw_input('Enter port (default is 6884): ')
  26. if port_text:
  27. connection_details['port'] = int(port_text)
  28. # Username and password.
  29. username = raw_input('Enter user name (leave blank if not applicable): ')
  30. password = None
  31. if username:
  32. import getpass
  33. connection_details['user'] = username
  34. connection_details['password'] = getpass.getpass('Enter password: ')
  35. my_locals = {}
  36. from dopal.main import make_connection
  37. connection = make_connection(**connection_details)
  38. connection.is_persistent_connection = True
  39. from dopal.errors import LinkError
  40. try:
  41. interface = connection.get_plugin_interface()
  42. except LinkError, error:
  43. interface = None
  44. connection_error = error
  45. else:
  46. connection_error = None
  47. from dopal import __version_str__
  48. banner = "DOPAL %s - interact module\n\n" % __version_str__
  49. banner += "Connection object stored in 'connection' variable.\n"
  50. if connection_error is None:
  51. banner += "Plugin interface stored in 'interface' variable.\n"
  52. else:
  53. banner += "\nError getting plugin interface object - could not connect to Azureus, error:\n %s" % connection_error.to_error_string()
  54. import dopal
  55. if dopal.__dopal_mode__ == 1:
  56. banner += "\nRunning in DEBUG mode.\n"
  57. elif dopal.__dopal_mode__ == 2:
  58. banner += '\nWARNING: Running in "epydoc" mode.\n'
  59. my_locals['connection'] = connection
  60. if interface is not None:
  61. my_locals['interface'] = interface
  62. my_locals['__import__'] = __import__
  63. print
  64. print '------------------------'
  65. print
  66. import code
  67. code.interact(banner, local=my_locals)
  68. if __name__ == '__main__':
  69. def _main(env):
  70. return main()
  71. import dopal.scripting
  72. dopal.scripting.ext_run(
  73. 'dopal.interact', _main,
  74. make_connection=False,
  75. setup_logging=False,
  76. timeout=8,
  77. pause_on_exit=2,
  78. )