webdriver_test_tools.common.files module

Common functions for working with files and Jinja templates

exception webdriver_test_tools.common.files.FileUtilError[source]

Bases: Exception

Exception raised if utility functions fail


‘Touch’ a file. Creates an empty file if it doesn’t exist, leaves existing files unchanged


filepath – Path of the file to touch

webdriver_test_tools.common.files.create_directory(target_path, directory_name)[source]

Creates a directory in the target path if it doesn’t already exist

  • target_path – The path to the directory that will contain the new one

  • directory_name – The name of the directory to create in the target path


The path to the newly created (or already existing) directory

webdriver_test_tools.common.files.render_template(template_path, context)[source]

Returns the rendered contents of a jinja template

  • template_path – The path to the jinja template

  • context – Jinja context used to render template


Results of rendering jinja template

webdriver_test_tools.common.files.render_template_to_file(template_path, context, target_path)[source]

Writes rendered jinja template to a file

  • template_path – The path to the jinja template

  • context – Jinja context used to render template

  • target_path – File path to write the rendered template to

webdriver_test_tools.common.files.create_file_from_template(template_path, target_path, filename, context, target_filename=None, overwrite=True)[source]

Short hand function that renders a template with the specified filename followed by a ‘.j2’ extension from the template path to a file with the specified name in the target path

The use of ‘.j2’ as a file extension is to distinguish templates from package modules.

  • template_path – Path to template directory

  • target_path – Path to target directory

  • filename – Name of the template file. Will be used as the filename for the rendered file written to the target directory

  • context – Jinja context used to render template

  • target_filename – (Optional) If specified, use a different filename for the created file. If not specified, will use the value of filename

  • overwrite – (Default: True) If False, a FileUtilError will be raised when a file with the same name and path already exists


The file path to the newly created file

webdriver_test_tools.common.files.validate_filename(filename, allow_spaces=False)[source]

Strips invalid characters from a filename

Considers POSIX “fully portable filenames” valid. These include:

A-Z a-z 0-9 ._-

Filenames cannot begin with a hyphen.

  • filename – The desired file name (without path)

  • allow_spaces – (Default = False) If True, spaces will be considered valid characters


Filename with invalid characters removed