Skip to content

Commit 5c08350

Browse files
authored
Merge pull request #2 from realpython/docs
Add docstrings to all modules, and a simple help text to the program
2 parents 64b3ddf + bdbd941 commit 5c08350

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

reader/__main__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1+
"""Read the latest Real Python tutorials
2+
3+
Usage:
4+
------
5+
6+
List the latest tutorials:
7+
8+
$ realpython
9+
10+
Read one tutorial:
11+
12+
$ realpython <id>
13+
14+
where <id> is the number shown when listing tutorials.
15+
16+
Read the latest tutorial:
17+
18+
$ realpython 0
19+
20+
21+
Contact:
22+
--------
23+
24+
- https://realpython.com/contact/
25+
26+
More information is available at:
27+
28+
- https://pypi.org/project/realpython-reader/
29+
- https://github.com/realpython/reader
30+
"""
31+
# Standard library imports
132
import sys
233

34+
# Reader imports
335
from reader import feed
436
from reader import viewer
537

638

739
def main() -> None:
840
"""Read the Real Python article feed"""
41+
# Show help message
42+
if "-h" in sys.argv or "--help" in sys.argv:
43+
viewer.show(__doc__)
44+
return
45+
946
# An article ID is given, show article
1047
if len(sys.argv) > 1:
1148
article = feed.get_article(sys.argv[1])

reader/feed.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
from typing import List
1+
"""Interact with the Real Python feed"""
2+
# Standard library imports
3+
from typing import Dict, List
4+
5+
# Third party imports
26
import feedparser
37
import html2text
4-
import reader
58

6-
_CACHED_FEED = feedparser.FeedParserDict()
9+
# Reader imports
10+
from reader import URL
11+
_CACHED_FEEDS: Dict[str, feedparser.FeedParserDict] = dict()
712

813

914
def _feed() -> feedparser.FeedParserDict:
1015
"""Cache contents of the feed, so it's only read once"""
11-
if not _CACHED_FEED:
12-
_CACHED_FEED.update(feedparser.parse(reader.URL))
13-
return _CACHED_FEED
16+
if URL not in _CACHED_FEEDS:
17+
_CACHED_FEEDS[URL] = feedparser.parse(URL)
18+
return _CACHED_FEEDS[URL]
1419

1520

1621
def get_site() -> str:
@@ -25,9 +30,14 @@ def get_article(article_id: str) -> str:
2530
try:
2631
article = articles[int(article_id)]
2732
except (IndexError, ValueError):
28-
raise SystemExit("Error: Unknown article ID")
33+
max_id = len(articles) - 1
34+
msg = f"Unknown article ID, use ID from 0 to {max_id}"
35+
raise SystemExit(f"Error: {msg}")
2936

30-
html = article.content[0].value
37+
try:
38+
html = article.content[0].value
39+
except AttributeError:
40+
html = article.summary
3141
text = html2text.html2text(html)
3242
return f"# {article.title}\n\n{text}"
3343

reader/viewer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""Functions for displaying the Real Python feed"""
2+
3+
# Standard library imports
14
from typing import List
25

36

setup.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
"""Setup script for realpython-reader"""
2+
13
import pathlib
2-
from setuptools import find_packages, setup
4+
from setuptools import setup
35

46
# The directory containing this file
57
HERE = pathlib.Path(__file__).parent
68

9+
# The text of the README file
10+
README = (HERE / "README.md").read_text()
11+
712

13+
# This call to setup() does all the work
814
setup(
915
name="realpython-reader",
1016
version="0.0.1",
1117
description="Read Real Python Tutorials",
12-
long_description=(HERE / "README.md").read_text(),
18+
long_description=README,
1319
long_description_content_type="text/markdown",
1420
url="https://github.com/realpython/reader",
1521
author="Real Python",
@@ -20,7 +26,7 @@
2026
"Programming Language :: Python",
2127
"Programming Language :: Python :: 3",
2228
],
23-
packages=find_packages(exclude=("tests",)),
29+
packages=["reader"],
2430
install_requires=["feedparser", "html2text"],
2531
entry_points={
2632
"console_scripts": [

0 commit comments

Comments
 (0)