persistency.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # File: persistency.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. Support module containing code which supports the persistency functionality offered by DOPAL.
  18. '''
  19. _refresh_methods = {
  20. # Simple ones.
  21. 'PluginConfig': lambda pi, obj: \
  22. pi.getPluginconfig(),
  23. 'DownloadManager': lambda pi, obj: \
  24. pi.getDownloadManager(),
  25. 'IPFilter': lambda pi, obj: \
  26. pi.getIPFilter(),
  27. 'ShortCuts': lambda pi, obj: \
  28. pi.getShortCuts(),
  29. 'TorrentManager': lambda pi, obj: \
  30. pi.getTorrentManager(),
  31. 'PluginInterface': lambda pi, obj: \
  32. pi,
  33. # Not so simple ones.
  34. 'Download': lambda pi, obj: \
  35. pi.getShortCuts().getDownload(obj.torrent.hash),
  36. 'Torrent': lambda pi, obj: \
  37. pi.getShortCuts().getDownload(obj.hash).torrent,
  38. }
  39. # XXX: Test and document.
  40. def get_equivalent_object_from_root(original_object, plugin_interface):
  41. import dopal.objects
  42. if not isinstance(original_object, dopal.objects.RemoteObject):
  43. raise ValueError, "%s is not a RemoteObject" % (original_object,)
  44. from dopal.errors import NonRefreshableObjectTypeError, \
  45. MissingRemoteAttributeError, NonRefreshableIncompleteObjectError
  46. remote_type = original_object.get_remote_type()
  47. try:
  48. refresh_function = _refresh_methods[remote_type]
  49. except KeyError:
  50. raise NonRefreshableObjectTypeError(obj=original_object)
  51. try:
  52. return refresh_function(plugin_interface, original_object)
  53. except MissingRemoteAttributeError, error:
  54. raise NonRefreshableIncompleteObjectError(obj=original_object, error=error)