Skip to content

Commit 258a616

Browse files
committed
Make docstrings use the Google docstring format, instead of pure Sphinx (much nicer)
1 parent db0f608 commit 258a616

File tree

5 files changed

+92
-53
lines changed

5 files changed

+92
-53
lines changed

basc_py4chan/board.py

+58-37
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,23 @@ def get_boards(board_name_list, *args, **kwargs):
1010
return [Board(name, *args, **kwargs) for name in board_name_list]
1111

1212
class Board(object):
13+
"""Represents a 4chan board.
14+
15+
Attributes:
16+
name (str): Name of this board, such as ``tg`` or ``k``
17+
"""
1318
def __init__(self, board_name, https=False, clean_comments=True,
1419
api_url=URL['api'], session=None):
20+
"""Creates a :mod:`basc_py4chan.Board` object.
21+
22+
Args:
23+
board_name (string): Name of the board, such as "tg" or "etc".
24+
https (bool): Whether to use a secure connection to 4chan.
25+
clean_comments (bool): Whether post objects will try to parse HTML comments
26+
(HTML entities, tags and links) into "cleaned" plaintext.
27+
api_url: Base 4chan API URL. This will be automatically set in all cases.
28+
session: Existing requests.session object to use instead of our current one.
29+
"""
1530
self._board_name = board_name
1631
self._protocol = 'https://' if https else 'http://'
1732
self._clean_comments = clean_comments
@@ -33,12 +48,14 @@ def _get_json(self, path):
3348
return res.json()
3449

3550
def get_thread(self, thread_id, update_if_cached=True):
36-
"""
37-
Get a thread from 4chan via 4chan API.
51+
"""Get a thread from 4chan via 4chan API.
52+
53+
Args:
54+
thread_id (int): Thread ID
55+
update_if_cached (bool): Whether the thread should be updated if it's already in our cache
3856
39-
:param thread_id: Thread ID
40-
:param update_if_cached: Should the thread be updated if it's found in the cache?
41-
:return: Thread
57+
Returns:
58+
:class:`basc_py4chan.Thread`: Thread object
4259
"""
4360
if thread_id in self._thread_cache:
4461
thread = self._thread_cache[thread_id]
@@ -55,11 +72,13 @@ def get_thread(self, thread_id, update_if_cached=True):
5572
return thread
5673

5774
def thread_exists(self, thread_id):
58-
"""
59-
Check if a thread exists, or is 404.
75+
"""Check if a thread exists or has 404'd.
6076
61-
:param thread_id: Thread ID
62-
:return: bool
77+
Args:
78+
thread_id (int): Thread ID
79+
80+
Returns:
81+
bool: Whether the given thread exists on this board.
6382
"""
6483
return self._requests_session.head(self._thread_path % thread_id).ok
6584

@@ -96,38 +115,48 @@ def _request_threads(self, page):
96115
return threads
97116

98117
def get_threads(self, page=1):
99-
"""
100-
Get all threads on a certain page. Pages are now indexed at 1, instead of 0.
101-
If the thread is already in cache, return cached thread entry.
118+
"""Returns all threads on a certain page.
119+
120+
Gets a list of Thread objects for every thread on the given page. If a thread is
121+
already in our cache, the cached version is returned and thread.want_update is
122+
set to True on the specific thread object.
123+
124+
Pages on 4chan are indexed from 1 onwards.
125+
126+
Args:
127+
page (int): Page to request threads for. Defaults to the first page.
102128
103-
Sets thread.want_update to True if the thread is being returned from the cache.
104-
:param: page: Page to fetch thread from
105-
:return: list[Thread]
129+
Returns:
130+
list: List of Thread objects representing the threads on the given page.
106131
"""
107132
return self._request_threads(page)
108133

109134
def get_all_thread_ids(self):
110-
"""
111-
Return a list of all thread IDs.
135+
"""Return the ID of every thread on this board.
112136
113-
:return: list[int]
137+
Returns:
138+
list: List of IDs of every thread on this board.
114139
"""
115140
json = self._get_json(ALL_THREADS)
116141
return [thread['no'] for page in json for thread in page['threads']]
117142

