From 74bba08955c79b3d7abbb324ed54559570561d97 Mon Sep 17 00:00:00 2001 From: Scott Date: Wed, 7 Dec 2016 01:28:28 -0600 Subject: [PATCH] Allows user to generate praw and shreddit configs --- setup.py | 2 +- shreddit/app.py | 20 +++++++++++++++++--- shreddit/praw.ini.example | 7 +++++++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 shreddit/praw.ini.example diff --git a/setup.py b/setup.py index d11cfb4..abd4453 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ setup( "Programming Language :: Python"], packages=["shreddit"], install_requires=["arrow", "backports-abc", "praw>=4", "prawcore", "PyYAML", "requests", "six", "tornado"], - package_data={"shreddit": ["shreddit.yml.example"]}, + package_data={"shreddit": ["*.example"]}, entry_points={ "console_scripts": [ "shreddit=shreddit.app:main" diff --git a/shreddit/app.py b/shreddit/app.py index d16b0e8..1678805 100644 --- a/shreddit/app.py +++ b/shreddit/app.py @@ -12,14 +12,28 @@ from shreddit.shredder import Shredder def main(): parser = argparse.ArgumentParser(description="Command-line frontend to the shreddit library.") parser.add_argument("-c", "--config", help="Config file to use instead of the default shreddit.yml") + parser.add_argument("-g", "--generate-configs", help="Write shreddit and praw config files to current directory.", + action="store_true") parser.add_argument("-u", "--user", help="User section from praw.ini if not default", default="default") args = parser.parse_args() + if args.generate_configs: + if not os.path.isfile("shreddit.yml"): + print("Writing shreddit.yml file...") + with open("shreddit.yml", "w") as fout: + fout.write(pkg_resources.resource_string("shreddit", "shreddit.yml.example")) + if not os.path.isfile("praw.ini"): + print("Writing praw.ini file...") + with open("praw.ini", "w") as fout: + fout.write(pkg_resources.resource_string("shreddit", "praw.ini.example")) + return + config_filename = args.config or "shreddit.yml" if not os.path.isfile(config_filename): - print("No configuration file could be found. Paste the following into a file called \"shreddit.yml\" and " \ - "try running shreddit again:\n\n") - print(pkg_resources.resource_string("shreddit", "shreddit.yml.example")) + print("No shreddit configuration file was found or provided. Run this script with -g to generate one.") + return + if not os.path.isfile("praw.ini"): + print("No praw configuration file was found. Run this script with -g to generate one.") return with open(config_filename) as fh: diff --git a/shreddit/praw.ini.example b/shreddit/praw.ini.example new file mode 100644 index 0000000..47ee85e --- /dev/null +++ b/shreddit/praw.ini.example @@ -0,0 +1,7 @@ +# Credentials go here. Fill out default, or provide one or more names and call shreddit with the -u option to specify +# which set to use. +[default] +client_id= +client_secret= +username= +password=