diff --git a/ChangeLog b/ChangeLog index 0da2e5e..6e65604 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,19 @@ CHANGES ======= +0.1.2 +----- + +* Add retry logic for ConnectionError exceptions + +0.1.1 +----- + * Update README with virtualenv instructions + +0.1.0 +----- + * Update classifiers for beta release * Fix timestamping * Fix setup.cfg formatting diff --git a/recho/cli.py b/recho/cli.py index b80c226..d877338 100644 --- a/recho/cli.py +++ b/recho/cli.py @@ -10,9 +10,7 @@ from recho.recho import get_reddit_posts_since, post_to_slack CONFIG_NAME = '.recho.ini' - TS_NAME = '.recho_timestamps' -TS_FILEPATH = os.path.join(os.path.dirname(__file__), TS_NAME) class RechoError(Exception): @@ -26,35 +24,49 @@ def _get_args(): help="The name of the target Redditor" ) + parser.add_argument( + '-V', '--version', action='version', + version='{version}'.format(version=recho_version) + ) + + default_config_path = os.path.join(os.path.expanduser('~'), CONFIG_NAME) + parser.add_argument( + '--config', type=str, default="{0}".format(default_config_path), + help="Location of the recho config file") + + default_ts_path = os.path.join(os.path.dirname(__file__), TS_NAME) + parser.add_argument( + '--timestamp-file', type=str, default="{0}".format(default_ts_path), + help="Location of the recho config file") + return parser.parse_args() -def _load_config(): +def _load_config(config_path): """ Load the configuration file """ config = ConfigParser() - config_path = os.path.join(os.path.expanduser('~'), CONFIG_NAME) if not os.path.exists(config_path): - raise RechoError("Config file must be present in ~/.recho.ini") + raise RechoError("Could not find config file at: {0}".format(config_path)) config.read(config_path) return config -def _get_last_seen(redditor): +def _get_last_seen(redditor, ts_path): """ Return the last time a check was done """ - if not os.path.exists(TS_FILEPATH): - with open(TS_FILEPATH, 'w') as w: # pylint: disable=C0103 + if not os.path.exists(ts_path): + with open(ts_path, 'w') as w: # pylint: disable=C0103 w.write(json.dumps({})) - with open(TS_FILEPATH, 'r') as rfile: + with open(ts_path, 'r') as rfile: timestamps = json.load(rfile) if redditor not in timestamps: last_seen = dt.utcnow() timestamps[redditor] = last_seen.timestamp() - with open(TS_FILEPATH, 'w') as w: # pylint: disable=C0103 + with open(ts_path, 'w') as w: # pylint: disable=C0103 w.write(json.dumps(timestamps, indent=4, sort_keys=True)) else: last_seen = dt.fromtimestamp(float(timestamps[redditor])) @@ -67,11 +79,11 @@ def main(): args = _get_args() # Load config - config = _load_config() + config = _load_config(args.config) try: # Get last timestamp - last_seen = _get_last_seen(args.redditor) + last_seen = _get_last_seen(args.redditor, args.timestamp_file) # Get new comments and threads from reddit new_posts = get_reddit_posts_since(args.redditor, last_seen) @@ -81,11 +93,11 @@ def main(): latest_timestamp = post_to_slack(config['slack'], new_posts) # Write new timestamp - with open(TS_FILEPATH, 'r') as r: # pylint: disable=C0103 + with open(args.timestamp_file, 'r') as r: # pylint: disable=C0103 timestamps = json.load(r) timestamps[args.redditor] = latest_timestamp.timestamp() - with open(TS_FILEPATH, 'w') as w: # pylint: disable=C0103 + with open(args.timestamp_file, 'w') as w: # pylint: disable=C0103 w.write(json.dumps(timestamps, indent=4, sort_keys=True)) except: # Log to sentry, if configured