hdx.data.resource
Resource class containing all logic for creating, checking, and updating resources.
Resource Objects
class Resource(HDXObject)
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]
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.yaml")) -> None
Update resource metadata with static metadata from YAML file
Arguments:
path
Optional[str] - Path to YAML dataset metadata. Defaults to config/hdx_resource_static.yaml.
Returns:
None
update_from_json
def update_from_json(path: str = join("config",
"hdx_resource_static.json")) -> None
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"]
Reads the resource given by identifier from HDX and returns Resource object
Arguments:
identifier
str - Identifier of resourceconfiguration
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
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
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 dateenddate
Union[datetime, str] - Resource end dateignore_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
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
Set formats dictionary
Arguments:
formats_dict
Dict - Formats dictionary
Returns:
None
get_mapped_format
@classmethod
def get_mapped_format(
cls,
format: str,
configuration: Optional[Configuration] = None) -> Optional[str]
Given a file format, return an HDX format to which it maps
Arguments:
format
str - File type to mapconfiguration
Optional[Configuration] - HDX configuration. Defaults to global configuration.
Returns:
Optional[str]
- Mapped format or None if no mapping found
get_format
def get_format() -> Optional[str]
Get the resource's format
Returns:
Optional[str]
- Resource's format or None if it has not been set
set_format
def set_format(format: str) -> str
Set the resource's file type
Arguments:
format
str - Format to set on resource
Returns:
str
- Format that was set
clean_format
def clean_format() -> str
Clean the resource's format, 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]
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
Delete any existing url and set the file uploaded to the local path provided
Arguments:
file_to_upload
str - Local path to file to uploadguess_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
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
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
Check if resource exists in HDX and if so, update it. To indicate that the data in an external resource (given by a URL) has been updated, set data_updated to True, which will result in the resource last_modified field being set to now. If the method set_file_to_upload is used to supply a file, the resource last_modified field is set to now automatically regardless of the value of data_updated.
Arguments:
**kwargs
- See belowoperation
string - Operation to perform eg. patch. Defaults to update.data_updated
bool - If True, set last_modified to now. Defaults to False.date_data_updated
datetime - Date to use for last_modified. Default to None.
Returns:
None
create_in_hdx
def create_in_hdx(**kwargs: Any) -> None
Check if resource exists in HDX and if so, update it, otherwise create it. To indicate that the data in an external resource (given by a URL) has been updated, set data_updated to True, which will result in the resource last_modified field being set to now. If the method set_file_to_upload is used to supply a file, the resource last_modified field is set to now automatically regardless of the value of data_updated.
Arguments:
**kwargs
- See belowdata_updated
bool - If True, set last_modified to now. Defaults to False.date_data_updated
datetime - Date to use for last_modified. Default to None.
Returns:
None
delete_from_hdx
def delete_from_hdx() -> None
Deletes a resource from HDX
Returns:
None
get_dataset
def get_dataset() -> "Dataset"
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"]
Searches for resources in HDX. NOTE: Does not search dataset metadata!
Arguments:
query
str - Queryconfiguration
Optional[Configuration] - HDX configuration. Defaults to global configuration.**kwargs
- See beloworder_by
str - A field on the Resource model that orders the resultsoffset
int - Apply an offset to the querylimit
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]
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]
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
Check if the resource has a datastore.
Returns:
bool
- Whether the resource has a datastore or not
delete_datastore
def delete_datastore() -> None
Delete a resource from the HDX datastore
Returns:
None
get_resource_views
def get_resource_views() -> List[ResourceView]
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
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
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
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
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
Enable dataset preview of resource
Returns:
None
disable_dataset_preview
def disable_dataset_preview() -> None
Disable dataset preview of resource
Returns:
None
is_broken
def is_broken() -> bool
Return if resource is broken
Returns:
bool
- Whether resource is broken
mark_broken
def mark_broken() -> None
Mark resource as broken
Returns:
None
is_marked_data_updated
def is_marked_data_updated() -> bool
Return if the resource's data is marked to be updated
Returns:
bool
- Whether resource's data is marked to be updated
mark_data_updated
def mark_data_updated() -> None
Mark resource data as updated
Returns:
None
get_date_data_updated
def get_date_data_updated() -> datetime
Get date resource data was updated
Returns:
datetime
- Date resource data was updated
set_date_data_updated
def set_date_data_updated(date: Union[datetime, str],
ignore_timeinfo: bool = False) -> None
Set date resource data was updated
Arguments:
date
Union[datetime, str] - Date resource data was updatedignore_timeinfo
bool - Ignore time and time zone of date. Defaults to False.
Returns:
None
get_hdx_url
def get_hdx_url() -> Optional[str]
Get the url of the resource on HDX
Returns:
Optional[str]
- Url of the resource on HDX or None if the resource is missing the id field
get_api_url
def get_api_url() -> Optional[str]
Get the API url of the resource on HDX
Returns:
Optional[str]
- API url of the resource on HDX or None if the resource is missing the id field