forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlicenses.py
More file actions
211 lines (136 loc) · 5.69 KB
/
licenses.py
File metadata and controls
211 lines (136 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# -*- coding: utf-8 -*-
"""
This module contains the classes relating to licenses.
See also: https://developer.github.com/v3/licenses/
"""
from __future__ import unicode_literals
import base64
from . import models
class _License(models.GitHubCore):
"""Base license object."""
class_name = '_License'
def _update_attributes(self, license):
self._api = license['url']
self.key = license['key']
self.name = license['name']
self.spdx_id = license['spdx_id']
def _repr(self):
return '<{0} [{1}]>'.format(self.class_name, self.name)
class License(_License):
"""This object represents a license as returned by the GitHub API.
See https://developer.github.com/v3/licenses/ for more information.
This object has all of the attributes of :class:`ShortLicense` as well as
the following attributes:
.. attribute:: body
The full text of this license.
.. attribute:: conditions
A list of the conditions of this license.
.. attribute:: description
The short description of this license.
.. attribute:: featured
A boolean attribute describing whether this license is featured on
GitHub or not.
.. attribute:: html_url
The URL to view this license on GitHub.
.. attribute:: implementation
The short description of how a user applies this license to their
original work.
.. attribute:: limitations
A list of limitations of this license.
.. attribute:: permissions
A list of the permissions granted by this license.
"""
class_name = 'License'
def _update_attributes(self, license):
super(License, self)._update_attributes(license)
self.body = license['body']
self.conditions = license['conditions']
self.description = license['description']
self.featured = license['featured']
self.html_url = license['html_url']
self.implementation = license['implementation']
self.limitations = license['limitations']
self.permissions = license['permissions']
def _repr(self):
return '<License [{0}]>'.format(self.name)
class ShortLicense(_License):
"""This object represents a license returned in a collection.
GitHub's API returns different representations of objects in different
contexts. This object reprsents a license that would be returned in a
collection, e.g., retrieving all licenses from ``/licenses``.
This object has the following attributes:
.. attribute:: key
The short, API name, for this license.
.. attribute:: name
The long form, legal name for this license.
.. attribute:: spdx_id
The Software Package Data Exchange (a.k.a, SPDX) identifier for this
license.
"""
class_name = 'ShortLicense'
_refresh_to = License
class RepositoryLicense(models.GitHubCore):
"""The representation of the repository's retrieved license.
This object will be returned from
:meth:`~github3.repos.repo.Repository.license` and behaves like
:class:`~github3.repos.contents.Contents` with a few differences.
This also includes a :attr:`license` attribute to access the licenses API.
This object has the following attributes:
.. attribute:: name
The name of the file this license is stored in on the repository.
.. attribute:: path
The path to the file of this license in the repository.
.. attribute:: sha
The current SHA of this license file in the repository.
.. attribute:: size
The size in bytes of this file.
.. attribute:: html_url
The URL used to view this file in a browser.
.. attribute:: git_url
The URL to retrieve this license file via the git protocol.
.. attribute:: download_url
The URL used to download this license file from the repository.
.. attribute:: type
Analogous to :attr:`github3.repos.contents.Contents.type`, this should
indicate whether this is a file, directory, or some other type.
.. attribute:: content
The content as returned by the API. This may be base64 encoded. See
:meth:`decode_content` to retrieve the content in plain-text.
.. attribute:: encoding
The encoding of the content. For example, ``base64``.
.. attribute:: links
The dictionary of URLs returned in the ``_links`` key by the API.
.. attribute:: license
A :class:`github3.licenses.ShortLicense` instance representing the
metadata GitHub knows about the license.
"""
def _update_attributes(self, license):
self._api = license['url']
self.name = license['name']
self.path = license['path']
self.sha = license['sha']
self.size = license['size']
self.html_url = license['html_url']
self.git_url = license['git_url']
self.download_url = license['download_url']
self.type = license['type']
self.content = license['content']
self.encoding = license['encoding']
self.links = license['_links']
self.license = ShortLicense(license['license'], self)
def _repr(self):
return '<RepositoryLicense [{0}]>'.format(self.name)
def decode_content(self):
"""Decode the :attr:`content` attribute.
If ``content`` is base64 encoded, decode this and return it.
Otherwise, return ``content``.
:returns:
plain-text content of this license
:rtype:
text (unicode on Python 2, str on Python 3)
"""
if self.encoding == 'base64':
return base64.b64decode(
self.content.encode('utf-8')
).decode('utf-8')
return self.content