8000 Merge pull request #753 from sigmavirus24/split-up-gist · pythonthings/github3.py@b70bd33 · GitHub
[go: up one dir, main page]

Skip to content

Commit b70bd33

Browse files
authored
Merge pull request sigmavirus24#753 from sigmavirus24/split-up-gist
Split-up Gist-related objects as necessary
2 parents 0914080 + 282c3bc commit b70bd33

22 files changed

+484
-445
lines changed

github3/gists/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
See also: http://developer.github.com/v3/gists/
1414
"""
1515

16-
from .gist import Gist
16+
from .gist import Gist, ShortGist
1717

18-
__all__ = [Gist]
18+
__all__ = ('Gist', 'ShortGist')

github3/gists/file.py

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,87 @@
11
# -*- coding: utf-8 -*-
2-
"""
3-
github3.gists.file
4-
------------------
5-
6-
Module containing the logic for the GistFile object.
7-
"""
2+
"""Module containing the GistFile object."""
83
from __future__ import unicode_literals
94

105
from ..models import GitHubCore
116

127

13-
class GistFile(GitHubCore):
8+
class _GistFile(GitHubCore):
9+
"""Base for GistFile classes."""
10+
11+
def _update_attributes(self, gistfile):
12+
self.raw_url = gistfile['raw_url']
13+
self.filename = gistfile['filename']
14+
self.language = gistfile['language']
15+
self.size = gistfile['size']
16+
self.type = gistfile['type']
17+
18+
def _repr(self):
19+
return '<{s.class_name} [{s.name}]>'.format(s=self)
20+
21+
def content(self):
22+
"""Retrieve contents of file from key 'raw_url'.
23+
24+
:returns: unaltered, untruncated contents of file.
25+
:rtype: bytes
26+
"""
27+
resp = self._get(self.raw_url)
28+
if self._boolean(resp, 200, 404):
29+
return resp.content
30+
return None
31+
1432

33+
class ShortGistFile(_GistFile):
1534
"""This represents the file object returned by interacting with gists.
1635
17-
It stores the raw url of the file, the file name, language, size and
18-
content.
36+
The object has the following attributes as returned by the API for a Gist:
37+
38+
.. attribute:: raw_url
39+
40+
This URL provides access to the complete, untruncated content of the
41+
file represented by this object.
42+
43+
.. attribute:: filename
44+
45+
The string for the filename.
1946
47+
.. attribute:: language
48+
49+
The GitHub detected language for the file, e.g., Erlang, Python, text.
50+
51+
.. attribute:: type
52+
53+
The mime-type of the file. Related to :attr:`language`.
54+
55+
.. attribute:: size
56+
57+
The file size in bytes.
2058
"""
2159

22-
def _update_attributes(self, gistfile):
23-
#: The raw URL for the file at GitHub.
24-
self.raw_url = self._get_attribute(gistfile, 'raw_url')
60+
class_name = 'ShortGistFile'
2561

26-
#: The name of the file.
27-
self.filename = self._get_attribute(gistfile, 'filename')
2862

29-
#: The name of the file.
30-
self.name = self._get_attribute(gistfile, 'filename')
63+
class GistFile(_GistFile):
64+
"""This represents the full file object returned by interacting with gists.
3165
32-
#: The language associated with the file.
33-
self.language = self._get_attribute(gistfile, 'language')
66+
The object has all of the attributes as returned by the API for a
67+
ShortGistFile as well as:
3468
35-
#: The size of the file.
36-
self.size = self._get_attribute(gistfile, 'size')
69+
.. attribute:: truncated
3770
38-
#: The content of the file.
39-
self.original_content = self._get_attribute(gistfile, 'content')
71+
A boolean attribute that indicates whether :attr:`original_content`
72+
contains all of the file's contents.
4073
41-
def _repr(self):
42-
return '<Gist File [{0}]>'.format(self.name)
74+
.. attribute:: original_content
4375
44-
def content(self):
45-
"""Retrieve contents of file from key 'raw_url' if there is no
46-
'content' key in Gist object.
47-
"""
48-
resp = self._get(self.raw_url)
49-
if self._boolean(resp, 200, 404):
50-
return resp.content
51-
return None
76+
The contents of the file (potentially truncated) returned by the API.
77+
If the file was truncated use :meth:`content` to retrieve it in its
78+
entirety.
79+
80+
"""
81+
82+
class_name = 'GistFile'
83+
84+
def _update_attributes(self, gistfile):
85+
super(GistFile, self)._update_attributes(gistfile)
86+
self.original_content = gistfile['content']
87+
self.truncated = gistfile['truncated']

0 commit comments

Comments
 (0)
0