From 6224d06d9fcd5cfa06c5ad939ccb0e3ced8bb85c Mon Sep 17 00:00:00 2001 From: Karl Dubost Date: Thu, 16 Nov 2017 11:28:34 +0900 Subject: [PATCH] Issue #1876 - Handles when data/milestones.json doesn't exist. --- config/__init__.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/config/__init__.py b/config/__init__.py index 63172dbc9..5ce72946d 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -17,9 +17,14 @@ from environment import * # nopep8 from secrets import * # nopep8 -MILESTONE_ERROR = '''It failed with {msg}! +MILESTONE_ERROR = """It failed with {msg}! We will read from data/milestones.json. -''' +""" +MILESTONE_MISSING_FILE = """Oooops. +We can't find {path} +Double check that everything is configured properly +in config/secrets.py and try again. Good luck! +""" def initialize_status(): @@ -49,8 +54,7 @@ def initialize_status(): # Not working, let's use the cached copy # This might fail the first time. print(MILESTONE_ERROR.format(msg=error)) - with open(milestones_path, 'r') as f: - milestones_content = f.read() + milestones_content = milestones_from_file(milestones_path) finally: # save in data/ the current version if milestones_content: @@ -62,6 +66,17 @@ def initialize_status(): return False +def milestones_from_file(milestones_path): + """Attempt to read the milestones data from the filesystem.""" + if os.path.isfile(milestones_path): + with open(milestones_path, 'r') as f: + milestones_content = f.read() + return milestones_content + else: + print(MILESTONE_MISSING_FILE.format(path=milestones_path)) + return None + + def convert_milestones(milestones_content): """Convert the JSON milestones from GitHub to a simple dict.""" milestone_full = json.loads(milestones_content)