Mercurial > bzapi
comparison bzapi.py @ 1:f2147b34d6af
Use pymongo instead of pickling
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 23 Dec 2009 02:23:11 -0800 |
parents | 78e4757601ec |
children | 6a0ad0463a89 |
comparison
equal
deleted
inserted
replaced
0:78e4757601ec | 1:f2147b34d6af |
---|---|
1 import urllib2 | 1 import urllib2 |
2 import urllib | 2 import urllib |
3 from datetime import datetime | 3 from datetime import datetime |
4 | 4 |
5 import pymongo | |
5 import simplejson as json | 6 import simplejson as json |
7 | |
8 def datetime_to_iso(dt): | |
9 return "%sZ" % (dt.replace(microsecond=0).isoformat('T')) | |
6 | 10 |
7 def datetime_from_iso(timestamp): | 11 def datetime_from_iso(timestamp): |
8 return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ') | 12 return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ') |
9 | 13 |
14 def sanitize(obj): | |
15 if type(obj) == dict: | |
16 bad_names = [name for name in obj | |
17 if "." in name] | |
18 for name in bad_names: | |
19 new_name = name.replace('.', '_DOT_') | |
20 obj[new_name] = obj[name] | |
21 del obj[name] | |
22 for name in obj: | |
23 sanitize(obj[name]) | |
24 elif type(obj) == list: | |
25 for item in obj: | |
26 sanitize(item) | |
27 | |
10 class CachedSearch(object): | 28 class CachedSearch(object): |
11 def __init__(self, api, **kwargs): | 29 def __init__(self, api, collection, **kwargs): |
12 self.options = kwargs | 30 self.options = kwargs |
13 self.last_update = None | |
14 self.bugs = {} | |
15 self.update(api) | |
16 | 31 |
17 def update(self, api): | 32 self.bugs = collection |
33 self.api = api | |
34 self._update_last_update() | |
35 | |
36 def _update_last_update(self): | |
37 bugs = self.bugs.find().sort("last_change_time", | |
38 pymongo.DESCENDING).limit(1) | |
39 if bugs.count() == 0: | |
40 self.last_update = None | |
41 else: | |
42 self.last_update = bugs[0]['last_change_time'] | |
43 | |
44 def update(self): | |
18 params = {} | 45 params = {} |
19 params.update(self.options) | 46 params.update(self.options) |
20 if self.last_update: | 47 if self.last_update: |
21 params['changed_after'] = self.last_update | 48 params['changed_after'] = self.last_update |
22 bugs = api.get('/bug', **params)['bugs'] | 49 bugs = self.api.get('/bug', **params)['bugs'] |
23 latest_time = None | |
24 for bug in bugs: | 50 for bug in bugs: |
25 last_change_time = datetime_from_iso(bug['last_change_time']) | 51 for name in ['last_change_time', 'creation_time']: |
26 if not latest_time or last_change_time > latest_time: | 52 bug[name] = datetime_from_iso(bug[name]) |
27 latest_time = last_change_time | 53 bug['_id'] = bug['id'] |
28 self.bugs[bug['id']] = bug | 54 self.bugs.save(bug) |
29 self.last_update = last_change_time | 55 self._update_last_update() |
30 | 56 |
31 class BugzillaApi(object): | 57 class BugzillaApi(object): |
32 def __init__(self, base_url, username=None, password=None): | 58 def __init__(self, base_url, collection, username=None, password=None): |
33 self.base_url = base_url | 59 self.base_url = base_url |
34 self.username = username | 60 self.username = username |
35 self.password = password | 61 self.password = password |
36 self.config = self.get('/configuration') | 62 config = collection.find_one() |
63 if not config: | |
64 config = self.get('/configuration') | |
65 sanitize(config) | |
66 collection.insert(config) | |
67 self.config = config | |
37 | 68 |
38 def _validate_component(self, product, component=None): | 69 def _validate_component(self, product, component=None): |
39 products = self.config['product'] | 70 products = self.config['product'] |
40 if product not in products: | 71 if product not in products: |
41 msg = 'product %s not in configuration' % repr(product) | 72 msg = 'product %s not in configuration' % repr(product) |
48 raise ValueError(msg) | 79 raise ValueError(msg) |
49 | 80 |
50 def get(self, url, **kwargs): | 81 def get(self, url, **kwargs): |
51 for name, value in kwargs.items(): | 82 for name, value in kwargs.items(): |
52 if isinstance(value, datetime): | 83 if isinstance(value, datetime): |
53 kwargs[name] = "%sZ" % ( | 84 kwargs[name] = datetime_to_iso(value) |
54 value.replace(microsecond=0).isoformat('T') | |
55 ) | |
56 | 85 |
86 params = {} | |
57 if self.username and self.password: | 87 if self.username and self.password: |
58 params = dict(username = self.username, | 88 params.update({'username': self.username, |
59 password = self.password) | 89 'password': self.password}) |
60 params.update(kwargs) | 90 params.update(kwargs) |
61 | 91 |
62 if 'product' in params: | 92 if 'product' in params: |
63 self._validate_component(params['product'], | 93 self._validate_component(params['product'], |
64 params.get('component')) | 94 params.get('component')) |