diff options
author | Mike Crute <mcrute@gmail.com> | 2011-06-05 12:25:28 -0400 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2011-06-05 12:25:28 -0400 |
commit | 3c8ce09e0e1f13cfc09553e8f51a63809a99ef02 (patch) | |
tree | 208fda2d4c49af08cb6f15f5efe64450c202de41 | |
parent | 2468ee28c7656677ce40552dd05124b5995e3667 (diff) | |
download | pygithubissues-3c8ce09e0e1f13cfc09553e8f51a63809a99ef02.tar.bz2 pygithubissues-3c8ce09e0e1f13cfc09553e8f51a63809a99ef02.tar.xz pygithubissues-3c8ce09e0e1f13cfc09553e8f51a63809a99ef02.zip |
Updating to use classes and the requests library
-rw-r--r-- | pygithubissues/__init__.py | 138 |
1 files changed, 29 insertions, 109 deletions
diff --git a/pygithubissues/__init__.py b/pygithubissues/__init__.py index fd8bf94..d077dea 100644 --- a/pygithubissues/__init__.py +++ b/pygithubissues/__init__.py | |||
@@ -1,123 +1,43 @@ | |||
1 | # vim: set expandtab ts=4 sw=4 filetype=python: | 1 | # vim:expandtab ts=4 sw=4 ft=python |
2 | |||
3 | """ | ||
4 | This will likely be rewritten into something with classes. | ||
5 | """ | ||
6 | 2 | ||
7 | import json | 3 | import json |
8 | import logging | 4 | import requests |
9 | import subprocess | ||
10 | |||
11 | def setup_logging(): | ||
12 | |||
13 | log = logging.getLogger('pygithubissues') | ||
14 | h = logging.StreamHandler() | ||
15 | f = logging.Formatter("%(levelname)s %(asctime)s %(message)s") | ||
16 | h.setFormatter(f) | ||
17 | log.addHandler(h) | ||
18 | |||
19 | return log | ||
20 | |||
21 | log = setup_logging() | ||
22 | |||
23 | |||
24 | def list_issues_for_repo(username, password, repo_owner, repo): | ||
25 | |||
26 | """ | ||
27 | Does this:: | ||
28 | |||
29 | curl -u "username:password" \\ | ||
30 | https://api.github.com/repos/repo_owner/repo/issues | ||
31 | |||
32 | """ | ||
33 | |||
34 | auth_option = '%s:%s' % (username, password) | ||
35 | |||
36 | url = ('https://api.github.com/repos/%s/%s/issues' | ||
37 | % (repo_owner, repo)) | ||
38 | |||
39 | args = ['curl', '-u', auth_option, url] | ||
40 | |||
41 | p = subprocess.Popen(args, stdout=subprocess.PIPE, | ||
42 | stderr=subprocess.PIPE) | ||
43 | |||
44 | results = p.communicate() | ||
45 | |||
46 | return json.loads(results[0]) | ||
47 | |||
48 | def get_a_single_issue(username, password, repo_owner, repo, issue_id): | ||
49 | |||
50 | """ | ||
51 | Does this:: | ||
52 | |||
53 | curl -u "username:password" \\ | ||
54 | https://api.github.com/repos/:repo_owner/:repo/issues/:issue_id | ||
55 | |||
56 | """ | ||
57 | |||
58 | auth_option = '%s:%s' % (username, password) | ||
59 | |||
60 | url = ('https://api.github.com/repos/%s/%s/issues/%s' | ||
61 | % (repo_owner, repo, issue_id)) | ||
62 | |||
63 | args = ['curl', '-u', auth_option, url] | ||
64 | |||
65 | p = subprocess.Popen(args, stdout=subprocess.PIPE, | ||
66 | stderr=subprocess.PIPE) | ||
67 | |||
68 | results = p.communicate() | ||
69 | |||
70 | return json.loads(results[0]) | ||
71 | |||
72 | |||
73 | |||
74 | def list_milestones_for_repo(username, password, repo_owner, repo): | ||
75 | |||
76 | """ | ||
77 | Does this:: | ||
78 | |||
79 | curl -u "{username}:{password}" \\ | ||
80 | https://api.github.com/repos/{repo_owner}/{repo}/milestones | ||
81 | |||
82 | """ | ||
83 | |||
84 | auth_option = '%s:%s' % (username, password) | ||
85 | |||
86 | url = ('https://api.github.com/repos/%s/%s/milestones' | ||
87 | % (repo_owner, repo)) | ||
88 | |||
89 | args = ['curl', '-u', auth_option, url] | ||
90 | |||
91 | p = subprocess.Popen(args, stdout=subprocess.PIPE, | ||
92 | stderr=subprocess.PIPE) | ||
93 | |||
94 | results = p.communicate() | ||
95 | 5 | ||
96 | return json.loads(results[0]) | ||
97 | 6 | ||
7 | class IssueManager(object): | ||
98 | 8 | ||
99 | def list_issues_in_milestone(username, password, repo_owner, repo, | 9 | def __init__(self, username, password, repo_owner, repo): |
100 | milestone_number): | 10 | self._auth_pair = (username, password) |
101 | 11 | ||
102 | """ | 12 | self.issues_url = ( |
103 | Does this:: | 13 | "https://api.github.com/repos/{repo_owner}/{repo}/issues" |
14 | .format(**locals()) | ||
15 | ) | ||
104 | 16 | ||
105 | curl -u "username:password" \\ | 17 | self.milestones_url = ( |
106 | https://api.github.com/repos/repo_owner/repo/issues \\ | 18 | "https://api.github.com/repos/{repo_owner}/{repo}/milestones" |
107 | ?milestone={milestone_number} | 19 | .format(**locals()) |
20 | ) | ||
108 | 21 | ||
109 | """ | 22 | def _do_request(self, url): |
23 | data = requests.get(url, auth=self._auth_pair) | ||
110 | 24 | ||
111 | auth_option = '%s:%s' % (username, password) | 25 | if data.status_code != 200: |
26 | raise Exception( | ||
27 | "Error {data.status_code}: {data.content}".format(data=data)) | ||
112 | 28 | ||
113 | url = ('https://api.github.com/repos/%s/%s/issues?milestone=%s' | 29 | return json.loads(data.content) |
114 | % (repo_owner, repo, milestone_number)) | ||
115 | 30 | ||
116 | args = ['curl', '-u', auth_option, url] | 31 | def list_issues(self): |
32 | return self._do_request(self.issues_url) | ||
117 | 33 | ||
118 | p = subprocess.Popen(args, stdout=subprocess.PIPE, | 34 | def get_issue(self, issue_id): |
119 | stderr=subprocess.PIPE) | 35 | url = "{0}/{1}".format(self.issues_url, issue_id) |
36 | return self._do_request(url) | ||
120 | 37 | ||
121 | results = p.communicate() | 38 | def list_milestones(self): |
39 | return self._do_request(self.milestones_url) | ||
122 | 40 | ||
123 | return json.loads(results[0]) | 41 | def list_issues_in_milestone(self, milestone_number): |
42 | url = "{0}?milestone={1:d}".format(self.issues_url, milestone_number) | ||
43 | return self._do_request(url) | ||