Skip to content

Commit

Permalink
Create default config file if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
kchristensen committed Apr 26, 2018
1 parent 8bb1d8f commit e0569c8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 23 additions & 2 deletions slappd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# Standard Library Imports
import os
import re
import shutil
import sys

# First Party Imports
Expand All @@ -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)

Expand Down
File renamed without changes.

0 comments on commit e0569c8

Please sign in to comment.