|
1 | 1 | # -*- 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.""" |
8 | 3 | from __future__ import unicode_literals
|
9 | 4 |
|
10 | 5 | from ..models import GitHubCore
|
11 | 6 |
|
12 | 7 |
|
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 | + |
14 | 32 |
|
| 33 | +class ShortGistFile(_GistFile): |
15 | 34 | """This represents the file object returned by interacting with gists.
|
16 | 35 |
|
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. |
19 | 46 |
|
| 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. |
20 | 58 | """
|
21 | 59 |
|
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' |
25 | 61 |
|
26 |
| - #: The name of the file. |
27 |
| - self.filename = self._get_attribute(gistfile, 'filename') |
28 | 62 |
|
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. |
31 | 65 |
|
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: |
34 | 68 |
|
35 |
| - #: The size of the file. |
36 |
| - self.size = self._get_attribute(gistfile, 'size') |
| 69 | + .. attribute:: truncated |
37 | 70 |
|
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. |
40 | 73 |
|
41 |
| - def _repr(self): |
42 |
| - return '<Gist File [{0}]>'.format(self.name) |
| 74 | + .. attribute:: original_content |
43 | 75 |
|
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