diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/6.\346\225\260\346\215\256\345\272\223\346\211\251\345\261\225/extension.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/6.\346\225\260\346\215\256\345\272\223\346\211\251\345\261\225/extension.json" index edadd0426fbefb339263bd924e21868742aa12e0..dc0900e74024d6c4e65a144b08aa7ffd08a140ee 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/6.\346\225\260\346\215\256\345\272\223\346\211\251\345\261\225/extension.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/6.\346\225\260\346\215\256\345\272\223\346\211\251\345\261\225/extension.json" @@ -2,5 +2,6 @@ "type": "code_options", "author": "ccat", "source": "extension.md", - "notebook_enable": false + "notebook_enable": false, + "exercise_id": "fad124e7fe1a420dbfbe72de8d4b3f11" } \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/6.\346\225\260\346\215\256\345\272\223\346\211\251\345\261\225/language.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/6.\346\225\260\346\215\256\345\272\223\346\211\251\345\261\225/language.json" index a34c6527a7e895a6220e9e4e7733afc55d4ee6bf..b3d1995105f96c6e531fe5a00e50f0c946d9af43 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/6.\346\225\260\346\215\256\345\272\223\346\211\251\345\261\225/language.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/6.\346\225\260\346\215\256\345\272\223\346\211\251\345\261\225/language.json" @@ -2,5 +2,6 @@ "type": "code_options", "author": "ccat", "source": "language.md", - "notebook_enable": false + "notebook_enable": false, + "exercise_id": "4657a4c34eec421a913625fb61af00d4" } \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/1.\350\201\232\345\220\210\345\222\214\345\210\206\347\273\204/salary.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/1.\350\201\232\345\220\210\345\222\214\345\210\206\347\273\204/salary.json" index 29941fc2ef932c7d2164bedf10f23675800a3b6b..d1ed8c23dad61317d7b9637fd888d086b297313b 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/1.\350\201\232\345\220\210\345\222\214\345\210\206\347\273\204/salary.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/1.\350\201\232\345\220\210\345\222\214\345\210\206\347\273\204/salary.json" @@ -2,5 +2,6 @@ "type": "code_options", "author": "ccat", "source": "salary.md", - "notebook_enable": false + "notebook_enable": false, + "exercise_id": "31deab40b4af44c38c363b2333bdd977" } \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/2.\350\277\236\346\216\245\346\237\245\350\257\242/customer_order.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/2.\350\277\236\346\216\245\346\237\245\350\257\242/customer_order.json" index 441330779c2951b818148f8313729c7b6cd8985e..3988606e13fec39500d3941561d8fc49bffd77db 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/2.\350\277\236\346\216\245\346\237\245\350\257\242/customer_order.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/2.\350\277\236\346\216\245\346\237\245\350\257\242/customer_order.json" @@ -2,5 +2,6 @@ "type": "code_options", "author": "ccat", "source": "customer_order.md", - "notebook_enable": false + "notebook_enable": false, + "exercise_id": "18d5a22c34874bb4b73aac6846674243" } \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.json" index b8caa4a3bc450d70c6a15fee08197751b2ed3353..d0b0d05f9fea5392f3d1a62851eaa6afd1d5432c 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.json" @@ -2,5 +2,6 @@ "type": "code_options", "author": "ccat", "source": "to_root.md", - "notebook_enable": false + "notebook_enable": false, + "exercise_id": "45f0cef12fcf4f9e90276dff6a336657" } \ No newline at end of file diff --git "a/data/3.PostgreSQL\351\253\230\351\230\266/1.\351\233\206\347\276\244\345\222\214\345\244\215\345\210\266/3.fdw/fdw.json" "b/data/3.PostgreSQL\351\253\230\351\230\266/1.\351\233\206\347\276\244\345\222\214\345\244\215\345\210\266/3.fdw/fdw.json" index d67ed1484b6fa73f04601681bb03b23cf03218e1..116908ae233493f6f082ceb8bc0db62e9de0c65b 100644 --- "a/data/3.PostgreSQL\351\253\230\351\230\266/1.\351\233\206\347\276\244\345\222\214\345\244\215\345\210\266/3.fdw/fdw.json" +++ "b/data/3.PostgreSQL\351\253\230\351\230\266/1.\351\233\206\347\276\244\345\222\214\345\244\215\345\210\266/3.fdw/fdw.json" @@ -2,5 +2,6 @@ "type": "code_options", "author": "ccat", "source": "fdw.md", - "notebook_enable": false + "notebook_enable": false, + "exercise_id": "762a9c3f032b4ce884858c18e2bb580c" } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 4f86452afcf7a9c7af43a8b5d7e46d2b19603add..f06008111298767b133183897c4bf9babff33b31 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ pre_commit~=2.16.0 +GitPython~=3.1.24 \ No newline at end of file diff --git a/src/tree.py b/src/tree.py index 91d03cb86db125acd4578689f0f08ffd33fbccfc..c856fe76e07dc8ce666013c9eb730dd7d8523421 100644 --- a/src/tree.py +++ b/src/tree.py @@ -4,6 +4,8 @@ import os import re import sys import uuid +import re +import git id_set = set() logger = logging.getLogger(__name__) @@ -12,7 +14,10 @@ handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) +repo = git.Repo(".") +def user_name(): + return repo.config_reader().get_value("user", "name") def load_json(p): with open(p, 'r') as f: @@ -301,6 +306,14 @@ class TreeWalker: def ensure_exercises(self, section_path): config = self.ensure_section_config(section_path) + for e in os.listdir(section_path): + base, ext = os.path.splitext(e) + _, source = os.path.split(e) + if ext != ".md": + continue + meta_path = os.path.join(section_path, base + ".json") + self.ensure_exercises_meta(meta_path, source) + for e in config.get("export", []): full_name = os.path.join(section_path, e) exercise = load_json(full_name) @@ -311,6 +324,36 @@ class TreeWalker: else: id_set.add(exercise["exercise_id"]) + def ensure_exercises_meta(self, meta_path, source): + _, mfile = os.path.split(meta_path) + 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 + } + dump_json(meta_path, meta, True, True) + + def default_notebook(self): + if self.name in ["python", "java", "c", "algorithm"]: + return True + else: + return False + def check_section_keywords(self, full_path): config = self.ensure_section_config(full_path) if not config.get("keywords", []):