Thanks. Patches typically go on the tracker at bugs.gpodder.org. You should
probably post this there ;)
Hilton
--
"People are like stained glass windows. They sparkle and shine when the sun
is out, but when the darkness sets in, their true beauty is revealed only if
there is a light from within."
Elisabeth Kubler-Ross
On Mon, Jun 20, 2011 at 3:13 PM, Neal H. Walfield <[email protected]> wrote:
> Well, that's rather embarrassing... There are a couple of obvious
> bugs in the previous patch. I've fixed them and (more throughly)
> tested the change.
>
> From 42924431d277040eb3f6bd01db0ac6e61a6b0582 Mon Sep 17 00:00:00 2001
> From: Neal H. Walfield <[email protected]>
> Date: Mon, 20 Jun 2011 17:48:36 +0200
> Subject: [PATCH] Make podcasts and episodes singletons.
>
> - Manage the singletons using weak references to limit memory use.
> ---
> src/gpodder/model.py | 50
> ++++++++++++++++++++++++++++++++++++++++++++------
> 1 files changed, 44 insertions(+), 6 deletions(-)
>
> diff --git a/src/gpodder/model.py b/src/gpodder/model.py
> index fd0d5a3..72a56d8 100644
> --- a/src/gpodder/model.py
> +++ b/src/gpodder/model.py
> @@ -39,6 +39,7 @@ import datetime
> import rfc822
> import hashlib
> import feedparser
> +from weakref import WeakValueDictionary
>
> _ = gpodder.gettext
>
> @@ -82,28 +83,65 @@ class PodcastModelObject(object):
> """
> A generic base class for our podcast model providing common helper
> and utility functions.
> - """
>
> + PodcastEpisode and PodcastChannel inherit from this class to
> + facilitate instantiation and wholesale updates of attributes.
> +
> + This base class also ensures that only a single instance of an
> + object with the same type and id exists.
> + """
> @classmethod
> def create_from_dict(cls, d, *args):
> """
> - Create a new object, passing "args" to the constructor
> - and then updating the object with the values from "d".
> + Return the python object for the object instance of tyep cls
> + with id d['id'].
> +
> + If the object does not yet exist, instantiates it and passes
> + "args" to the constructor.
> +
> + Update the object's attributes with the values from "d".
> """
> - o = cls(*args)
> + id = d['id']
> +
> + if not hasattr (cls, '_singletons'):
> + cls._singletons = WeakValueDictionary()
> +
> + o = cls._singletons.get (id, None)
> + if o is None:
> + o = cls(*args)
> + cls._singletons[id] = o
> +
> o.update_from_dict(d)
> +
> return o
>
> + def __setattr__(self, name, value):
> + """
> + If changing the id attribute, update the singleton map
> + appropriately. Either way, set the attribute as usual.
> + """
> + if name == 'id':
> + old_value = self.__dict__.get ('id', None)
> + if value != old_value:
> + # The identity changed. Move the singleton.
> + if old_value is not None:
> + del self.__class__._singletons[old_value]
> + if value is not None:
> + self.__class__._singletons[value] = self
> +
> + super (PodcastModelObject, self).__setattr__ (name, value)
> +
> def update_from_dict(self, d):
> """
> Updates the attributes of this object with values from the
> - dictionary "d" by using the keys found in "d".
> + dictionary "d" by using the keys found in "d". Any keys in
> + "d" that are not attributes of the object are silently
> + ignored.
> """
> for k in d:
> if hasattr(self, k):
> setattr(self, k, d[k])
>
> -
> class PodcastEpisode(PodcastModelObject):
> """holds data for one object in a channel"""
> MAX_FILENAME_LENGTH = 200
> --
> 1.7.2.5
>
> _______________________________________________
> gpodder-devel mailing list
> [email protected]
> https://lists.berlios.de/mailman/listinfo/gpodder-devel
>
_______________________________________________
gpodder-devel mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/gpodder-devel