Ejemplo n.º 1
0
 def _extract(archive: ZipFile, filename: str, dirname: str = '') -> File:
     with archive.open(filename) as current_file:
         log.debug(f'Extracting from archive: {filename}')
         code = current_file.read()
     decoded = code.decode('utf-8', errors='replace').replace('\x00', '')
     filename = filename[len(dirname):]
     return File(path=f'/{filename}', code=decoded)
Ejemplo n.º 2
0
    def get_exercise(self, to_extract: bytes) -> Tuple[int, List[File]]:
        exercise_id = 0
        if self.filename:
            exercise_id, _ = self._clean(self.filename_no_ext)
        if not exercise_id:
            raise BadUploadFile("Can't resolve exercise id.", self.filename)

        decoded = base64.b64encode(to_extract)
        return (exercise_id, [File(f'/main.{self.ext.lower()}', decoded)])
Ejemplo n.º 3
0
    def get_exercise(self, to_extract: str) -> Tuple[int, List[File]]:
        exercise_id, content = self._clean(to_extract)
        if self.filename and not exercise_id:
            exercise_id, _ = self._clean(self.filename_no_ext)
            content = to_extract
        if not exercise_id:
            raise BadUploadFile("Can't resolve exercise id", self.filename)

        return (exercise_id, [File(f'/main.{self.ext}', content)])
Ejemplo n.º 4
0
 def _extract(archive: ZipFile, filename: str, dirname: str = '') -> File:
     with archive.open(filename) as current_file:
         log.debug(f'Extracting from archive: {filename}')
         code = current_file.read()
     if filename.rpartition('.')[-1].lower() in ALLOWED_IMAGES_EXTENSIONS:
         decoded = base64.b64encode(code)
     else:
         decoded = code.decode(
             'utf-8', errors='replace',
         ).replace('\x00', '')
     filename = filename[len(dirname):]
     return File(path=f'/{filename.lower()}', code=decoded)
Ejemplo n.º 5
0
def create_solution(
    exercise: Exercise,
    student_user: User,
    code: Optional[str] = None,
) -> Solution:
    if code is None:
        code = ''.join(random.choices(string.printable, k=100))

    return Solution.create_solution(
        exercise=exercise,
        solver=student_user,
        files=[File('exercise.py', code)],
    )
Ejemplo n.º 6
0
 def get_zip_files() -> Iterator[File]:
     with ZipFile(DOWNLOAD_FILE) as zip_file:
         for file_path in zip_file.namelist():
             with zip_file.open(file_path) as file_code:
                 yield File(file_path, file_code.read())
Ejemplo n.º 7
0
 def get_exercise(self, to_extract: Cell) -> Tuple[int, List[File]]:
     code: List[str] = to_extract.get('source', [])
     exercise_id, clean_code = self._clean(code)
     clean_code, ext = self._get_file_type(clean_code)
     return (exercise_id, [File(f'/main.{ext}', clean_code)])
Ejemplo n.º 8
0
 def get_exercise(self, to_extract: Cell) -> Tuple[int, List[File]]:
     code: List[str] = to_extract.get('source', [])
     exercise_id, clean_code = self._clean(code)
     return (exercise_id, [File('/main.py', clean_code)])