Merge lp:~jelmer/brz/github-lazy into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: 7647
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/github-lazy
Merge into: lp:brz
Diff against target: 103 lines (+70/-8)
2 files modified
breezy/plugins/github/cmds.py (+2/-3)
breezy/plugins/github/forge.py (+68/-5)
To merge this branch: bzr merge lp:~jelmer/brz/github-lazy
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve
Review via email: mp+430713@code.launchpad.net

Commit message

Lazy load github mps

Description of the change

Lazy load github mps.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) :
review: Approve
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :
lp:~jelmer/brz/github-lazy updated
7647. By Jelmer Vernooij

Fix syntax.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/plugins/github/cmds.py'
2--- breezy/plugins/github/cmds.py 2020-06-21 02:15:25 +0000
3+++ breezy/plugins/github/cmds.py 2022-09-29 05:18:31 +0000
4@@ -56,6 +56,5 @@
5 raise errors.CommandError(e.data['message'])
6 # TODO(jelmer): This should really use something in
7 # AuthenticationConfig
8- from .github import store_github_token
9- store_github_token(scheme='https', host='github.com',
10- token=authorization.token)
11+ from .forge import store_github_token
12+ store_github_token(token=authorization.token)
13
14=== modified file 'breezy/plugins/github/forge.py'
15--- breezy/plugins/github/forge.py 2022-09-27 14:29:35 +0000
16+++ breezy/plugins/github/forge.py 2022-09-29 05:18:31 +0000
17@@ -250,6 +250,68 @@
18 return url.split('{')[0]
19
20
21+class _LazyDict(dict):
22+
23+ def __init__(self, base, load_fn):
24+ self._load_fn = load_fn
25+ super(_LazyDict, self).update(base)
26+
27+ def _load_full(self):
28+ super(_LazyDict, self).update(self._load_fn())
29+ self._load_fn = None
30+
31+ def __getitem__(self, key):
32+ if self._load_fn is not None:
33+ try:
34+ return super(_LazyDict, self).__getitem__(key)
35+ except KeyError:
36+ self._load_full()
37+ return super(_LazyDict, self).__getitem__(key)
38+
39+ def items(self):
40+ self._load_full()
41+ return super(_LazyDict, self).items()
42+
43+ def keys(self):
44+ self._load_full()
45+ return super(_LazyDict, self).keys()
46+
47+ def values(self):
48+ self._load_full()
49+ return super(_LazyDict, self).values()
50+
51+ def __contains__(self, key):
52+ if super(_LazyDict, self).__contains__(key):
53+ return True
54+ if self._load_fn is not None:
55+ self._load_full()
56+ return super(_LazyDict, self).__contains__(key)
57+ return False
58+
59+ def __delitem__(self, name):
60+ raise NotImplementedError
61+
62+ def __setitem__(self, name, value):
63+ raise NotImplementedError
64+
65+ def get(self, name, default=None):
66+ if self._load_fn is not None:
67+ try:
68+ return super(_LazyDict, self).get(name, default)
69+ except KeyError:
70+ self._load_full()
71+ return super(_LazyDict, self).get(name, default)
72+
73+ def pop(self):
74+ raise NotImplementedError
75+
76+ def popitem(self):
77+ raise NotImplementedError
78+
79+ def clear(self):
80+ raise NotImplementedError
81+
82+
83 class GitHub(Forge):
84
85 name = 'github'
86@@ -554,11 +616,12 @@
87 author = self.current_user['login']
88 query.append('author:%s' % author)
89 for issue in self._search_issues(query=' '.join(query)):
90- url = issue['pull_request']['url']
91- response = self._api_request('GET', url)
92- if response.status != 200:
93- raise UnexpectedHttpStatus(url, response.status, headers=response.getheaders())
94- yield GitHubMergeProposal(self, json.loads(response.text))
95+ def retrieve_full():
96+ response = self._api_request('GET', issue['pull_request']['url'])
97+ if response.status != 200:
98+ raise UnexpectedHttpStatus(issue['pull_request']['url'], response.status, headers=response.getheaders())
99+ return json.loads(response.text)
100+ yield GitHubMergeProposal(self, _LazyDict(issue['pull_request'], retrieve_full))
101
102 def get_proposal_by_url(self, url):
103 raise UnsupportedForge(url)

Subscribers

People subscribed via source and target branches