main.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # File: main.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. Main module for using DOPAL with little effort.
  18. '''
  19. def make_connection(persistent=False, **kwargs):
  20. '''
  21. Generate a L{DopalConnection} to an Azureus server, whose location is
  22. specified by keyword arguments.
  23. To see what keywords are accepted, see the
  24. L{set_link_details<dopal.core.AzureusLink.set_link_details>} method. This
  25. method also takes an additional keyword - C{persistent}, which determines
  26. whether the connection should be persistent or not (by default, it is not
  27. persistent).
  28. This function will return a DopalConnection instance.
  29. @rtype: L{DopalConnection}
  30. @see: L{set_link_details<dopal.core.AzureusLink.set_link_details>}
  31. '''
  32. connection = DopalConnection()
  33. connection.set_link_details(**kwargs)
  34. if not persistent:
  35. connection.is_persistent_connection = False
  36. return connection
  37. from dopal.objects import AzureusObjectConnection
  38. class DopalConnection(AzureusObjectConnection):
  39. '''
  40. A subclass of
  41. L{AzureusObjectConnection<dopal.objects.AzureusObjectConnection>} which
  42. contains an extended API.
  43. This class defines an extended API, similar to the way that C{Dopal}
  44. classes contain additional methods compared to their C{Azureus}
  45. counterparts. It also sets up some different default behaviours (compared
  46. to L{AzureusObjectConnection<dopal.objects.AzureusObjectConnection>}):
  47. - All instances are I{persistent} connections by default.
  48. - A L{RemoteObjectConverter<dopal.convert.RemoteObjectConverter>}
  49. instance is installed as the default handler for converting XML to
  50. its appropriate object representation.
  51. - The L{DOPAL class map<dopal.obj_impl.DOPAL_CLASS_MAP>} is used as the
  52. standard class mapping.
  53. @see: The L{obj_impl<dopal.obj_impl>} module documentation.
  54. '''
  55. def __init__(self):
  56. super(DopalConnection, self).__init__()
  57. from dopal.convert import RemoteObjectConverter
  58. converter = RemoteObjectConverter(self)
  59. from dopal.obj_impl import DOPAL_CLASS_MAP
  60. converter.class_map = DOPAL_CLASS_MAP
  61. self.converter = converter
  62. self.is_persistent_connection = True
  63. del AzureusObjectConnection