|
7 | 7 | 'pull_to_dict', |
8 | 8 | 'commit_to_dict', |
9 | 9 | 'label_to_dict', |
10 | | - 'user_to_dict' |
| 10 | + 'user_to_dict', |
| 11 | + 'contents_to_dict', |
| 12 | + 'file_response_to_dict' |
11 | 13 | ] |
12 | 14 |
|
13 | 15 |
|
@@ -75,6 +77,7 @@ def pull_to_dict(pull): |
75 | 77 | result['head'] = pull.head.ref |
76 | 78 | result['state'] = pull.state |
77 | 79 | result['merged'] = pull.merged |
| 80 | + # noinspection SpellCheckingInspection |
78 | 81 | result['mergeable_state'] = pull.mergeable_state |
79 | 82 | result['merge_commit_sha'] = pull.merge_commit_sha |
80 | 83 |
|
@@ -116,36 +119,93 @@ def pull_to_dict(pull): |
116 | 119 |
|
117 | 120 |
|
118 | 121 | def commit_to_dict(commit): |
119 | | - result = {} |
120 | | - result['sha'] = commit.sha |
| 122 | + result = {'sha': commit.sha} |
121 | 123 | return result |
122 | 124 |
|
123 | 125 |
|
124 | 126 | def label_to_dict(label): |
125 | | - result = {} |
| 127 | + result = {'name': label.name, 'color': label.color, 'url': label.url} |
126 | 128 |
|
127 | | - result['name'] = label.name |
128 | | - result['color'] = label.color |
129 | | - result['url'] = label.url |
130 | 129 | return result |
131 | 130 |
|
132 | 131 |
|
133 | 132 | def user_to_dict(user): |
134 | 133 | if not user: |
135 | 134 | return None |
136 | 135 |
|
137 | | - result = {} |
138 | | - result['name'] = user.name |
139 | | - result['login'] = user.login |
| 136 | + result = {'name': user.name, 'login': user.login} |
140 | 137 | return result |
141 | 138 |
|
142 | 139 |
|
143 | 140 | def team_to_dict(team): |
144 | 141 | if not team: |
145 | 142 | return None |
146 | 143 |
|
147 | | - result = {} |
148 | | - result['id'] = team.id |
149 | | - result['name'] = team.name |
150 | | - result['members_count'] = team.members_count |
| 144 | + result = {'id': team.id, 'name': team.name, 'members_count': team.members_count} |
| 145 | + return result |
| 146 | + |
| 147 | + |
| 148 | +def contents_to_dict(contents, decode=False): |
| 149 | + if not contents: |
| 150 | + return None |
| 151 | + |
| 152 | + directory = False |
| 153 | + if isinstance(contents, list): |
| 154 | + directory = True |
| 155 | + else: |
| 156 | + contents = [contents] |
| 157 | + |
| 158 | + result = [] |
| 159 | + data = {} |
| 160 | + |
| 161 | + for item in contents: |
| 162 | + item_type = item.type |
| 163 | + data['type'] = item_type |
| 164 | + if item_type == 'symlink': |
| 165 | + data['target'] = item.target |
| 166 | + elif item_type == 'submodule': |
| 167 | + data['submodule_git_url'] = item.submodule_git_url |
| 168 | + elif not directory: |
| 169 | + encoding = item.encoding |
| 170 | + content = item.content |
| 171 | + data['encoding'] = encoding |
| 172 | + if decode and encoding == 'base64': |
| 173 | + content = decode_base64(content) |
| 174 | + data['content'] = content |
| 175 | + |
| 176 | + data['size'] = item.size |
| 177 | + data['name'] = item.name |
| 178 | + data['path'] = item.path |
| 179 | + data['sha'] = item.sha |
| 180 | + data['url'] = item.url |
| 181 | + data['git_url'] = item.git_url |
| 182 | + data['html_url'] = item.html_url |
| 183 | + data['download_url'] = item.download_url |
| 184 | + result.append(data) |
| 185 | + |
| 186 | + if not directory: |
| 187 | + return result[0] |
| 188 | + else: |
| 189 | + return result |
| 190 | + |
| 191 | + |
| 192 | +def decode_base64(data): |
| 193 | + """Decode base64, padding being optional. |
| 194 | +
|
| 195 | + :param data: Base64 data as an ASCII byte string |
| 196 | + :returns: The decoded byte string. |
| 197 | +
|
| 198 | + """ |
| 199 | + missing_padding = len(data) % 4 |
| 200 | + if missing_padding != 0: |
| 201 | + data += b'=' * (4 - missing_padding) |
| 202 | + |
| 203 | + import base64 |
| 204 | + data = data.encode("utf-8") |
| 205 | + data = base64.b64decode(data).decode("utf-8") |
| 206 | + return data |
| 207 | + |
| 208 | + |
| 209 | +def file_response_to_dict(response): |
| 210 | + result = {'commit': response['commit'].sha} |
151 | 211 | return result |
0 commit comments