From c3d205d6f11080bbf4feb8020dae3fe9024ef774 Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Thu, 2 Dec 2021 18:44:19 +0800 Subject: [PATCH] fixed odd orders --- src/tree.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/tree.py b/src/tree.py index ba92b46..ae5c7e5 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): -- GitLab