def exercise_contents(exercise): contents = raw_exercise_contents("%s.html" % exercise.name) re_data_require = re.compile("<html[^>]*(data-require=\".*?\")[^>]*>", re.DOTALL) match_data_require = re_data_require.search(contents) data_require = match_data_require.groups()[0] if match_data_require else "" re_body_contents = re.compile("<body>(.*)</body>", re.DOTALL) match_body_contents = re_body_contents.search(contents) body_contents = match_body_contents.groups()[0] re_script_contents = re.compile("<script[^>]*>(.*?)</script>", re.DOTALL) list_script_contents = re_script_contents.findall(contents) script_contents = ";".join(list_script_contents) re_style_contents = re.compile("<style[^>]*>(.*?)</style>", re.DOTALL) list_style_contents = re_style_contents.findall(contents) style_contents = "\n".join(list_style_contents) sha1 = hashlib.sha1(contents.decode("utf-8").encode("ascii", "replace")).hexdigest() if not len(body_contents): raise MissingExerciseException("Missing exercise body in content for exid '%s'" % exercise.name) result = map(lambda s: s.decode('utf-8'), (body_contents, script_contents, style_contents, data_require, sha1)) if templatetags.use_compressed_packages(): return result else: return layer_cache.UncachedResult(result)
def raw_exercise_contents(exercise_file): if templatetags.use_compressed_packages(): exercises_dir = "../khan-exercises/exercises-packed" safe_to_cache = True else: exercises_dir = "../khan-exercises/exercises" safe_to_cache = False path = os.path.join(os.path.dirname(__file__), "%s/%s" % (exercises_dir, exercise_file)) f = None contents = "" try: f = open(path) contents = f.read() except: raise MissingExerciseException("Missing exercise file for exid '%s'" % exercise_file) finally: if f: f.close() if not len(contents): raise MissingExerciseException("Missing exercise content for exid '%s'" % exercise_file) if safe_to_cache: return contents else: # we are displaying an unpacked exercise, either locally or in prod # with a querystring override. It's unsafe to cache this. return layer_cache.UncachedResult(contents)
def raw_exercise_contents(exercise_file): if templatetags.use_compressed_packages(): exercises_dir = "../khan-exercises/exercises-packed" safe_to_cache = True else: exercises_dir = "../khan-exercises/exercises" safe_to_cache = False path = os.path.join(os.path.dirname(__file__), "%s/%s" % (exercises_dir, exercise_file)) f = None contents = "" try: f = open(path) contents = f.read() except: raise MissingExerciseException( "Missing exercise file for exid '%s'" % exercise_file) finally: if f: f.close() if not len(contents): raise MissingExerciseException( "Missing exercise content for exid '%s'" % exercise_file) if safe_to_cache: return contents else: # we are displaying an unpacked exercise, either locally or in prod # with a querystring override. It's unsafe to cache this. return layer_cache.UncachedResult(contents)
def exercise_sha1(exercise): sha1 = None try: contents = raw_exercise_contents("%s.html" % exercise.name) sha1 = hashlib.sha1(contents).hexdigest() except MissingExerciseException: pass if templatetags.use_compressed_packages(): return sha1 else: return layer_cache.UncachedResult(sha1)
def exercise_sha1(exercise): sha1 = None try: file_name = exercise.file_name # TODO(eater): remove this after adding the filename to all existing # exercise entities if not file_name or file_name == "": file_name = exercise.name + ".html" contents = raw_exercise_contents(file_name) sha1 = hashlib.sha1(contents).hexdigest() except MissingExerciseException: pass if templatetags.use_compressed_packages(): return sha1 else: return layer_cache.UncachedResult(sha1)