Skip to content

Commit b119f5f

Browse files
committed
Copy from PRAW
1 parent 9007b94 commit b119f5f

16 files changed

+278
-776
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
*.egg-info/
33
*.eggs/
44
*.pyc
5-
*~
6-
.DS_Store
75
.cache/
86
.coverage
7+
.pytest_cache/
8+
.tox/
99
_build/
1010
build/
1111
dist/

LICENSE.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2016, Randall Goodman
1+
Copyright (c) 2020, PokestarFan
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

README.rst

+2-19
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,9 @@ Install prawdditions using ``pip`` via:
1818
1919
pip install prawdditions
2020
21-
22-
Usage
23-
-----
24-
25-
The following example demonstrates how to use prawdditions...
26-
27-
.. code-block:: python
28-
29-
import praw
30-
import prawdditions
31-
32-
#Instantiate Reddit Client
33-
reddit = praw.Reddit(...)
34-
35-
#Abstract message function now available.
36-
reddit.message('user_or_subreddit','subject','body')
37-
3821
Contact
3922
-------
4023

41-
Author: Randy Goodman
24+
Author: PokestarFan
4225

43-
26+
Please file a Github Issue if you have any problems.

prawdditions/__init__.py

+1-63
Original file line numberDiff line numberDiff line change
@@ -1,63 +1 @@
1-
import json
2-
3-
4-
from praw import Reddit, const
5-
from praw.models.reddit.wikipage import WikiPage
6-
from prawcore.exceptions import Conflict
7-
8-
9-
def message(self, to, title, body, from_sr=None):
10-
"""Abstract function for sending out a message via string.
11-
12-
:param to: Destination of the message.
13-
:param title: The subject line of the message.
14-
:param body: The body of the message.
15-
:param from_sr: A Subreddit instance of string to send the message from.
16-
By default the message originates from the user.
17-
:returns: The json response from the server.
18-
19-
example:
20-
21-
.. code-block:: python
22-
23-
reddit = praw.Reddit(client_id='CLIENT_ID',
24-
client_secret="CLIENT_SECRET", password='PASSWORD',
25-
user_agent='USERAGENT', username='USERNAME')
26-
reddit.message('username','title','body')
27-
28-
"""
29-
data = {'subject': title,
30-
'text': body,
31-
'to': to}
32-
if from_sr:
33-
data['from_sr'] = str(from_sr)
34-
return self.post(const.API_PATH['compose'], data=data)
35-
36-
37-
def update(self, transformation, reason=None):
38-
"""Safely update a page based on its current content.
39-
40-
:param transformation: A function taking the previous content as its
41-
sole parameter and returning the new content.
42-
:param reason: (Optional) The reason for the revision.
43-
44-
"""
45-
current_revision = next(self.revisions(limit=1))
46-
revision_id = current_revision['id']
47-
content = current_revision['page'].content_md
48-
new_content = transformation(content)
49-
while True:
50-
try:
51-
self.edit(new_content, reason=reason, previous=revision_id)
52-
return
53-
except Conflict as conflict:
54-
response_body = json.loads(conflict.response.content.decode())
55-
new_content = transformation(response_body['newcontent'])
56-
revision_id = response_body['newrevision']
57-
58-
"""
59-
Attach the message function to the Reddit object.
60-
Instantiations of the reddit object will now contain the message function.
61-
"""
62-
Reddit.message = message
63-
WikiPage.update = update
1+
"""Top level of the PRAWdittions package."""

prawdditions/const.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
__version__ = '0.1.2'
1+
"""PRAWdittions constants."""
2+
3+
__version__ = "0.2.0"

pre_push.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
"""Run static analysis on the project."""
3+
4+
import argparse
5+
import sys
6+
from os import path
7+
from subprocess import CalledProcessError, check_call
8+
9+
current_directory = path.abspath(path.join(__file__, ".."))
10+
11+
12+
def do_process(args, shell=False):
13+
"""Run program provided by args.
14+
15+
Return True on success.
16+
17+
Output failed message on non-zero exit and return False.
18+
19+
Exit if command is not found.
20+
"""
21+
print("Running: {}".format(" ".join(args)))
22+
try:
23+
check_call(args, shell=shell)
24+
except CalledProcessError:
25+
print("\nFailed: {}".format(" ".join(args)))
26+
return False
27+
except Exception as exc:
28+
sys.stderr.write(str(exc) + "\n")
29+
sys.exit(1)
30+
return True
31+
32+
33+
def run_static():
34+
"""Runs the static tests.
35+
36+
Returns a statuscode of 0 if everything ran correctly.
37+
Otherwise, it will return statuscode 1
38+
"""
39+
success = True
40+
success &= do_process(["black", "."])
41+
success &= do_process(["flake8", "--exclude=.eggs,build,docs"])
42+
success &= do_process(["pydocstyle", "prawdditions"])
43+
return success
44+
45+
46+
def run_unit():
47+
"""Runs the unit-tests.
48+
49+
Follows the behavior of the static tests,
50+
where any failed tests cause pre_push.py to fail.
51+
"""
52+
return do_process([sys.executable, "pytest"])
53+
54+
55+
def main():
56+
"""Runs the main function.
57+
58+
usage: pre_push.py [-h] [-n] [-u] [-a]
59+
60+
Run static and/or unit-tests
61+
"""
62+
parser = argparse.ArgumentParser(
63+
description="Run static and/or unit-tests"
64+
)
65+
parser.add_argument(
66+
"-n",
67+
"--unstatic",
68+
action="store_true",
69+
help="Do not run static tests (black/flake8/pydocstyle/sphinx-build)",
70+
default=False,
71+
)
72+
parser.add_argument(
73+
"-u",
74+
"--unit-tests",
75+
"--unit",
76+
action="store_true",
77+
default=False,
78+
help="Run the unit tests",
79+
)
80+
parser.add_argument(
81+
"-a",
82+
"--all",
83+
action="store_true",
84+
default=False,
85+
help="Run all of the tests (static and unit). "
86+
"Overrides the unstatic argument.",
87+
)
88+
args = parser.parse_args()
89+
success = True
90+
try:
91+
if not args.unstatic or args.all:
92+
success &= run_static()
93+
if args.all or args.unit_tests:
94+
success &= run_unit()
95+
except KeyboardInterrupt:
96+
return int(not False)
97+
return int(not success)
98+
99+
100+
if __name__ == "__main__":
101+
exit_code = main()
102+
print(
103+
"\npre_push.py: Success!" if not exit_code else "\npre_push.py: Fail"
104+
)
105+
sys.exit(exit_code)

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tool.black]
2+
exclude = '/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist)/'
3+
line-length = 79

pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[pytest]
22
testpaths = tests
3+
filterwarnings =
4+
ignore::DeprecationWarning

setup.py

+48-34
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,56 @@
66
from setuptools import find_packages, setup
77

88

9-
PACKAGE_NAME = 'prawdditions'
9+
PACKAGE_NAME = "prawdditions"
1010
HERE = path.abspath(path.dirname(__file__))
11-
with open(path.join(HERE, 'README.rst'), encoding='utf-8') as fp:
11+
with open(path.join(HERE, "README.rst"), encoding="utf-8") as fp:
1212
README = fp.read()
13-
with open(path.join(HERE, PACKAGE_NAME, 'const.py'),
14-
encoding='utf-8') as fp:
13+
with open(path.join(HERE, PACKAGE_NAME, "const.py"), encoding="utf-8") as fp:
1514
VERSION = re.search("__version__ = '([^']+)'", fp.read()).group(1)
1615

16+
extras = {
17+
"ci": ["coveralls"],
18+
"dev": ["pre-commit"],
19+
"lint": ["black", "flake8", "pydocstyle", "sphinx", "sphinx_rtd_theme"],
20+
"test": [
21+
"betamax >=0.8, <0.9",
22+
"betamax-matchers >=0.3.0, <0.5",
23+
"betamax-serializers >=0.2, <0.3",
24+
"mock >=0.8",
25+
"pytest >=2.7.3",
26+
],
27+
}
1728

18-
setup(name=PACKAGE_NAME,
19-
author='Randall Goodman',
20-
author_email='[email protected]',
21-
classifiers=[
22-
'Development Status :: 4 - Beta',
23-
'Intended Audience :: Developers',
24-
'License :: OSI Approved :: BSD License',
25-
'Natural Language :: English',
26-
'Programming Language :: Python',
27-
'Programming Language :: Python :: 2.7',
28-
'Programming Language :: Python :: 3',
29-
'Programming Language :: Python :: 3.3',
30-
'Programming Language :: Python :: 3.4',
31-
'Programming Language :: Python :: 3.5',
32-
'Programming Language :: Python :: Implementation :: CPython'],
33-
description='DESCRIPTION.',
34-
install_requires=['praw >=5.0.0, <7.0'],
35-
keywords='praw additions',
36-
license='Simplified BSD License',
37-
long_description=README,
38-
packages=find_packages(exclude=['tests', 'tests.*']),
39-
setup_requires=['pytest-runner ==2.7'],
40-
tests_require=['betamax >=0.8, <0.9',
41-
'betamax-matchers >=0.3.0, <0.4',
42-
'betamax-serializers >=0.2, <0.3',
43-
'mock ==1.0.1',
44-
'pytest ==2.8.7'],
45-
test_suite='tests',
46-
url='https://github.com/praw-dev/prawdditions',
47-
version=VERSION)
29+
extras["dev"] += extras["lint"] + extras["test"]
30+
31+
setup(
32+
name=PACKAGE_NAME,
33+
author="PokestarFan",
34+
author_email="[email protected]",
35+
classifiers=[
36+
"Development Status :: 4 - Beta",
37+
"Environment :: Console",
38+
"Intended Audience :: Developers",
39+
"License :: OSI Approved :: BSD License",
40+
"Natural Language :: English",
41+
"Operating System :: OS Independent",
42+
"Programming Language :: Python",
43+
"Programming Language :: Python :: 3",
44+
"Programming Language :: Python :: 3.5",
45+
"Programming Language :: Python :: 3.6",
46+
"Programming Language :: Python :: 3.7",
47+
"Programming Language :: Python :: 3.8",
48+
"Topic :: Utilities",
49+
],
50+
description="High-level utilities for PRAW.",
51+
extras_require=extras,
52+
install_requires=["praw < 7.0"],
53+
keywords="praw additions",
54+
license="Simplified BSD License",
55+
long_description=README,
56+
packages=find_packages(exclude=["tests", "tests.*"]),
57+
package_data={"": ["LICENSE.txt"]},
58+
test_suite="tests",
59+
url="https://github.com/praw-dev/prawdditions",
60+
version=VERSION,
61+
)

0 commit comments

Comments
 (0)