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