forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontents.py
More file actions
157 lines (127 loc) · 5.55 KB
/
contents.py
File metadata and controls
157 lines (127 loc) · 5.55 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
# -*- coding: utf-8 -*-
"""
github3.repos.contents
======================
This module contains the Contents object pertaining to READMEs and other files
that can be accessed via the GitHub API.
"""
from __future__ import unicode_literals
from json import dumps
from base64 import b64decode, b64encode
from ..git import Commit
from ..models import GitHubCore
from ..decorators import requires_auth
class Contents(GitHubCore):
"""The :class:`Contents <Contents>` object. It holds the information
concerning any content in a repository requested via the API.
Two content instances can be checked like so::
c1 == c2
c1 != c2
And is equivalent to::
c1.sha == c2.sha
c1.sha != c2.sha
See also: http://developer.github.com/v3/repos/contents/
"""
def __init__(self, content, session=None):
super(Contents, self).__init__(content, session)
# links
self._api = content.get('url')
#: Dictionary of links
self.links = content.get('_links')
#: URL of the README on github.com
self.html_url = content.get('html_url')
#: URL for the git api pertaining to the README
self.git_url = content.get('git_url')
#: git:// URL of the content if it is a submodule
self.submodule_git_url = content.get('submodule_git_url')
# should always be 'base64'
#: Returns encoding used on the content.
self.encoding = content.get('encoding', '')
# content, base64 encoded and decoded
#: Base64-encoded content of the file.
self.content = content.get('content', '')
#: Decoded content of the file as a bytes object. If we try to decode
#: to character set for you, we might encounter an exception which
#: will prevent the object from being created. On python2 this is the
#: same as a string, but on python3 you should call the decode method
#: with the character set you wish to use, e.g.,
#: ``content.decoded.decode('utf-8')``.
#: .. versionchanged:: 0.5.2
self.decoded = ''
if self.encoding == 'base64' and self.content:
self.decoded = b64decode(self.content.encode())
# file name, path, and size
#: Name of the content.
self.name = content.get('name', '')
#: Path to the content.
self.path = content.get('path', '')
#: Size of the content
self.size = content.get('size', 0)
#: SHA string.
self.sha = content.get('sha', '')
#: Type of content. ('file', 'symlink', 'submodule')
self.type = content.get('type', '')
#: Target will only be set of type is a symlink. This is what the link
#: points to
self.target = content.get('target', '')
self._uniq = self.sha
def _repr(self):
return '<Content [{0}]>'.format(self.path)
def __eq__(self, other):
return self.decoded == other
def __ne__(self, other):
return self.sha != other
@requires_auth
def delete(self, message, committer=None, author=None):
"""Delete this file.
:param str message: (required), commit message to describe the removal
:param dict committer: (optional), if no information is given the
authenticated user's information will be used. You must specify
both a name and email.
:param dict author: (optional), if omitted this will be filled in with
committer information. If passed, you must specify both a name and
email.
:returns: :class:`Commit <github3.git.Commit>`
"""
json = None
if message:
data = {'message': message, 'sha': self.sha,
'committer': validate_commmitter(committer),
'author': validate_commmitter(author)}
self._remove_none(data)
json = self._json(self._delete(self._api, data=dumps(data)), 200)
if 'commit' in json:
json = Commit(json['commit'], self)
return json
@requires_auth
def update(self, message, content, committer=None, author=None):
"""Update this file.
:param str message: (required), commit message to describe the update
:param str content: (required), content to update the file with
:param dict committer: (optional), if no information is given the
authenticated user's information will be used. You must specify
both a name and email.
:param dict author: (optional), if omitted this will be filled in with
committer information. If passed, you must specify both a name and
email.
:returns: :class:`Commit <github3.git.Commit>`
"""
if content and not isinstance(content, bytes):
raise ValueError( # (No coverage)
'content must be a bytes object') # (No coverage)
json = None
if message and content:
content = b64encode(content).decode('utf-8')
data = {'message': message, 'content': content, 'sha': self.sha,
'committer': validate_commmitter(committer),
'author': validate_commmitter(author)}
self._remove_none(data)
json = self._json(self._put(self._api, data=dumps(data)), 200)
if 'content' in json and 'commit' in json:
self.__init__(json['content'], self)
json = Commit(json['commit'], self)
return json
def validate_commmitter(d):
if d and d.get('name') and d.get('email'):
return d
return None