hdx.data.resource

[view_source]

Resource class containing all logic for creating, checking, and updating resources.

Resource Objects

class Resource(HDXObject)

[view_source]

Resource class containing all logic for creating, checking, and updating resources.

Arguments:

  • initial_data Optional[Dict] - Initial resource metadata dictionary. Defaults to None.
  • configuration Optional[Configuration] - HDX configuration. Defaults to global configuration.

actions

@staticmethod
def actions() -> Dict[str, str]

[view_source]

Dictionary of actions that can be performed on object

Returns:

Dict[str, str]: Dictionary of actions that can be performed on object

update_from_yaml

def update_from_yaml(path: str = join("config", "hdx_resource_static.yml")) -> None

[view_source]

Update resource metadata with static metadata from YAML file

Arguments:

  • path Optional[str] - Path to YAML dataset metadata. Defaults to config/hdx_resource_static.yml.

Returns:

None

update_from_json

def update_from_json(path: str = join("config", "hdx_resource_static.json")) -> None

[view_source]

Update resource metadata with static metadata from JSON file

Arguments:

  • path Optional[str] - Path to JSON dataset metadata. Defaults to config/hdx_resource_static.json.

Returns:

None

read_from_hdx

@classmethod
def read_from_hdx(cls, identifier: str, configuration: Optional[Configuration] = None) -> Optional["Resource"]

[view_source]

Reads the resource given by identifier from HDX and returns Resource object

Arguments:

  • identifier str - Identifier of resource
  • configuration Optional[Configuration] - HDX configuration. Defaults to global configuration.

Returns:

  • Optional[Resource] - Resource object if successful read, None if not

get_date_of_resource

def get_date_of_resource(date_format: Optional[str] = None, today: datetime = now_utc()) -> Dict

[view_source]

Get resource date as datetimes and strings in specified format. If no format is supplied, the ISO 8601 format is used. Returns a dictionary containing keys startdate (start date as datetime), enddate (end date as datetime), startdate_str (start date as string), enddate_str (end date as string) and ongoing (whether the end date is a rolls forward every day).

Arguments:

  • date_format Optional[str] - Date format. None is taken to be ISO 8601. Defaults to None.
  • today datetime - Date to use for today. Defaults to now_utc().

Returns:

  • Dict - Dictionary of date information

set_date_of_resource

def set_date_of_resource(startdate: Union[datetime, str], enddate: Union[datetime, str], ignore_timeinfo: bool = True) -> None

[view_source]

Set resource date from either datetime objects or strings. Any time and time zone information will be ignored by default (meaning that the time of the start date is set to 00:00:00, the time of any end date is set to 23:59:59 and the time zone is set to UTC). To have the time and time zone accounted for, set ignore_timeinfo to False. In this case, the time will be converted to UTC.

Arguments:

  • startdate Union[datetime, str] - Resource start date
  • enddate Union[datetime, str] - Resource end date
  • ignore_timeinfo bool - Ignore time and time zone of date. Defaults to True.

Returns:

None

read_formats_mappings

@classmethod
def read_formats_mappings(cls, configuration: Optional[Configuration] = None, url: Optional[str] = None) -> Dict

[view_source]

Read HDX formats list

Arguments:

  • configuration Optional[Configuration] - HDX configuration. Defaults to global configuration.
  • url Optional[str] - Url of tags cleanup spreadsheet. Defaults to None (internal configuration parameter).

Returns:

  • Dict - Returns formats dictionary

set_formatsdict

@classmethod
def set_formatsdict(cls, formats_dict: Dict) -> None

[view_source]

Set formats dictionary

Arguments:

  • formats_dict Dict - Formats dictionary

Returns:

None

get_mapped_format

@classmethod
def get_mapped_format(cls, file_type: str, configuration: Optional[Configuration] = None) -> Optional[str]

[view_source]

Given a format, return a format to which it maps

Arguments:

  • file_type str - File type to map
  • configuration Optional[Configuration] - HDX configuration. Defaults to global configuration.

Returns:

  • Optional[str] - Mapped format or None if no mapping found

get_file_type

def get_file_type() -> Optional[str]

[view_source]

Get the resource's file type

Returns:

  • Optional[str] - Resource's file type or None if it has not been set

set_file_type

def set_file_type(file_type: str) -> str

[view_source]

Set the resource's file type

Arguments:

  • file_type str - File type to set on resource

Returns:

  • str - Format that was set

clean_file_type

def clean_file_type() -> str

[view_source]

Clean the resource's file type, setting it to None if it is invalid and cannot be mapped

Returns:

  • str - Format that was set

get_file_to_upload

def get_file_to_upload() -> Optional[str]

[view_source]

Get the file uploaded

Returns:

  • Optional[str] - The file that will be or has been uploaded or None if there isn't one

set_file_to_upload

def set_file_to_upload(file_to_upload: str, guess_format_from_suffix: bool = False) -> str

[view_source]

Delete any existing url and set the file uploaded to the local path provided

