{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始编译单元与课时目标信息pdf文件: 临时文件/课时目标及单元目标表_20230221.tex\n",
      "开始编译课时划分信息pdf文件: 临时文件/按课时分类目标及题目清单_20230221.tex\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os,re,time,json,sys\n",
    "\n",
    "\"\"\"\n",
    "\"/模板文件\"目录下 课时划分.txt 与 课时目标及单元目标.txt 文件不能缺失\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"---设置文件名---\"\"\"\n",
    "#目录和文件的分隔务必用/\n",
    "lessoncut_file = \"临时文件/按课时分类目标及题目清单\" \n",
    "lessonobj_file = \"临时文件/课时目标及单元目标表\" \n",
    "\"\"\"---设置文件名结束---\"\"\"\n",
    "\n",
    "#读取系统日期\n",
    "current_time = time.localtime()\n",
    "time_string = \"_\"+str(current_time.tm_year).zfill(4)+str(current_time.tm_mon).zfill(2)+str(current_time.tm_mday).zfill(2)\n",
    "\n",
    "\n",
    "lessoncut_file +=  time_string +\".tex\"\n",
    "lessonobj_file +=  time_string +\".tex\"\n",
    "\n",
    "\n",
    "#读取题库json文件并转化为字典\n",
    "with open(r\"../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    database = f.read()\n",
    "pro_dict = json.loads(database)\n",
    "\n",
    "#读取目标数据库json并转化为字典\n",
    "with open(r\"../题库0.3/LessonObj.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    database = f.read()\n",
    "lessonobj_dict = json.loads(database)\n",
    "\n",
    "#读取课时数据库json并转化为字典\n",
    "with open(r\"../题库0.3/LessonsCut.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    database = f.read()\n",
    "lessoncut_dict = json.loads(database)\n",
    "\n",
    "#读取单元目标数据库json并转化为字典\n",
    "with open(r\"../题库0.3/UnitObj.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    database = f.read()\n",
    "unitobj_dict = json.loads(database)\n",
    "\n",
    "#以下是课时题目统计\n",
    "#生成单元课时列表(Kddll)\n",
    "lessons_descr = [] \n",
    "for o in lessonobj_dict:\n",
    "    if not o[:5] in lessons_descr:\n",
    "        lessons_descr.append(o[:5])\n",
    "lessons_dict = {}\n",
    "lessons_string = \"\"\n",
    "for l in lessons_descr:\n",
    "    lessons_dict[l] = [0,\"\"]\n",
    "for p in pro_dict:\n",
    "    for l in lessons_descr:\n",
    "        for o in pro_dict[p][\"objs\"]:\n",
    "            if l in o:\n",
    "                lessons_dict[l][0] += 1\n",
    "                lessons_dict[l][1] += p + \",\"\n",
    "                break\n",
    "#lessons_string是简要列表, lessons_dict是详细清单\n",
    "for l in lessons_descr:\n",
    "    lessons_string += l + \": \" + str(lessons_dict[l][0]) + \"\\n\" \n",
    "#课时统计完成\n",
    "\n",
    "#以下是目标题目统计\n",
    "objs_dict = {}\n",
    "objs_string = \"\"\n",
    "for o in lessonobj_dict:\n",
    "    objs_dict[o] = [0,\"\"]\n",
    "for p in pro_dict:\n",
    "    for o in lessonobj_dict:\n",
    "        if o in pro_dict[p][\"objs\"]:\n",
    "            objs_dict[o][0] += 1\n",
    "            objs_dict[o][1] += p + \",\"\n",
    "# objs_string是简要列表, objs_dict是详细清单\n",
    "for o in lessonobj_dict:\n",
    "    objs_string += o + \": \" + str(objs_dict[o][0]) + \"\\n\" \n",
    "#课时统计完成\n",
    "\n",
    "#生成课时目标汇总的latex文件内容lessons_obj_string, 准备放到latex文件中\n",
    "lessons_obj_string = \"\"\n",
    "for obj in lessonobj_dict:\n",
    "    lessons_obj_string += lessonobj_dict[obj][\"id\"] + \" & \" + lessonobj_dict[obj][\"unit_obj\"] + \" & \" + lessonobj_dict[obj][\"content\"] + r\"\\\\ \\hline\" + \"\\n\"   \n",
    "\n",
    "#生成单元目标汇总的latex文件内容units_obj_string, 准备放到latex文件中\n",
    "units_obj_string = \"\"\n",
    "for obj in unitobj_dict:\n",
    "    units_obj_string += unitobj_dict[obj][\"id\"] + \"&\" + unitobj_dict[obj][\"content\"] + r\"\\\\ \\hline\" + \"\\n\"\n",
    "\n",
    "#生成课时统计的latex文件内容lessons_cut_string, 准备放到latex文件中\n",
    "lessons_cut_string = \"\"\n",
    "for lesson in lessons_dict:\n",
    "    unit_index = lesson[1:3]\n",
    "    lesson_index = lesson[3:5]\n",
    "    lessons_cut_string += r\"\\section*{第\" + unit_index + \"单元, 第\" + lesson_index +r\"课时}\"+ \"\\n\\n\"\n",
    "    lessons_cut_string += r\"起始页码: \" + lessoncut_dict[lesson][\"start\"] + \"; 终止页码: \" + lessoncut_dict[lesson][\"end\"] + \".\\n\\n\"\n",
    "    lessons_cut_string += r\"\\begin{itemize}\" + \"\\n\\n\"\n",
    "    for o in lessonobj_dict:\n",
    "        if lesson in o:\n",
    "            lessons_cut_string +=  r\"\\item \" + o + \"|\" + lessonobj_dict[o][\"unit_obj\"] + \"|\" + lessonobj_dict[o][\"content\"] + \"\\n\\n\"\n",
    "            lessons_cut_string += \"关联题目数: \" + str(objs_dict[o][0]) + \". 列表: \" + objs_dict[o][1].replace(\",\",\", \") + \"\\n\\n\"\n",
    "    lessons_cut_string += r\"\\item 课时汇总\" + \"\\n\\n\" + \"本课时总题目数: \" + str(lessons_dict[lesson][0]) + \".\\n\\n\" + \"列表: \" +  lessons_dict[lesson][1].replace(\",\",\", \") + \"\\n\\n\"\n",
    "    lessons_cut_string += r\"\\end{itemize}\" + \"\\n\\n\"\n",
    "\n",
    "\n",
    "#替换单元课时目标的latex文件的内容并编译\n",
    "with open(\"模板文件/课时目标及单元目标.txt\",\"r\",encoding = \"utf8\") as f:\n",
    "    obj_latex_file_raw = f.read()\n",
    "#识别操作系统\n",
    "if sys.platform != \"win32\":\n",
    "    obj_latex_file_raw = re.sub(r\"fontset[\\s]*=[\\s]*none\",\"fontset = fandol\",obj_latex_file_raw)\n",
    "    obj_latex_file_raw = re.sub(r\"\\\\setCJKmainfont\",r\"% \\\\setCJKmainfont\",obj_latex_file_raw)\n",
    "obj_latex_file_new = obj_latex_file_raw.replace(\"课时目标待替换\",lessons_obj_string).replace(\"单元目标待替换\",units_obj_string)\n",
    "with open(lessonobj_file,\"w\",encoding = \"utf8\") as f:\n",
    "    f.write(obj_latex_file_new)\n",
    "print(\"开始编译单元与课时目标信息pdf文件:\", lessonobj_file)\n",
    "d = re.search(\"/[^/]*$\",lessonobj_file).span()[0]\n",
    "lessonobj_dir = lessonobj_file[:d]\n",
    "os.system(\"xelatex -interaction=batchmode -output-directory=\" + lessonobj_dir + \" \"+ lessonobj_file)\n",
    "os.system(\"xelatex -interaction=batchmode -output-directory=\" + lessonobj_dir + \" \"+ lessonobj_file)\n",
    "\n",
    "#替换课时划分的latex文件的内容并编译\n",
    "with open(\"模板文件/课时划分.txt\",\"r\",encoding = \"utf8\") as f:\n",
    "    lessons_cut_latex_file_raw = f.read()\n",
    "#识别操作系统\n",
    "if sys.platform != \"win32\":\n",
    "    lessons_cut_latex_file_raw = re.sub(r\"fontset[\\s]*=[\\s]*none\",\"fontset = fandol\",lessons_cut_latex_file_raw)\n",
    "    lessons_cut_latex_file_raw = re.sub(r\"\\\\setCJKmainfont\",r\"% \\\\setCJKmainfont\",lessons_cut_latex_file_raw)\n",
    "lessons_cut_latex_file_new = lessons_cut_latex_file_raw.replace(\"待替换\",lessons_cut_string)\n",
    "with open(lessoncut_file,\"w\",encoding = \"utf8\") as f:\n",
    "    f.write(lessons_cut_latex_file_new)\n",
    "print(\"开始编译课时划分信息pdf文件:\", lessoncut_file)\n",
    "d = re.search(\"/[^/]*$\",lessoncut_file).span()[0]\n",
    "lessoncut_dir = lessoncut_file[:d]\n",
    "os.system(\"xelatex -interaction=batchmode -output-directory=\" + lessoncut_dir + \" \"+ lessoncut_file)\n",
    "os.system(\"xelatex -interaction=batchmode -output-directory=\" + lessoncut_dir + \" \"+ lessoncut_file)\n",
    "\n",
    "print(\"已输出至pdf文件\",r\"临时文件/课时目标及单元目标表\",\"及\",r\"临时文件/按课时分类目标及题目清单\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.15 ('pythontest')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "91219a98e0e9be72efb992f647fe78b593124968b75db0b865552d6787c8db93"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
