def import_quiz(file): try: os.mkdir('tempdir') except: shutil.rmtree('tempdir') os.mkdir('tempdir') os.mkdir('tempdir/extracted') with open('tempdir/uploaded.zip', 'wb') as quiz_zip: for chunk in file.chunks(): quiz_zip.write(chunk) arch = PyZipFile('tempdir/uploaded.zip') arch.extractall(path='tempdir/extracted') try: with open('tempdir/extracted/description.txt', 'r') as description: info = description.readlines() quiz = Quizzes.objects.get_or_create( url_name=info[0].strip(), name=info[1].strip(), description=info[2].strip(), )[0] with open('tempdir/extracted/test.txt', 'r') as test: # parse plaintext lines = test.readlines() tags = [] tag = '' current_text = '' for line in lines: if tag and current_text: tags.append((tag, current_text.rstrip('\n\r '))) if line.startswith("<%%"): tag = line.partition('>')[0] + '>' current_text = line.partition('>')[2] else: current_text += line # if last line was without tag: if tag and current_text: tags.append((tag, current_text.rstrip('\n\r '))) # handling parsed data to custom classes questions = [] for tag, value in tags: if tag == "<%%ID>": questions.append(Question(value)) elif tag == "<%%Q>": questions[-1].text = value elif tag == "<%%QP>": questions[-1].pic = value elif tag == "<%%AC>" or tag == "<%%A>": questions[-1].answers.append(Answer()) questions[-1].answers[-1].text = value questions[-1].answers[-1].correct = tag == "<%%AC>" elif tag == "<%%AP>": questions[-1].answers[-1].pic = value # save to db for q in questions: question = Questions.objects.get_or_create( quiz_id=quiz, question_tag=q.id_)[0] question.question_text = q.text if q.pic: save_pic(q.pic, question) question.save() for a in q.answers: answer = Answers.objects.get_or_create( question_id=question, answer_text=a.text)[0] answer.answer_correct = a.correct if a.pic: save_pic(a.pic, answer) answer.save() quiz.calc_weight() quiz.save() return True except Exception as e: print(e, str(e))
#!/usr/bin/env python3 import sys from zipfile import PyZipFile for zip_file in sys.argv[1:]: pzf = PyZipFile(zip_file) pzf.extractall()
def debug_install_egg(egg_path: str, mods_dir, dest_name: str, mod_folder_name: str) -> None: """ Copies the debug egg provided by Pycharm Pro which adds the capability to make debugging happen inside of PyCharm Pro. A bit of work goes into this so it'll be much slower. :param egg_path: Path to the debug egg :param mods_dir: Path to the mods folder :param dest_name: Name of the mod :param mod_folder_name: Name of mod Subfolder :return: """ print("Re-packaging and installing the debugging capability mod...") # Get egg filename and path filename = Path(egg_path).name mods_sub_dir = os.path.join(mods_dir, mod_folder_name) mod_path = os.path.join(mods_sub_dir, dest_name + ".ts4script") ensure_path_created(mods_sub_dir) # Get python ctypes folder sys_ctypes_folder = os.path.join(get_sys_folder(), "Lib", "ctypes") # Create temp directory tmp_dir = tempfile.TemporaryDirectory() tmp_egg = tmp_dir.name + os.sep + filename # Remove old mod in mods folder there, if it exists remove_file(mod_path) # Copy egg to temp path shutil.copyfile(egg_path, tmp_egg) # Extract egg # This step is a bit redundant but I need to copy over everything but one folder into the zip file and I don't # know how to do that in python so I copy over the zip, extract it, copy in the whole folder, delete the one # sub-folder, then re-zip everything up. It's a pain but it's what I know hwo to do now and Google's not much help zip = PyZipFile(tmp_egg) zip.extractall(tmp_dir.name) zip.close() # Remove archive remove_file(tmp_egg) # Copy ctype folder to extracted archive shutil.copytree(sys_ctypes_folder, tmp_dir.name + os.sep + "ctypes") # Remove that one folder remove_dir(tmp_dir.name + os.sep + "ctypes" + os.sep + "__pycache__") # Grab a handle on the egg zf = PyZipFile(mod_path, mode='w', compression=ZIP_STORED, allowZip64=True, optimize=2) # Add all the files in the tmp directory to the zip file for folder, subs, files in os.walk(tmp_dir.name): for file in files: archive_path = get_rel_path(folder + os.sep + file, tmp_dir.name) zf.write(folder + os.sep + file, archive_path) zf.close() # There's a temporary directory bug that causes auto-cleanup to sometimes fail # We're preventing crash messages from flooding the screen to keep things tidy try: tmp_dir.cleanup() except: pass