Arguments:

  • file_to_upload str - Local path to file to upload
  • guess_format_from_suffix bool - Set format from file suffix. Defaults to False.

Returns:

  • Optional[str] - The format that was guessed or None if no format was set

check_url_filetoupload

def check_url_filetoupload() -> None

[view_source]

Check if url or file to upload provided for resource and add resource_type and url_type if not supplied. Correct the file type.

Returns:

None

check_required_fields

def check_required_fields(ignore_fields: ListTuple[str] = tuple()) -> None

[view_source]

Check that metadata for resource is complete. The parameter ignore_fields should be set if required to any fields that should be ignored for the particular operation.

Arguments:

  • ignore_fields ListTuple[str] - Fields to ignore. Default is tuple().

Returns:

None

update_in_hdx

def update_in_hdx(**kwargs: Any) -> None

[view_source]

Check if resource exists in HDX and if so, update it

Arguments:

  • **kwargs - See below
  • operation string - Operation to perform eg. patch. Defaults to update.

Returns:

None

create_in_hdx

def create_in_hdx(**kwargs: Any) -> None

[view_source]

Check if resource exists in HDX and if so, update it, otherwise create it

Returns:

None

delete_from_hdx

def delete_from_hdx() -> None

[view_source]

Deletes a resource from HDX

Returns:

None

get_dataset

def get_dataset() -> "Dataset"

[view_source]

Return dataset containing this resource

Returns:

  • Dataset - Dataset containing this resource

search_in_hdx

@staticmethod
def search_in_hdx(query: str, configuration: Optional[Configuration] = None, **kwargs: Any, ,) -> List["Resource"]

[view_source]

Searches for resources in HDX. NOTE: Does not search dataset metadata!

Arguments:

  • query str - Query
  • configuration Optional[Configuration] - HDX configuration. Defaults to global configuration.
  • **kwargs - See below
  • order_by str - A field on the Resource model that orders the results
  • offset int - Apply an offset to the query
  • limit int - Apply a limit to the query

Returns:

  • List[Resource] - List of resources resulting from query

download

def download(folder: Optional[str] = None) -> Tuple[str, str]

[view_source]

Download resource store to provided folder or temporary folder if no folder supplied

Arguments:

  • folder Optional[str] - Folder to download resource to. Defaults to None.

Returns:

Tuple[str, str]: (URL downloaded, Path to downloaded file)

get_all_resource_ids_in_datastore

@staticmethod
def get_all_resource_ids_in_datastore(configuration: Optional[Configuration] = None) -> List[str]

[view_source]

Get list of resources that have a datastore returning their ids.

Arguments:

  • configuration Optional[Configuration] - HDX configuration. Defaults to global configuration.

Returns:

  • List[str] - List of resource ids that are in the datastore

has_datastore

def has_datastore() -> bool

[view_source]

Check if the resource has a datastore.

Returns:

  • bool - Whether the resource has a datastore or not

delete_datastore

def delete_datastore() -> None

[view_source]

Delete a resource from the HDX datastore

Returns:

None

get_resource_views

def get_resource_views() -> List[ResourceView]

[view_source]

Get any resource views in the resource

Returns:

  • List[ResourceView] - List of resource views

add_update_resource_view

def add_update_resource_view(resource_view: Union[ResourceView, Dict]) -> None

[view_source]

Add new resource view in resource with new metadata

Arguments:

  • resource_view Union[ResourceView,Dict] - Resource view metadata either from a ResourceView object or a dictionary

Returns:

None

add_update_resource_views

def add_update_resource_views(resource_views: ListTuple[Union[ResourceView, Dict]]) -> None

[view_source]

Add new or update existing resource views in resource with new metadata.

Arguments:

  • resource_views ListTuple[Union[ResourceView,Dict]] - A list of resource views metadata from ResourceView objects or dictionaries

Returns:

None

reorder_resource_views

def reorder_resource_views(resource_views: ListTuple[Union[ResourceView, Dict, str]]) -> None

[view_source]

Order resource views in resource.

Arguments:

  • resource_views ListTuple[Union[ResourceView,Dict,str]] - A list of either resource view ids or resource views metadata from ResourceView objects or dictionaries

Returns:

None

delete_resource_view

def delete_resource_view(resource_view: Union[ResourceView, Dict, str]) -> None

[view_source]

Delete a resource view from the resource and HDX

Arguments:

  • resource_view Union[ResourceView,Dict,str] - Either a resource view id or resource view metadata either from a ResourceView object or a dictionary

Returns:

None

enable_dataset_preview

def enable_dataset_preview() -> None

[view_source]

Enable dataset preview of resource

Returns:

None

disable_dataset_preview

def disable_dataset_preview() -> None

[view_source]

Disable dataset preview of resource

Returns:

None

is_broken

def is_broken() -> bool

[view_source]

Return if resource is broken

Returns:

  • bool - Whether resource is broken

mark_broken

def mark_broken() -> None

[view_source]

Mark resource as broken

Returns:

None