dapla_metadata.standards package¶
Subpackages¶
dapla_metadata.standards.dataset_path module¶
Build complete or partial paths from validated dataset metadata.
- class DataState(*values)[source]¶
Bases:
StrEnumData states supported by the dataset path generator.
SOURCE_DATAis excluded: kildedata filenames are not covered by the naming standard that paths are generated from.- INPUT_DATA = 'inndata'¶
- OUTPUT_DATA = 'utdata'¶
- PROCESSED_DATA = 'klargjorte-data'¶
- STATISTICS = 'statistikk'¶
- class FileType(*values)[source]¶
Bases:
StrEnumFile types supported by the dataset path generator.
- CSV = 'csv'¶
- JSON = 'json'¶
- PARQUET = 'parquet'¶
- XML = 'xml'¶
- create_dataset_path(*, bucket=None, product=None, data_state=None, short_description=None, period_from=None, period_to=None, version=None, file_type=None, folders=None)[source]¶
Build a complete or contiguous partial path from dataset metadata.
The complete path has the form:
gs://{bucket}/{product}/{data_state}/{folders...}/{short_description}_p{period_from}[_p{period_to}]_v{version}.{file_type}
Any argument can be combined as long as they follow the correct order.
A filename is atomic:
short_description,period_from,version, andfile_typemust either all be provided or all be omitted.period_tois optional, but can only be used as part of a complete filename.Callers should provide semantic values only. The function should add the
gs://,_p,_v, and.syntax itself. It should not access GCS, inspect the filesystem, or silently correct invalid input.- Parameters:
bucket (
str|None) – Optional bucket name, without thegs://prefix. It must be a non-empty single path segment: no slashes and no leading or trailing whitespace. Beyond that the name is included verbatim and is not validated against the full GCS naming rules, so callers are responsible for validating it when necessary. For example,"ssb-dapla-example-data-produkt-prod".product (
str|None) – Optional non-empty statistics-product or data-product short name. It may contain uppercase and lowercase letters, digits,-, and_. For example,"ledstill"or"ameld_data".data_state (
DataState|None) – An optionalDataStateenum member:DataState.INPUT_DATA,DataState.PROCESSED_DATA,DataState.STATISTICS, orDataState.OUTPUT_DATA. Do not pass a raw string.short_description (
str|None) – Optional non-empty dataset short description. It must contain only letters, digits, and hyphens, with no restriction on hyphen placement. Underscores, spaces, slashes, and periods are not accepted. For example,"varehandel","grensehandel-imputert", or"-imputert--data-".period_from (
str|None) –The optional first period as a string. Do not include the
pprefix. Supported formats are:year:
YYYY— for example,"2019";month:
YYYY-MM— for example,"2022-10";calendar date:
YYYY-MM-DD— for example,"2022-01-24";ISO week:
YYYY-Www— for example,"2020-W15";ISO ordinal date:
YYYY-DDD— for example,"2022-015";date and time:
YYYY-MM-DDTHH-MM-SS.sss— for example,"2024-12-31T23-59-30.000";SSB bimonthly period:
YYYY-Bn, wherenis 1-6;SSB quarterly period:
YYYY-Qn, wherenis 1-4;SSB four-month period:
YYYY-Tn, wherenis 1-3;SSB half-year period:
YYYY-Hn, wherenis 1-2.
period_to (
str|None) – An optional second period as a string, without thepprefix. If supplied, it must use the same format asperiod_fromand be in chronological order.version (
int|None) – An optional non-negative integer, such as0or3. Do not include thevprefix.file_type (
FileType|None) – An optionalFileTypeenum member:FileType.JSON,FileType.CSV,FileType.XML, orFileType.PARQUET. Do not pass a raw string or include the leading period.folders (
list[str] |None) –Noneor a list of optional folder names below the product and data-state folders. Each folder must be non-empty and contain only letters, digits,-, and_. For example,["on-prem", "revidert_data"].
- Return type:
str
Examples
Build a complete path:
>>> create_dataset_path( ... bucket="bucket", ... product="ledstill", ... data_state=DataState.OUTPUT_DATA, ... short_description="varehandel", ... period_from="2018-Q1", ... version=1, ... file_type=FileType.PARQUET, ... ) 'gs://bucket/ledstill/utdata/varehandel_p2018-Q1_v1.parquet'
Build a partial path:
>>> create_dataset_path(product="ledstill", data_state=DataState.INPUT_DATA) 'ledstill/inndata'
Build a filename:
>>> create_dataset_path(short_description="befolkning", period_from="2025", version=0, file_type=FileType.JSON) 'befolkning_p2025_v0.json'
Build a path containing folders:
>>> create_dataset_path(data_state=DataState.OUTPUT_DATA, folders=["publisert", "arkiv"]) 'utdata/publisert/arkiv'
Build a filename containing a period range:
>>> create_dataset_path(short_description="handel", period_from="2025-Q1", period_to="2025-Q4", version=2, file_type=FileType.CSV) 'handel_p2025-Q1_p2025-Q4_v2.csv'
- Return type:
str- Returns:
A complete GCS path or contiguous relative path fragment.
- Raises:
TypeError – If an argument has an invalid Python type.
ValueError – If an argument does not satisfy the naming standard, the filename is incomplete, the path contains a hierarchy gap, or no path component is supplied.
- Parameters:
dapla_metadata.standards.name_validator module¶
- class NamingStandardReport(validation_results)[source]¶
Bases:
objectReport object for name standard validation.
- Parameters:
validation_results (list[ValidationResult])
dapla_metadata.standards.standard_validators module¶
- async check_naming_standard(file_path)[source]¶
Check whether a given path follows the SSB naming standard.
This function checks whether the provided file_path and subdirectories thereof comply with the naming standard. Currently we only examine ‘.parquet’ files. Other files are ignored.
- Parameters:
file_path (
str|PathLike[str]) –The path to a bucket, directory, or specific file to validate. This can be in the following forms: - A bucket URL in the form ‘gs://ssb-dapla-felles-data-produkt-test’ - An absolute path to a mounted bucket in the form ‘/buckets/produkt’ - Any subdirectory or file thereof
We also accept paths which don’t yet exist so that you can test if a path will comply.
- Returns:
A list of validation results, including success status, checked file path, messages, and any detected violations.
- Return type:
list[ValidationResult]
Examples
>>> (await check_naming_standard("/data/example_file.parquet"))[0].success False
>>> (await check_naming_standard("/buckets/produkt/datadoc/utdata/person-data_p2021_v2.parquet"))[0].success True
- async flatten_generator(gen)[source]¶
Recursively flatten nested async generators.
- Return type:
AsyncGenerator[Task,None]- Parameters:
gen (AsyncGenerator)
- generate_validation_report(validation_results)[source]¶
Generate and print a formatted naming standard validation report.
This function takes a list of ValidationResult objects, creates a NamingStandardReport instance, and prints the generated report.
- Parameters:
validation_results (
list[ValidationResult]) – A list of ValidationResult objects thatchecks. (contain the outcomes of the name standard)
- Returns:
An instance of NamingStandardReport containing the validation results.
- Return type: