Merge lp:~robru/gwibber/expiry into lp:~barry/gwibber/py3
Proposed by
Robert Bruce Park
Status: | Merged |
---|---|
Merged at revision: | 1426 |
Proposed branch: | lp:~robru/gwibber/expiry |
Merge into: | lp:~barry/gwibber/py3 |
Diff against target: |
31 lines (+17/-0) 1 file modified
gwibber/gwibber/utils/avatar.py (+17/-0) |
To merge this branch: | bzr merge lp:~robru/gwibber/expiry |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Barry Warsaw | Pending | ||
Review via email: mp+123809@code.launchpad.net |
Description of the change
Just a simple cache expiry method that scans the CACHE_DIR for any files older than 1 month and then deletes them.
To post a comment you must log in.
On Sep 11, 2012, at 06:56 PM, Robert Bruce Park wrote:
>Robert Bruce Park has proposed merging lp:~robru/gwibber/expiry into lp:~barry/gwibber/py3. /code.launchpad .net/~robru/ gwibber/ expiry/ +merge/ 123809
>
>Requested reviews:
> Barry Warsaw (barry)
>
>For more details, see:
>https:/
>
>Just a simple cache expiry method that scans the CACHE_DIR for any files
>older than 1 month and then deletes them.
This does need a test, and one is possible, but datetime will have to be old_avatars( )
mocked so that you can fiddle with the dates to see if expire_
does the right thing.
=== modified file 'gwibber/ gwibber/ utils/avatar. py' gwibber/ utils/avatar. py 2012-09-10 22:04:24 +0000 gwibber/ utils/avatar. py 2012-09-11 18:55:42 +0000 savev(local_ path, 'png', [], []) old_avatars( ): date.today( ) - datetime. timedelta( weeks=4)
--- gwibber/
+++ gwibber/
> @@ -22,6 +22,10 @@
> import os
> import errno
> import logging
> +import datetime
> +import calendar
> +
> +from glob import glob
>
> from gi.repository import Gio, GLib, GdkPixbuf
> from hashlib import sha1
> @@ -67,3 +71,16 @@
> input_stream, 48, 48, True, None)
> pixbuf.
> return local_path
> +
> + @staticmethod
> + def expire_
> + # TODO actually call this from somewhere.
> + limit = datetime.
It's possible we'll want the cache lifetime to be configurable at some point,
but I guess we can worry about that later.
Also, IME expiry arithmetic is easier to follow if it's done slightly
differently. With the above code, and the following condition, you have to
sort of stop and think about the meaning. If instead you *added* 4 weeks to
the file's mtime, and then compared that to see if it was greater than today,
it's typically easier to follow the logic. I've done it both ways over the
years, and the latter just seems to be more readable.
> + limit = calendar. timegm( limit.timetuple ()) path.join( CACHE_DIR, '*')):
> +
> + for cached_file in glob(os.
Rather than using glob() on '*', os.listdir() is probably faster and more
idiomatic.
> + try: cached_ file).st_ mtime < limit: cached_ file)
> + if os.stat(
> + os.unlink(
> + except OSError:
> + pass
What OSErrors do you expect to suppress? Until Python 3.3 comes out, it's
always a good idea to reraise errnos you're not expecting. E.g. in this case,
you can ignore ENOENTs, but anything else would be unexpected.
Thanks for the branch. I'll write a test and merge.