ContentFile.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net> # 4 # Copyright 2012 Zearin <zearin@gonk.net> # 5 # Copyright 2013 AKFish <akfish@gmail.com> # 6 # Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net> # 7 # Copyright 2014 Thialfihar <thi@thialfihar.org> # 8 # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net> # 9 # Copyright 2016 Jannis Gebauer <ja.geb@me.com> # 10 # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> # 11 # Copyright 2018 Wan Liuyang <tsfdye@gmail.com> # 12 # Copyright 2018 sfdye <tsfdye@gmail.com> # 13 # # 14 # This file is part of PyGithub. # 15 # http://pygithub.readthedocs.io/ # 16 # # 17 # PyGithub is free software: you can redistribute it and/or modify it under # 18 # the terms of the GNU Lesser General Public License as published by the Free # 19 # Software Foundation, either version 3 of the License, or (at your option) # 20 # any later version. # 21 # # 22 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 23 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 24 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 25 # details. # 26 # # 27 # You should have received a copy of the GNU Lesser General Public License # 28 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 29 # # 30 ################################################################################ 31 from __future__ import annotations 32 33 import base64 34 from typing import TYPE_CHECKING, Any 35 36 import github.GithubObject 37 import github.Repository 38 from github.GithubObject import Attribute, CompletableGithubObject, NotSet, _ValuedAttribute 39 40 if TYPE_CHECKING: 41 from github.License import License 42 from github.Repository import Repository 43 44 45 class ContentFile(CompletableGithubObject): 46 """ 47 This class represents ContentFiles. The reference can be found here https://docs.github.com/en/rest/reference/repos#contents 48 """ 49 50 def _initAttributes(self) -> None: 51 self._content: Attribute[str] = NotSet 52 self._download_url: Attribute[str] = NotSet 53 self._encoding: Attribute[str] = NotSet 54 self._git_url: Attribute[str] = NotSet 55 self._html_url: Attribute[str] = NotSet 56 self._license: Attribute[License] = NotSet 57 self._name: Attribute[str] = NotSet 58 self._path: Attribute[str] = NotSet 59 self._repository: Attribute[Repository] = NotSet 60 self._sha: Attribute[str] = NotSet 61 self._size: Attribute[int] = NotSet 62 self._type: Attribute[str] = NotSet 63 self._url: Attribute[str] = NotSet 64 self._text_matches: Attribute[str] = NotSet 65 66 def __repr__(self) -> str: 67 return self.get__repr__({"path": self._path.value}) 68 69 @property 70 def content(self) -> str: 71 self._completeIfNotSet(self._content) 72 return self._content.value 73 74 @property 75 def decoded_content(self) -> bytes: 76 assert self.encoding == "base64", f"unsupported encoding: {self.encoding}" 77 return base64.b64decode(bytearray(self.content, "utf-8")) 78 79 @property 80 def download_url(self) -> str: 81 self._completeIfNotSet(self._download_url) 82 return self._download_url.value 83 84 @property 85 def encoding(self) -> str: 86 self._completeIfNotSet(self._encoding) 87 return self._encoding.value 88 89 @property 90 def git_url(self) -> str: 91 self._completeIfNotSet(self._git_url) 92 return self._git_url.value 93 94 @property 95 def html_url(self) -> str: 96 self._completeIfNotSet(self._html_url) 97 return self._html_url.value 98 99 @property 100 def license(self) -> License: 101 self._completeIfNotSet(self._license) 102 return self._license.value 103 104 @property 105 def name(self) -> str: 106 self._completeIfNotSet(self._name) 107 return self._name.value 108 109 @property 110 def path(self) -> str: 111 self._completeIfNotSet(self._path) 112 return self._path.value 113 114 @property 115 def repository(self) -> Repository: 116 if self._repository is NotSet: 117 # The repository was not set automatically, so it must be looked up by url. 118 repo_url = "/".join(self.url.split("/")[:6]) # pragma no cover (Should be covered) 119 self._repository = _ValuedAttribute( 120 github.Repository.Repository(self._requester, self._headers, {"url": repo_url}, completed=False) 121 ) # pragma no cover (Should be covered) 122 return self._repository.value 123 124 @property 125 def sha(self) -> str: 126 self._completeIfNotSet(self._sha) 127 return self._sha.value 128 129 @property 130 def size(self) -> int: 131 self._completeIfNotSet(self._size) 132 return self._size.value 133 134 @property 135 def type(self) -> str: 136 self._completeIfNotSet(self._type) 137 return self._type.value 138 139 @property 140 def url(self) -> str: 141 self._completeIfNotSet(self._url) 142 return self._url.value 143 144 @property 145 def text_matches(self) -> str: 146 self._completeIfNotSet(self._text_matches) 147 return self._text_matches.value 148 149 def _useAttributes(self, attributes: dict[str, Any]) -> None: 150 if "content" in attributes: # pragma no branch 151 self._content = self._makeStringAttribute(attributes["content"]) 152 if "download_url" in attributes: # pragma no branch 153 self._download_url = self._makeStringAttribute(attributes["download_url"]) 154 if "encoding" in attributes: # pragma no branch 155 self._encoding = self._makeStringAttribute(attributes["encoding"]) 156 if "git_url" in attributes: # pragma no branch 157 self._git_url = self._makeStringAttribute(attributes["git_url"]) 158 if "html_url" in attributes: # pragma no branch 159 self._html_url = self._makeStringAttribute(attributes["html_url"]) 160 if "license" in attributes: # pragma no branch 161 self._license = self._makeClassAttribute(github.License.License, attributes["license"]) 162 if "name" in attributes: # pragma no branch 163 self._name = self._makeStringAttribute(attributes["name"]) 164 if "path" in attributes: # pragma no branch 165 self._path = self._makeStringAttribute(attributes["path"]) 166 if "repository" in attributes: # pragma no branch 167 self._repository = self._makeClassAttribute(github.Repository.Repository, attributes["repository"]) 168 if "sha" in attributes: # pragma no branch 169 self._sha = self._makeStringAttribute(attributes["sha"]) 170 if "size" in attributes: # pragma no branch 171 self._size = self._makeIntAttribute(attributes["size"]) 172 if "type" in attributes: # pragma no branch 173 self._type = self._makeStringAttribute(attributes["type"]) 174 if "url" in attributes: # pragma no branch 175 self._url = self._makeStringAttribute(attributes["url"]) 176 if "text_matches" in attributes: # pragma no branch 177 self._text_matches = self._makeListOfDictsAttribute(attributes["text_matches"])