From e0569c80b9c0456e16b5eec4d2fa0b870ffe8f9b Mon Sep 17 00:00:00 2001 From: Kyle Christensen Date: Thu, 26 Apr 2018 07:19:32 -0400 Subject: [PATCH] Create default config file if it doesn't exist --- README.md | 11 +++++----- makefile | 6 ------ slappd/__main__.py | 25 ++++++++++++++++++++-- slappd/templates/{slappd.cfg => config.j2} | 0 4 files changed, 29 insertions(+), 13 deletions(-) rename slappd/templates/{slappd.cfg => config.j2} (100%) diff --git a/README.md b/README.md index 18cf337..57333dd 100644 --- a/README.md +++ b/README.md @@ -19,23 +19,24 @@ This script is designed to be run from crontab, and issues one API call per run. ### Requirements -* Python 3.5+ (It may run on >= 3.0, but I have not tested.) -* Some Python modules (configparser, Jinja2, simplejson, requests) +* Docker, or the ability to create a [Virtual Environment](https://docs.python.org/3/tutorial/venv.html) +* Python 3.5+ and a couple of common Python modules: (configparser, Jinja2, requests) * A way of periodically running this script (at, cron, etc) * Untappd [API access](https://untappd.com/api/register?register=new) * A Slack channel full of beer lovers ### Installation & Configuration -If this is your first time, create the default config file with `make config` -and then edit it (`~/.config/slappd/slappd.cfg`) to reflect your API information -and friends list. +If this is your first time running Slappd, it will attempt to create a config file +(`~/.config/slappd/slappd.cfg`). You should edit it to reflect your Untappd API +information and friends list. #### Docker * Run `make docker-run` to build and run Slappd. **Note:** This mounts the config you created earlier into the container to keep track of which check-ins it has seen. +* Run it from crontab: `*/5 * * * cd /path/to/slappd/source && make docker-run > /dev/null 2>&1` #### Virtualenv diff --git a/makefile b/makefile index ef9ee48..7bd0eb4 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,5 @@ SHELL = /bin/bash VIRTUALENV_DIR = ${HOME}/.virtualenv -CFG_DIR = ${HOME}/.config/slappd - -.PHONY: config -config: - mkdir -p ${CFG_DIR} - test -f ${CFG_DIR}/slappd.cfg || cp -p slappd/templates/slappd.cfg ${CFG_DIR}/slappd.cfg .PHONY: dev dev: ${VIRTUALENV_DIR}/slappd diff --git a/slappd/__main__.py b/slappd/__main__.py index 79e1fc5..49d18d5 100644 --- a/slappd/__main__.py +++ b/slappd/__main__.py @@ -26,6 +26,7 @@ # Standard Library Imports import os import re +import shutil import sys # First Party Imports @@ -51,12 +52,32 @@ def check_for_photos(checkins): return True +def config_copy(): + """ Copy config file template to the proper location """ + config_dst = get_cfg_path() + config_src = '{}/templates/config.j2'.format(os.path.dirname(__file__)) + config_dir = os.path.dirname(config_dst) + + print('Configuration file {} does not exist, attempting to create it.' + .format(config_dst)) + if not os.path.exists(config_dir): + try: + os.makedirs(config_dir, exist_ok=True) + except IOError: + sys.exit('Error: Could not create directory {}'.format(config_dir)) + try: + shutil.copy(config_src, config_dst) + sys.exit('Successfully created configuration file, please edit ' + 'it to reflect your API information.') + except IOError: + sys.exit('Error: Could not write to configuration file {}'.format(config_dst)) + + def config_load(): """ Load configuration options from file """ config_file = get_cfg_path() if not os.path.exists(config_file): - sys.exit('Error: Configuration file {} does not exist' - .format(config_file)) + config_copy() else: CONFIG.read(config_file) diff --git a/slappd/templates/slappd.cfg b/slappd/templates/config.j2 similarity index 100% rename from slappd/templates/slappd.cfg rename to slappd/templates/config.j2