118143
def get_all_threads(self, expand=False):
119-
"""
120-
Return all threads on all pages.
144+
"""Return every thread on this board.
145+
146+
If not expanded, result is same as get_threads run across all board pages,
147+
with last 3-5 replies included.
121148
122-
If not expanded, result is same as get_threads run across all board pages,
123-
with last 3-5 replies included.
149+
Uses the catalog when not expanding, and uses the flat thread ID listing
150+
at /{board}/threads.json when expanding for more efficient resource usage.
124151
125-
Uses the catalog when not expanding, and uses the flat thread ID listing
126-
at /{board}/threads.json when expanding for more efficient resource usage.
152+
If expanded, all data of all threads is returned with no omitted posts.
127153
128-
If expanded, all data of all threads is returned with no omitted posts.
129-
:param: expand: Whether or not to expand threads
130-
:return: list[Thread]
154+
Args:
155+
expand (bool): Whether to download every single post of every thread.
156+
If enabled, this option can be very slow and bandwidth-intensive.
157+
158+
Returns:
159+
list: List of Thread objects representing every thread on this board.
131160
"""
132161
if not expand:
133162
return self._request_threads(CATALOG)
@@ -138,27 +167,19 @@ def get_all_threads(self, expand=False):
138167
return filter(None, threads)
139168

140169
def refresh_cache(self, if_want_update=False):
141-
"""
142-
Update all threads currently stored in the cache.
143-
"""
170+
"""Update all threads currently stored in our cache."""
144171
for thread in self._thread_cache.values():
145172
if if_want_update:
146173
if not thread.want_update:
147174
continue
148175
thread.update()
149176

150177
def clear_cache(self):
151-
"""
152-
Remove everything currently stored in the cache.
153-
"""
178+
"""Remove everything currently stored in our cache."""
154179
self._thread_cache.clear()
155180

156181
@property
157182
def name(self):
158-
"""
159-
Board name that this board represents.
160-
:return: string
161-
"""
162183
return self._board_name
163184

164185
def __repr__(self):

docs/conf.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
import sys, os
1515

16+
# on_rtd is whether we are on readthedocs.org, this line of code grabbed from docs.readthedocs.org
17+
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
18+
19+
if not on_rtd: # only import and set the theme if we're building docs locally
20+
import sphinx_rtd_theme
21+
html_theme = 'sphinx_rtd_theme'
22+
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
23+
24+
# otherwise, readthedocs.org uses their theme by default, so no need to specify it
25+
1626
# If extensions (or modules to document with autodoc) are in another directory,
1727
# add these directories to sys.path here. If the directory is relative to the
1828
# documentation root, use os.path.abspath to make it absolute, like shown here.
@@ -26,7 +36,7 @@
2636

2737
# Add any Sphinx extension module names here, as strings. They can be extensions
2838
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
29-
extensions = ['sphinx.ext.coverage', 'sphinx.ext.autodoc', 'sphinx.ext.viewcode']
39+
extensions = ['sphinx.ext.coverage', 'sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinxcontrib.napoleon']
3040

3141
# Add any paths that contain templates here, relative to this directory.
3242
templates_path = ['_templates']
@@ -92,7 +102,7 @@
92102

93103
# The theme to use for HTML and HTML Help pages. See the documentation for
94104
# a list of builtin themes.
95-
html_theme = 'default'
105+
# html_theme = 'default'
96106

97107
# Theme options are theme-specific and customize the look and feel of a theme
98108
# further. For a list of options available for each theme, see the

docs/index.rst

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
.. BASC-py4chan documentation master file, created by
2-
sphinx-quickstart on Fri Dec 5 08:19:29 2014.
3-
You can adapt this file completely to your liking, but it should at least
4-
contain the root `toctree` directive.
1+
BASC-py4chan - 4chan Python Library
2+
===================================
3+
BASC-py4chan is a Python library that gives access to the 4chan API and an object-oriented way to browse and get board and thread information quickly and easily.
54

6-
Welcome to BASC-py4chan's documentation!
7-
========================================
5+
Originally written by `Edgeworth <https://github.com/e000/py-4chan>`_, the library has been adopted and extended by `Bibliotheca Anonoma <https://github.com/bibanon>`_.
6+
7+
We're currently writing up the documentation for this module, so stick with us while we flesh out the library documentation and the rest of it!
88

99
Contents:
1010

1111
.. toctree::
1212
:maxdepth: 2
1313

14-
15-
Indices and tables
16-
==================
17-
18-
* :ref:`genindex`
19-
* :ref:`modindex`
20-
* :ref:`search`
21-
14+
library/board

docs/library/board.rst

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ Here is a sample application that grabs and uses Board information, :file:`examp
1616
.. module:: basc_py4chan
1717
.. autoclass:: basc_py4chan.Board
1818

19+
.. automethod:: basc_py4chan.Board.__init__
20+
1921
.. automethod:: basc_py4chan.Board.thread_exists
2022

2123
.. automethod:: basc_py4chan.Board.get_thread
24+
25+
.. automethod:: basc_py4chan.Board.get_threads
26+
27+
.. automethod:: basc_py4chan.Board.get_all_threads
28+
29+
.. automethod:: basc_py4chan.Board.get_all_thread_ids
30+
31+
.. automethod:: basc_py4chan.Board.refresh_cache
32+
33+
.. automethod:: basc_py4chan.Board.clear_cache
34+
35+
.. autoattribute:: basc_py4chan.Board.name

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests
2+
sphinxcontrib-napoleon

0 commit comments

Comments
 (0)