diff --git a/src/tree.py b/src/tree.py index ba92b46ebaf95480da000ae651ff1657a33a3f6d..ae5c7e5861641fa56461104badcfefcaecc8412a 100644 --- a/src/tree.py +++ b/src/tree.py @@ -326,26 +326,30 @@ class TreeWalker: def ensure_exercises_meta(self, meta_path, source): _, mfile = os.path.split(meta_path) + meta = None if os.path.exists(meta_path): - meta = load_json(meta_path) - if "exercise_id" not in meta: - meta["exercise_id"] = uuid.uuid4().hex - if "notebook_enable" not in meta: - meta["notebook_enable"] = self.default_notebook() - if "source" not in meta: - meta["source"] = source - if "author" not in meta: - meta["author"] = user_name() - if "type" not in meta: - meta["type"] = "code_options" - else: - meta = { - "type": "code_options", - "author": user_name(), - "source": source, - "notebook_enable": self.default_notebook(), - "exercise_id": uuid.uuid4().hex - } + with open(meta_path) as f: + content = f.read() + if content: + meta = json.loads(content) + if "exercise_id" not in meta: + meta["exercise_id"] = uuid.uuid4().hex + if "notebook_enable" not in meta: + meta["notebook_enable"] = self.default_notebook() + if "source" not in meta: + meta["source"] = source + if "author" not in meta: + meta["author"] = user_name() + if "type" not in meta: + meta["type"] = "code_options" + if meta is None: + meta = { + "type": "code_options", + "author": user_name(), + "source": source, + "notebook_enable": self.default_notebook(), + "exercise_id": uuid.uuid4().hex + } dump_json(meta_path, meta, True, True) def default_notebook(self):