utils ¶
Access local and packaged resources for GKG extraction.
GKG_COLUMNS module-attribute ¶
GKG column names in serialization order, also exported by gdelt_regkg.utils.
ResourceUnavailableError ¶
Bases: FileNotFoundError
Required local resource is missing.
Prepare the resource in the selected resource directory or supply an explicit lexicon or gazetteer to the extraction function. The exception message identifies the missing path and provides setup guidance.
ResourceStore dataclass ¶
Resolve and read resources using a selected local resource directory.
Prepared lexicons and GCAM data are loaded from the local directory. Packaged count and name rules provide fallbacks, and other resource paths refer to packaged files. Local defaults.json entries override packaged profile names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resources_dir | StrPath | None | Root directory for locally prepared resources. Defaults to | None |
resources_dir class-attribute instance-attribute ¶
Root directory used to look up prepared resources and local defaults.
path ¶
Select a local or packaged resource by its relative path.
Paths under lexicons and gcam use local files when available. Missing lexicon files whose names start with counts. or names. fall back to packaged resources. Other paths refer directly to packaged resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parts | str | Path components relative to the resource root. Omit components to refer to the packaged resource directory. | () |
Returns:
| Type | Description |
|---|---|
ResourcePath | A local file path or packaged resource reference. Packaged references |
ResourcePath | are returned before their existence is checked. |
Raises:
| Type | Description |
|---|---|
ResourceUnavailableError | If a requested local lexicon or GCAM file is missing and has no packaged fallback. |
Source code in src/gdelt_regkg/utils.py
read_json ¶
Read a UTF-8 JSON resource, decompressing files ending in .gz.
Reading defaults.json merges the packaged mapping with entries from the local resource directory. Local entries take precedence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parts | str | Path components identifying a JSON file relative to the resource root. | () |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | The decoded JSON data. For |
dict[str, Any] | field-to-filename mapping. |
Raises:
| Type | Description |
|---|---|
ResourceUnavailableError | If a required local resource is missing. |
OSError | If the selected file cannot be read or its gzip header is invalid. |
UnicodeDecodeError | If resource data or local defaults are not UTF-8. |
JSONDecodeError | If resource data or local defaults are invalid JSON. |
ValueError | If packaged or local |
Source code in src/gdelt_regkg/utils.py
profile ¶
Locate the default extraction profile for a field.
The filename comes from the merged defaults.json mapping. Local files under lexicons take precedence. The counts and ner profiles can fall back to packaged files when those files exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind | str | Profile key in | required |
Returns:
| Type | Description |
|---|---|
ResourcePath | A reference to an existing local or packaged profile file. |
Raises:
| Type | Description |
|---|---|
KeyError | If |
ValueError | If the selected filename is empty, is not a string, or contains a slash, backslash, or colon; or defaults data is not a JSON object. |
ResourceUnavailableError | If the profile is missing locally and no packaged fallback exists. |
OSError | If a defaults file cannot be read. |
UnicodeDecodeError | If a defaults file is not valid UTF-8. |
JSONDecodeError | If a defaults file contains invalid JSON. |
Source code in src/gdelt_regkg/utils.py
resource_directory ¶
Resolve the directory used for locally prepared resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resources_dir | StrPath | None | Resource directory to use. Defaults to | None |
Returns:
| Type | Description |
|---|---|
Path | The absolute resource directory path. Relative paths are resolved against |
Path | the current working directory. |
Source code in src/gdelt_regkg/utils.py
packaged_resource_path ¶
Return a reference to a resource shipped with the package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parts | str | Path components relative to the package's | () |
Returns:
| Type | Description |
|---|---|
ResourcePath | A resource reference supporting file. The referenced path may be a file, |
ResourcePath | a directory, or a path that does not exist. |
Source code in src/gdelt_regkg/utils.py
resource_path ¶
Select a resource using the local directory and packaged fallbacks.
Uses the lookup rules of ResourceStore.path. Prepared lexicons and GCAM files are selected locally, with packaged fallbacks for count and name rules. Other resource paths refer to packaged files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parts | str | Path components relative to the resource root. Omit components to refer to the packaged resource directory. | () |
resources_dir | StrPath | None | Root directory for locally prepared resources. Defaults to | None |
Returns:
| Type | Description |
|---|---|
ResourcePath | A local file path or packaged resource reference. Packaged references |
ResourcePath | are returned before their existence is checked. |
Raises:
| Type | Description |
|---|---|
ResourceUnavailableError | If a requested local lexicon or GCAM file is missing and has no packaged fallback. |
Source code in src/gdelt_regkg/utils.py
read_resource_json ¶
Load a JSON resource using the selected resource directory.
Uses ResourceStore.read_json to select the resource, decode UTF-8 JSON, decompress files ending in .gz, and merge local defaults.json overrides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parts | str | Path components identifying a JSON file relative to the resource root. | () |
resources_dir | StrPath | None | Root directory for locally prepared resources. Defaults to | None |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | The decoded JSON data. For |
dict[str, Any] | field-to-filename mapping updated with local entries. |
Raises:
| Type | Description |
|---|---|
ResourceUnavailableError | If a required local resource is missing. |
OSError | If the selected file cannot be read or its gzip header is invalid. |
UnicodeDecodeError | If resource data or local defaults are not UTF-8. |
JSONDecodeError | If resource data or local defaults are invalid JSON. |
ValueError | If packaged or local |