Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions .coveragerc

This file was deleted.

29 changes: 12 additions & 17 deletions .github/workflows/publish-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,22 @@ jobs:
url: https://test.pypi.org/p/hdx-python-utilities

permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
id-token: write
contents: read

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Get history and tags for versioning to work
run: |
git fetch --prune --unshallow
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
- name: Install Hatch
uses: pypa/hatch@install
- name: Build with hatch
run: |
hatch build

- name: Install uv
uses: astral-sh/setup-uv@v7

- name: Build with uv
run: uv build

- name: Publish distribution 📦 to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
run: uv publish --publish-url https://test.pypi.org/legacy/
27 changes: 12 additions & 15 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,22 @@ jobs:
url: https://pypi.org/p/hdx-python-utilities

permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
id-token: write
contents: read

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Get history and tags for versioning to work
run: |
git fetch --prune --unshallow
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
- name: Install Hatch
uses: pypa/hatch@install
- name: Build with hatch
run: |
hatch build

- name: Install uv
uses: astral-sh/setup-uv@v7

- name: Build with uv
run: uv build

- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
run: uv publish
74 changes: 35 additions & 39 deletions .github/workflows/run-python-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,53 +1,49 @@
# This workflow will install Python dependencies, lint and run tests
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Run tests

on:
workflow_dispatch: # add run button in github
workflow_dispatch:
push:
branches-ignore:
- gh-pages
- 'dependabot/**'
branches-ignore: [gh-pages, "dependabot/**"]
pull_request:
branches-ignore:
- gh-pages
branches-ignore: [gh-pages]

jobs:
build:
runs-on: ubuntu-latest

permissions:
contents: read
checks: write
pull-requests: write

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
- name: Install Hatch
uses: pypa/hatch@install
- name: Test with hatch/pytest
run: |
hatch test
- name: Check styling
if: always()
run: |
hatch fmt --check
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
junit_files: test-results.xml
- name: Publish in Coveralls
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: tests
format: lcov
- uses: actions/checkout@v6

- name: Install uv
uses: astral-sh/setup-uv@v7
with:
enable-cache: true
python-version: "3.13"

- name: Install dependencies
run: uv sync --frozen

- name: Check styling
run: |
uv run ruff format --check
uv run ruff check

- name: Test with pytest
run: uv run --extra html --extra diff --extra email pytest

- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: test-results.xml

- name: Publish in Coveralls
uses: coverallsapp/github-action@v2
if: always()
with:
flag-name: tests
format: lcov
17 changes: 8 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
default_language_version:
python: python3.13
python: python3.13

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
Expand All @@ -9,20 +10,18 @@ repos:
- id: end-of-file-fixer
exclude: (test_csv_processing_blanks.csv|test.txt)
- id: check-ast

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.10
rev: v0.14.13
hooks:
# Run the linter.
- id: ruff-check
args: [ --fix ]
# Run the formatter.
- id: ruff-format

- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.9.22
rev: 0.9.25
hooks:
# Run the pip compile
- id: pip-compile
name: pip-compile requirements.txt
files: pyproject.toml
args: [ pyproject.toml, --resolver=backtracking, --upgrade, -q,
-o, requirements.txt ]
# Ensure the lockfile is up-to-date with pyproject.toml
- id: uv-lock
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note that these are not specific to HDX.
1. Easy logging setup and error logging
1. State utility
1. Path utilities
1. URL utilities
1. Text processing
1. Stable file hashing
1. Matching utilities
Expand Down
41 changes: 32 additions & 9 deletions documentation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Python developers. Note that these are not specific to HDX.
1. [Easy logging setup and error logging](#logging)
1. [State utility](#state-utility)
1. [Path utilities](#path-utilities)
1. [URL utilities](#path-utilities)
1. [Text processing](#text-processing)
1. [Stable file hashing](#stable-file-hashing)
1. [Matching utilities](#matching-utilities)
Expand All @@ -33,6 +34,9 @@ The code for the library is [here](https://github.com/OCHA-DAP/hdx-python-utilit
The library has detailed API documentation which can be found in the menu at the top.

## Breaking Changes
From 4.0.4, get_filename_from_url, get_filename_extension_from_url, get_path_for_url,
get_url_for_get, get_url_params_for_post moved to module url

From 4.0.1, Library is Path aware. The following methods return Path not str:
download_file, get_path_for_url, stream_path, get_temp_dir, script_dir_plus_file,
script_dir. The context managers in hdx.utilities.path where they yield a path
Expand Down Expand Up @@ -829,15 +833,6 @@ Get current directory of script with filename appended

path = script_dir_plus_file("myfile.txt", ANY_PYTHON_OBJECT_IN_SCRIPT)

Get filename or (filename, extension) from url

url = "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-utilities/master/tests/fixtures/test_data.csv"
filename = get_filename_from_url(fixtureurl)
assert filename == "test_data.csv"
filename, extension = get_filename_extension_from_url(fixtureurl)
assert filename == "test_data"
assert extension == ".csv"

Gets temporary directory from environment variable `TEMP_DIR` and falls back to
the temporary folder created by the os function `gettempdir`.

Expand Down Expand Up @@ -890,6 +885,34 @@ The batch code can be passed into `wheretostart_tempdir_batch` in the `batch`
parameter. If not given, the batch code is generated. The folder to use will be
a generated temporary folder unless `tempdir` is given.

## URL utilities

Examples:

Get filename or (filename, extension) or path from url

url = "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-utilities/master/tests/fixtures/test_data.csv"
filename = get_filename_from_url(fixtureurl)
assert filename == "test_data.csv"
filename, extension = get_filename_extension_from_url(fixtureurl)
assert filename == "test_data"
assert extension == ".csv"
# Get unique filename from url and join to provided folder or temporary folder
# if no folder supplied
path = get_path_for_url(url, folder)

Conversion between POST dictionary and GET parameters and the reverse:

# Build get url from url and dictionary of parameters
get_url_for_get("http://www.lala.com/hdfa?a=3&b=4",
OrderedDict([("c", "e"), ("d", "f")]))
# == "http://www.lala.com/hdfa?a=3&b=4&c=e&d=f"

# Extract url and dictionary of parameters from get url
get_url_params_for_post("http://www.lala.com/hdfa?a=3&b=4",
OrderedDict([("c", "e"), ("d", "f")]))


## Text processing

Examples:
Expand Down
34 changes: 0 additions & 34 deletions hatch.toml

This file was deleted.

Loading