| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import csv | |
| 6 import logging | |
| 7 import operator | |
| 8 from StringIO import StringIO | |
| 9 | |
| 10 import object_store | |
| 11 from url_constants import OPEN_ISSUES_CSV_URL, CLOSED_ISSUES_CSV_URL | |
| 12 | |
| 13 class KnownIssuesDataSource: | |
| 14 """A data source that fetches Apps issues from code.google.com, and returns a | |
| 15 list of open and closed issues. | |
| 16 """ | |
| 17 def __init__(self, object_store, url_fetch): | |
| 18 self._object_store = object_store | |
| 19 self._url_fetch = url_fetch | |
| 20 | |
| 21 def GetIssues(self, url): | |
| 22 cached_data = self._object_store.Get(url, object_store.KNOWN_ISSUES).Get() | |
| 23 if cached_data is not None: | |
| 24 return cached_data | |
| 25 | |
| 26 issues = [] | |
| 27 response = self._url_fetch.Fetch(url) | |
| 28 if response.status_code != 200: | |
| 29 logging.warning('Fetching %s gave status code %s.' % | |
| 30 (KNOWN_ISSUES_CSV_URL, response.status_code)) | |
| 31 # Return None so we can do {{^known_issues.open}} in the template. | |
| 32 return None | |
| 33 issues_reader = csv.DictReader(StringIO(response.content)) | |
| 34 | |
| 35 for issue_dict in issues_reader: | |
| 36 id = issue_dict.get('ID', '') | |
| 37 title = issue_dict.get('Summary', '') | |
| 38 if not id or not title: | |
| 39 continue | |
| 40 issues.append({ 'id': id, 'title': title }) | |
| 41 issues = sorted(issues, key=operator.itemgetter('title')) | |
| 42 self._object_store.Set(url, issues, object_store.KNOWN_ISSUES) | |
| 43 return issues | |
| 44 | |
| 45 def get(self, key): | |
| 46 return { | |
| 47 'open': self.GetIssues(OPEN_ISSUES_CSV_URL), | |
| 48 'closed': self.GetIssues(CLOSED_ISSUES_CSV_URL) | |
| 49 }.get(key, None) | |
| OLD | NEW |