digital_land.utils package

Submodules

digital_land.utils.add_data_utils module

digital_land.utils.add_data_utils.clear_log(collection_dir, endpoint)
digital_land.utils.add_data_utils.download_dataset(dataset, specification, cache_dir)
digital_land.utils.add_data_utils.get_column_field_summary(dataset, endpoint_resource_info, column_field_dir, converted_dir, specification_dir, pipeline_dir)
digital_land.utils.add_data_utils.get_entity_summary(endpoint_resource_info, output_path, pipeline, issue_dir, pipeline_dir)
digital_land.utils.add_data_utils.get_existing_endpoints_summary(endpoint_resource_info, collection, dataset)
digital_land.utils.add_data_utils.get_issue_summary(endpoint_resource_info, issue_dir, new_entities=None)
digital_land.utils.add_data_utils.get_provision_entities_from_duckdb(lookup_path, pipeline, endpoint_resource_info)
digital_land.utils.add_data_utils.get_transformed_entities(dataset_path, transformed_path)

Returns a Dataframe of entities from a dataset. It returns entities that have facts in the transformed file at transformed_path

digital_land.utils.add_data_utils.get_updated_entities_summary(original_entity_df, updated_entity_df)

This will return a summary of the differences between two dataframes of the same entities

digital_land.utils.add_data_utils.get_user_response(message)
digital_land.utils.add_data_utils.is_date_valid(date, date_type)
digital_land.utils.add_data_utils.is_url_valid(url, url_type)
digital_land.utils.add_data_utils.normalise_json(val)

Returns a sorted stringified json

digital_land.utils.add_endpoints_utils module

digital_land.utils.add_endpoints_utils.task_preprocess(ctx)

preparatory steps to tidy up previous runs, and populate the context :param ctx: :return:

digital_land.utils.dataset_resource_utils module

digital_land.utils.dataset_resource_utils.read_dataset_resource_log(dataset_resource_dir, dataset, resource)

Read an existing DatasetResourceLog CSV for a given dataset/resource.

Returns a dict with code-version, config-hash, and specification-hash, or None if the file doesn't exist or can't be read.

Expected path: {dataset_resource_dir}/{dataset}/{resource}.csv

digital_land.utils.dataset_resource_utils.resource_needs_processing(dataset_resource_dir, dataset, resource, current_code_version, current_config_hash, current_specification_hash)

Check whether a resource needs processing by comparing its log against current state.

Returns True if there is no existing log or if any of the three values differ.

digital_land.utils.gdal_utils module

digital_land.utils.gdal_utils.get_gdal_version()

digital_land.utils.hash_utils module

digital_land.utils.hash_utils.hash_directory(dir, exclude=[])

Returns a SHA1 hex digest of the contents of a directory.

Files are sorted before hashing so the result is stable regardless of filesystem ordering. File names are included in the hash so renames are detected.

Parameters:
  • dir -- Path to the directory to hash.

  • exclude -- List of path prefixes (relative to dir) to exclude.

Raises:

RuntimeError -- If dir does not exist or is not a directory.

digital_land.utils.safe_fetch module

Stop URL fetches from reaching internal addresses.

digital_land.utils.url_safety.check_fetch_url rejects bad URLs up front, but a public hostname can still resolve to an internal address, a public URL can redirect to an internal one, and DNS answers can change between a check and the connection.

untrusted_fetches() closes those gaps for everything that goes through urllib3 (requests, and so the Collector and the arcgis plugin). While it is active on the current thread, every new connection resolves the host, refuses non-public addresses, and connects to the address it checked. Connections made outside the context manager are unaffected.

The paged WFS plugin fetches with ogr2ogr (GDAL) in a subprocess, which this cannot see, so check_host_resolves_publicly is also called before fetching.

exception digital_land.utils.safe_fetch.BlockedAddressError

Bases: OSError

A fetch tried to connect to a non-public address.

digital_land.utils.safe_fetch.check_host_resolves_publicly(url)

Raise UnsafeUrlError unless every address the URL's host resolves to is public.

digital_land.utils.safe_fetch.untrusted_fetches()

Refuse connections to non-public addresses on this thread while active.

digital_land.utils.url_safety module

Checks for URLs that the Collector fetches.

Endpoint URLs come from outside the platform, so a fetch must not be able to read local files (file:) or reach internal addresses such as cloud metadata and credential endpoints.

check_fetch_url is a static check that needs no network access. The addresses actually connected to are checked by digital_land.utils.safe_fetch, which also covers hostnames that resolve to internal addresses and redirects.

exception digital_land.utils.url_safety.UnsafeUrlError

Bases: ValueError

The URL must not be fetched.

digital_land.utils.url_safety.check_fetch_url(url)

Raise UnsafeUrlError unless url is an http(s) URL whose host is not an internal address. Returns the url unchanged.

digital_land.utils.url_safety.is_public_ip(ip)

True if ip is a publicly routable address.

digital_land.utils.url_safety.parse_ip(host)

Return the address if host is an IP literal, else None.

Also accepts the legacy IPv4 forms that resolvers understand, such as "2852039166", "0xa9fea9fe" or "169.254.43518", which all mean 169.254.169.254.

Module contents