view example.py @ 0:78e4757601ec

origination
author Atul Varma <varmaa@toolness.com>
date Tue, 22 Dec 2009 21:40:25 -0800
parents
children f2147b34d6af
line wrap: on
line source

import os
from datetime import datetime, timedelta
import cPickle as pickle

import bzapi

class Picklable(object):
    def __init__(self, filename, constructor, **kwargs):
        self.__filename = filename

        if not os.path.exists(filename):
            self.__obj = constructor(**kwargs)
            self.save()
        else:
            obj_file = open(filename, 'r')
            self.__obj = pickle.load(obj_file)
            obj_file.close()

    def save(self):
        obj_file = open(self.__filename, 'w')
        pickle.dump(self.__obj, obj_file)
        obj_file.close()

    def __getattr__(self, attr):
        if not attr.startswith('_Picklable_'):
            return getattr(self.__obj, attr)
        else:
            return self.__dict__[attr]

    def __setattr__(self, attr, value):
        if not attr.startswith('_Picklable_'):
            setattr(self.__obj, attr, value)
        else:
            self.__dict__[attr] = value

api = Picklable(
    'example.api.pickle',
    bzapi.BugzillaApi,
    base_url = 'https://api-dev.bugzilla.mozilla.org/latest'
    )

search = Picklable(
    'example.cached-search.pickle',
    bzapi.CachedSearch,
    api = api,
    product='Mozilla Labs',
    component='Jetpack'
    )

#search.update(api)

#print len([bug for bug in search.bugs.itervalues()])

#print search.bugs.values()[12]['summary']

#print api.get('/bug/510339/history')

#print api.get('/bug',
#              product='Mozilla Labs', component='Jetpack',
#              changed_after=now-timedelta(minutes=60))