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
-
webdriver_test_tools.common.files.
touch
(filepath)[source]¶ ‘Touch’ a file. Creates an empty file if it doesn’t exist, leaves existing files unchanged
- Parameters
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
- Parameters
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
- Returns
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
- Parameters
template_path – The path to the jinja template
context – Jinja context used to render template
- Returns
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
- Parameters
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.
- Parameters
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
- Returns
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.
- Parameters
filename – The desired file name (without path)
allow_spaces – (Default = False) If True, spaces will be considered valid characters
- Returns
Filename with invalid characters removed