google.cloud.forseti.common.util.file_loader module

Utility functions for reading and parsing files in a variety of formats.

_get_filetype_parser(file_path, parser_type)[source]

Return a parser function for parsing the file.

Parameters:
  • file_path (str) – The file path.
  • parser_type (str) – The file parser type.
Returns:

The parser function.

Return type:

function

_parse_json_file(data)[source]

Parse the data from a json file.

Parameters:data (filepointer) – File-like object containing a Json document, to be parsed into json.
Returns:The file successfully parsed into a dict.
Return type:dict
Raises:ValueError – If there was an error parsing the file.
_parse_json_string(data)[source]

Parse the data from a string of json.

Parameters:data (str) – String data to parse into json.
Returns:The json string successfully parsed into a dict.
Return type:dict
Raises:ValueError – If there was an error parsing the data.
_parse_yaml(data)[source]

Parse yaml data.

Parameters:data (stream) – A yaml data stream to parse.
Returns:The stream successfully parsed into a dict.
Return type:dict
Raises:YAMLError – If there was an error parsing the stream.
_read_file_from_gcs(file_path, storage_client=None)[source]

Load file from GCS.

Parameters:
  • file_path (str) – The GCS path to the file.
  • storage_client (storage.StorageClient) – The Storage API Client to use for downloading the file using the API.
Returns:

The parsed dict from the loaded file.

Return type:

dict

_read_file_from_local(file_path)[source]

Load rules file from local path.

Parameters:file_path (str) – The path to the file.
Returns:The parsed dict from the loaded file.
Return type:dict
copy_file_from_gcs(file_path, output_path=None, storage_client=None)[source]

Copy file from GCS to local file.

Parameters:
  • file_path (str) – The full GCS path to the file.
  • output_path (str) – The local file to copy to, if not set creates a temporary file.
  • storage_client (storage.StorageClient) – The Storage API Client to use for downloading the file using the API.
Returns:

The output_path the file was copied to.

Return type:

str

read_and_parse_file(file_path)[source]

Parse a json or yaml formatted file from a local path or GCS.

Parameters:file_path (str) – The full path to the file to read and parse.
Returns:The results of parsing the file.
Return type:dict