示例#1
0
def create_file(do_create,
                rel_path,
                contests,
                type_name,
                ext,
                env,
                translate=None):
    """
    Create a file of contest data using the given function, and return
    a Path object.

    Args:
      do_create: a function with signature create(output_path, contests)
        that creates the file.
      rel_path: a path relative to the output directory configured in the
        Jinja2 Environment object. This can be any path-like object
        and should **not** have the file extension added (the function
        will add it).
      type_name: the name of the file type, for logging purposes.
      ext: the file extension to use, including the leading dot.
      env: a Jinja2 Environment object.
    """
    rel_path = Path(rel_path)
    # Add the suffix.
    rel_path = rel_path.with_suffix(ext)
    output_path = utils.get_output_path(env, rel_path)

    contests = make_contest_pairs(contests, translate=translate)

    do_create(output_path, contests=contests)

    return rel_path
示例#2
0
def secure_hash(env, rel_path):
    """
    Return a secure hash of the contents of the file, using SHA-256.

    Returns the hash as a hexadecimal string.

    Args:
      env: a Jinja2 Environment object.
      rel_path: a path relative to the output directory configured in the
        Jinja2 Environment object. This can be any path-like object.
    """
    path = utils.get_output_path(env, rel_path)
    sha = utils.hash_file(path)

    return sha
示例#3
0
def output_file_uri(env, rel_path):
    """
    Return an (absolute) file URI to an output path.

    This template filter can be used to add hyperlinks to allow browsing
    the rendered files on the local file system.

    Args:
      env: a Jinja2 Environment object.
      rel_path: a path relative to the output directory configured in the
        Jinja2 Environment object. This can be any path-like object.
    """
    output_path = utils.get_output_path(env, rel_path)
    output_path = output_path.resolve()
    uri = output_path.as_uri()

    return uri
示例#4
0
 def test_get_output_path(self):
     output_dir = 'my/path'
     env = testhelpers.make_test_env(output_dir=output_dir)
     actual = utils.get_output_path(env, rel_path='html/index.html')
     self.assertEqual(actual, Path('my/path/html/index.html'))