OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013 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 json |
| 6 |
| 7 import patching |
| 8 import util |
| 9 |
| 10 |
| 11 class Patch(object): |
| 12 """Helper class for lazily loading and parsing patch data.""" |
| 13 |
| 14 def __init__(self, rietveld, issue_id, patchset_id, patch_id): |
| 15 self.rietveld = rietveld |
| 16 self.issue_id = issue_id |
| 17 self.patchset_id = patchset_id |
| 18 self.patch_id = patch_id |
| 19 |
| 20 @util.lazy_property |
| 21 def raw(self): |
| 22 return self.rietveld.post_data( |
| 23 'download/issue%s_%s_%s.diff' % |
| 24 (self.issue_id, self.patchset_id, self.patch_id)) |
| 25 |
| 26 @util.lazy_property |
| 27 def lines(self): |
| 28 return patching.ParsePatchToLines(self.raw.splitlines()) |
| 29 |
| 30 |
| 31 class Review(object): |
| 32 """Represents a code review. |
| 33 |
| 34 Information from rietveld can be obtained via the following properties: |
| 35 - |issue_id| is the issue identifier. |
| 36 - |issue_data| contains issue meta data as retrieved from rietveld. The data |
| 37 is pulled lazily from the rietveld API on first access. |
| 38 - |patchsets| has lazily-pulled patchset meta data, indexed by patchset IDa. |
| 39 |
| 40 The subclass may then do its processing and trigger any actions. In |
| 41 particular, the |rietveld| object may be used to update rietveld issue state. |
| 42 """ |
| 43 def __init__(self, rietveld, issue_id): |
| 44 self.rietveld = rietveld |
| 45 self.issue_id = issue_id |
| 46 |
| 47 @util.lazy_property |
| 48 def issue_data(self): |
| 49 json_data = self.rietveld.post_data('api/%s?messages=true' % self.issue_id) |
| 50 data = json.loads(json_data) |
| 51 data['messages'] = [util.ObjectDict(msg) for msg in data['messages']] |
| 52 return util.ObjectDict(data) |
| 53 |
| 54 @util.lazy_property |
| 55 def patchsets(self): |
| 56 def retrieve_patchset(ps): |
| 57 json_patchset_data = self.rietveld.post_data('api/%s/%s' % |
| 58 (self.issue_id, ps)) |
| 59 patchset_data = json.loads(json_patchset_data) |
| 60 |
| 61 # Amend the files property so it can lazily load and return patch data. |
| 62 for file_data in patchset_data.get('files', {}).values(): |
| 63 file_data['patch'] = Patch(self.rietveld, self.issue_id, ps, |
| 64 file_data['id']) |
| 65 |
| 66 return util.ObjectDict(patchset_data) |
| 67 |
| 68 return util.LazyDict(retrieve_patchset) |
| 69 |
| 70 @util.lazy_property |
| 71 def latest_patchset(self): |
| 72 return self.patchsets[self.issue_data.patchsets[-1]] |
OLD | NEW |