cdm_data_loaders.readers.jsonschema_xsv.xsv_validator.helpers ¶
Helpers for the qsv validator.
ErrorRecord
pydantic-model
¶
Bases: BaseModel
A single error encountered while cleaning/validating a file.
Used so that errors always contains a uniform type, regardless of whether the
error came from a failed qsv subprocess, an OSError raised while copying/moving files, or
any other type of fun error that might occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
name of the xsv file being processed when the error occurred |
required |
message
|
str
|
human-readable description of the error |
required |
returncode
|
(int | None, optional)
|
return code of the qsv subprocess, if the error originated from one |
required |
Show JSON schema:
{
"description": "A single error encountered while cleaning/validating a file.\n\nUsed so that ``errors`` always contains a uniform type, regardless of whether the\nerror came from a failed qsv subprocess, an ``OSError`` raised while copying/moving files, or\nany other type of fun error that might occur.\n\n:param file: name of the xsv file being processed when the error occurred\n:type file: str\n:param message: human-readable description of the error\n:type message: str\n:param returncode: return code of the qsv subprocess, if the error originated from one\n:type returncode: int | None, optional",
"properties": {
"file": {
"title": "File",
"type": "string"
},
"message": {
"title": "Message",
"type": "string"
},
"returncode": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Returncode"
}
},
"required": [
"file",
"message"
],
"title": "ErrorRecord",
"type": "object"
}
Fields:
-
file(str) -
message(str) -
returncode(int | None)
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
from_qsv_result
classmethod
¶
Build an ErrorRecord from a failed qsv subprocess result.
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
from_exception
classmethod
¶
Build an ErrorRecord from a raised exception (e.g. OSError).
to_rejected_row ¶
Format this error record for the _rejected table.
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
CleanerValidatorArgs
pydantic-model
¶
Bases: BaseModel
Class containing required parameters for running the XSV file clean/validate functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qsv_cmd
|
str
|
command for executing qsv |
required |
xsv_file_path
|
FilePath
|
path to the data file to be validated |
required |
header_file_path
|
FilePath
|
path to the generated header file |
required |
first_pass_schema
|
ValidatedXsvSchema
|
schema for initial validation of the data |
required |
post_norm_schema
|
ValidatedXsvSchema
|
schema for validating data after cleaning and normalisation |
required |
tmp_dir_path
|
DirectoryPath
|
temporary working directory |
required |
qsv_output_dir_path
|
DirectoryPath
|
directory for qsv output from failed executions |
required |
validated_file_dir_path
|
DirectoryPath
|
directory to put the cleaned, validated files in |
required |
errors
|
list[ErrorRecord]
|
list of errors accumulated during processing |
required |
delimiter
|
(CharStr, optional)
|
delimiter used in the xsv file, defaults to tab |
required |
comment_char
|
(CharStr, optional)
|
comment character used in the xsv file, defaults to |
required |
quote
|
(CharStr, optional)
|
quote character used in the xsv file; if unset, qsv will assume it is |
required |
escape
|
(NonEmptyStr, optional)
|
the escape character used in the xsv file. If not specified, qsv escapes quotes by doubling them. Default None |
required |
null_regex
|
(NonEmptyStr, optional)
|
regex to replace with an empty string |
required |
null_regex_cols
|
list[NonEmptyStr] | None
|
list of columns to apply the null regex to, optional; defaults to None (all cols) |
required |
missing_header
|
(bool, optional)
|
whether or not the xsv file has a missing header, defaults to False |
required |
source_id
|
(NonEmptyStr | None, optional)
|
identifier recorded for the input file in error records, e.g. its path relative to the
input location; defaults to the name of |
required |
Show JSON schema:
{
"$defs": {
"ErrorRecord": {
"description": "A single error encountered while cleaning/validating a file.\n\nUsed so that ``errors`` always contains a uniform type, regardless of whether the\nerror came from a failed qsv subprocess, an ``OSError`` raised while copying/moving files, or\nany other type of fun error that might occur.\n\n:param file: name of the xsv file being processed when the error occurred\n:type file: str\n:param message: human-readable description of the error\n:type message: str\n:param returncode: return code of the qsv subprocess, if the error originated from one\n:type returncode: int | None, optional",
"properties": {
"file": {
"title": "File",
"type": "string"
},
"message": {
"title": "Message",
"type": "string"
},
"returncode": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Returncode"
}
},
"required": [
"file",
"message"
],
"title": "ErrorRecord",
"type": "object"
},
"ValidatedXsvSchema": {
"description": "A JSON Schema to be used for parsing and validating XSV data.",
"properties": {
"jsonschema": {
"additionalProperties": true,
"title": "Jsonschema",
"type": "object"
}
},
"required": [
"jsonschema"
],
"title": "ValidatedXsvSchema",
"type": "object"
}
},
"description": "Class containing required parameters for running the XSV file clean/validate functions.\n\n:param qsv_cmd: command for executing qsv\n:type qsv_cmd: str\n:param xsv_file_path: path to the data file to be validated\n:type xsv_file_path: FilePath\n:param header_file_path: path to the generated header file\n:type header_file_path: FilePath\n\n:param first_pass_schema: schema for initial validation of the data\n:type first_pass_schema: ValidatedXsvSchema\n:param post_norm_schema: schema for validating data after cleaning and normalisation\n:type post_norm_schema: ValidatedXsvSchema\n\n:param tmp_dir_path: temporary working directory\n:type tmp_dir_path: DirectoryPath\n:param qsv_output_dir_path: directory for qsv output from failed executions\n:type qsv_output_dir_path: DirectoryPath\n:param validated_file_dir_path: directory to put the cleaned, validated files in\n:type validated_file_dir_path: DirectoryPath\n:param errors: list of errors accumulated during processing\n:type errors: list[ErrorRecord]\n:param delimiter: delimiter used in the xsv file, defaults to tab\n:type delimiter: CharStr, optional\n:param comment_char: comment character used in the xsv file, defaults to `#`\n:type comment_char: CharStr, optional\n:param quote: quote character used in the xsv file; if unset, qsv will assume it is `\"`\n:type quote: CharStr, optional\n:param escape: the escape character used in the xsv file. If not specified, qsv escapes quotes by doubling them. Default None\n:type escape: NonEmptyStr, optional\n:param null_regex: regex to replace with an empty string\n:type null_regex: NonEmptyStr, optional\n:param null_regex_cols: list of columns to apply the null regex to, optional; defaults to None (all cols)\n:type null_regex_cols: list[NonEmptyStr] | None\n:param missing_header: whether or not the xsv file has a missing header, defaults to False\n:type missing_header: bool, optional\n:param source_id: identifier recorded for the input file in error records, e.g. its path relative to the\n input location; defaults to the name of ``xsv_file_path``\n:type source_id: NonEmptyStr | None, optional",
"properties": {
"qsv_cmd": {
"minLength": 1,
"title": "Qsv Cmd",
"type": "string"
},
"xsv_file_path": {
"format": "file-path",
"title": "Xsv File Path",
"type": "string"
},
"header_file_path": {
"format": "file-path",
"title": "Header File Path",
"type": "string"
},
"first_pass_schema": {
"$ref": "#/$defs/ValidatedXsvSchema"
},
"post_norm_schema": {
"$ref": "#/$defs/ValidatedXsvSchema"
},
"tmp_dir_path": {
"format": "directory-path",
"title": "Tmp Dir Path",
"type": "string"
},
"qsv_output_dir_path": {
"format": "directory-path",
"title": "Qsv Output Dir Path",
"type": "string"
},
"validated_file_dir_path": {
"format": "directory-path",
"title": "Validated File Dir Path",
"type": "string"
},
"errors": {
"items": {
"$ref": "#/$defs/ErrorRecord"
},
"title": "Errors",
"type": "array"
},
"delimiter": {
"default": "\t",
"maxLength": 1,
"minLength": 1,
"title": "Delimiter",
"type": "string"
},
"comment_char": {
"default": "#",
"maxLength": 1,
"minLength": 1,
"title": "Comment Char",
"type": "string"
},
"quote": {
"anyOf": [
{
"maxLength": 1,
"minLength": 1,
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Quote"
},
"escape": {
"anyOf": [
{
"minLength": 1,
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Escape"
},
"null_regex": {
"anyOf": [
{
"minLength": 1,
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Null Regex"
},
"null_regex_cols": {
"anyOf": [
{
"items": {
"minLength": 1,
"type": "string"
},
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"title": "Null Regex Cols"
},
"missing_header": {
"default": false,
"title": "Missing Header",
"type": "boolean"
},
"source_id": {
"anyOf": [
{
"minLength": 1,
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Source Id"
}
},
"required": [
"qsv_cmd",
"xsv_file_path",
"header_file_path",
"first_pass_schema",
"post_norm_schema",
"tmp_dir_path",
"qsv_output_dir_path",
"validated_file_dir_path"
],
"title": "CleanerValidatorArgs",
"type": "object"
}
Fields:
-
qsv_cmd(NonEmptyStr) -
xsv_file_path(FilePath) -
header_file_path(FilePath) -
first_pass_schema(ValidatedXsvSchema) -
post_norm_schema(ValidatedXsvSchema) -
tmp_dir_path(DirectoryPath) -
qsv_output_dir_path(DirectoryPath) -
validated_file_dir_path(DirectoryPath) -
errors(list[ErrorRecord]) -
delimiter(CharStr) -
comment_char(CharStr) -
quote(CharStr | None) -
escape(NonEmptyStr | None) -
null_regex(NonEmptyStr | None) -
null_regex_cols(list[NonEmptyStr] | None) -
missing_header(bool) -
source_id(NonEmptyStr | None)
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
xsv_file_base_name
property
¶
Base name of the input xsv file, without the final suffix.
file_name
property
¶
Identifier of the input xsv file used in error records: source_id if set, else the file name.
valid_file_suffix
property
¶
Suffix added to validator output file containing valid lines.
invalid_file_suffix
property
¶
Suffix added to validator output file containing valid lines.
FileNames
dataclass
¶
Dataclass for storing derived file names.
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
copy_safely ¶
Copy src to dst, recording an ErrorRecord and returning False on failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Path
|
source path |
required |
dst
|
Path
|
destination path |
required |
args
|
CleanerValidatorArgs
|
args for the cleaner validator |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the copy succeeded, False otherwise |
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
move_safely ¶
Move src to dst, recording an ErrorRecord and returning False on failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Path
|
source path |
required |
dst
|
Path
|
destination path |
required |
args
|
CleanerValidatorArgs
|
args for the cleaner validator |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the move succeeded, False otherwise |
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
non_header_lines_present ¶
Return True if path has any non-blank lines beyond the header line.
Stops reading as soon as a second non-blank line is found.
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
prepend_header ¶
Write a new file at dest consisting of the header file's contents followed by the data file's contents.
Source code in src/cdm_data_loaders/readers/jsonschema_xsv/xsv_validator/helpers.py
generate_qsv_validate_file_names ¶
generate_qsv_validate_file_names(
args: CleanerValidatorArgs,
input_file_name: str,
first_pass: bool = True,
) -> FileNames
Compute the file names run_qsv_validate generates from a file with validation errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
CleanerValidatorArgs
|
arguments |
required |
input_file_name
|
str
|
input file name |
required |
first_pass
|
bool
|
whether or not this is the first validation pass, defaults to True |
True
|
Returns:
| Type | Description |
|---|---|
FileNames
|
dataclass containing derived paths (valid_output, errors, valid_lines, invalid_lines, schema) |