Skip to content

Commit

Permalink
Add move resource method to Dataset with test
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarans committed Feb 14, 2025
1 parent 2e185a2 commit a1ee2e5
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .config/pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
- id: end-of-file-fixer
- id: check-ast
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.6
rev: v0.9.6
hooks:
# Run the linter.
- id: ruff
Expand All @@ -17,7 +17,7 @@ repos:
- id: ruff-format
args: [--config, .config/ruff.toml]
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.5.15
rev: 0.5.29
hooks:
# Run the pip compile
- id: pip-compile
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ dependencies = [
"ckanapi>=4.8",
"defopt>=6.4.0",
"email_validator",
"hdx-python-country>=3.8.6",
"hdx-python-utilities>=3.8.2",
"hdx-python-country>=3.8.8",
"hdx-python-utilities>=3.8.3",
"libhxl>=5.2.2",
"makefun",
"quantulum3",
Expand All @@ -54,7 +54,7 @@ content-type = "text/markdown"
Homepage = "https://github.com/OCHA-DAP/hdx-python-api"

[project.optional-dependencies]
test = ["pytest", "pytest-cov", "gspread"]
test = ["pytest", "pytest-check", "pytest-cov", "gspread"]
dev = ["pre-commit"]


Expand Down Expand Up @@ -98,7 +98,7 @@ run = """
"""

[tool.hatch.envs.hatch-static-analysis]
dependencies = ["ruff==0.8.6"]
dependencies = ["ruff==0.9.6"]

[tool.hatch.envs.hatch-static-analysis.scripts]
format-check = ["ruff format --config .config/ruff.toml --check --diff {args:.}",]
Expand Down
36 changes: 20 additions & 16 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# uv pip compile pyproject.toml --resolver=backtracking --all-extras -o requirements.txt
annotated-types==0.7.0
# via pydantic
attrs==24.3.0
attrs==25.1.0
# via
# frictionless
# jsonlines
# jsonschema
# referencing
cachetools==5.5.0
cachetools==5.5.1
# via google-auth
certifi==2024.12.14
certifi==2025.1.31
# via requests
cfgv==3.4.0
# via pre-commit
Expand All @@ -22,7 +22,7 @@ ckanapi==4.8
# via hdx-python-api (pyproject.toml)
click==8.1.8
# via typer
coverage==7.6.10
coverage==7.6.12
# via pytest-cov
defopt==6.4.0
# via hdx-python-api (pyproject.toml)
Expand All @@ -40,27 +40,27 @@ email-validator==2.2.0
# via hdx-python-api (pyproject.toml)
et-xmlfile==2.0.0
# via openpyxl
filelock==3.16.1
filelock==3.17.0
# via virtualenv
frictionless==5.18.0
# via hdx-python-utilities
google-auth==2.37.0
google-auth==2.38.0
# via
# google-auth-oauthlib
# gspread
google-auth-oauthlib==1.2.1
# via gspread
gspread==6.1.4
# via hdx-python-api (pyproject.toml)
hdx-python-country==3.8.6
hdx-python-country==3.8.8
# via hdx-python-api (pyproject.toml)
hdx-python-utilities==3.8.2
hdx-python-utilities==3.8.3
# via
# hdx-python-api (pyproject.toml)
# hdx-python-country
humanize==4.11.0
# via frictionless
identify==2.6.5
identify==2.6.7
# via pre-commit
idna==3.10
# via
Expand Down Expand Up @@ -102,7 +102,7 @@ markupsafe==3.0.2
# via jinja2
mdurl==0.1.2
# via markdown-it-py
more-itertools==10.5.0
more-itertools==10.6.0
# via inflect
nodeenv==1.9.1
# via pre-commit
Expand All @@ -126,15 +126,15 @@ ply==3.11
# libhxl
pockets==0.9.1
# via sphinxcontrib-napoleon
pre-commit==4.0.1
pre-commit==4.1.0
# via hdx-python-api (pyproject.toml)
pyasn1==0.6.1
# via
# pyasn1-modules
# rsa
pyasn1-modules==0.4.1
# via google-auth
pydantic==2.10.5
pydantic==2.10.6
# via frictionless
pydantic-core==2.27.2
# via pydantic
Expand All @@ -145,7 +145,10 @@ pyphonetics==0.5.3
pytest==8.3.4
# via
# hdx-python-api (pyproject.toml)
# pytest-check
# pytest-cov
pytest-check==2.5.0
# via hdx-python-api (pyproject.toml)
pytest-cov==6.0.0
# via hdx-python-api (pyproject.toml)
python-dateutil==2.9.0.post0
Expand All @@ -168,7 +171,7 @@ quantulum3==0.9.2
# via hdx-python-api (pyproject.toml)
ratelimit==2.2.1
# via hdx-python-utilities
referencing==0.35.1
referencing==0.36.2
# via
# jsonschema
# jsonschema-specifications
Expand Down Expand Up @@ -216,7 +219,7 @@ sphinxcontrib-napoleon==0.7
# via defopt
stringcase==1.2.0
# via frictionless
structlog==24.4.0
structlog==25.1.0
# via libhxl
tableschema-to-template==0.0.13
# via hdx-python-utilities
Expand All @@ -235,6 +238,7 @@ typing-extensions==4.12.2
# frictionless
# pydantic
# pydantic-core
# referencing
# typeguard
# typer
unidecode==1.3.8
Expand All @@ -247,7 +251,7 @@ urllib3==2.3.0
# requests
validators==0.34.0
# via frictionless
virtualenv==20.28.1
virtualenv==20.29.2
# via pre-commit
wheel==0.45.1
# via libhxl
Expand All @@ -257,7 +261,7 @@ xlrd3==1.1.0
# via libhxl
xlsx2csv==0.8.4
# via hdx-python-utilities
xlsxwriter==3.2.0
xlsxwriter==3.2.2
# via tableschema-to-template
xlwt==1.3.0
# via hdx-python-utilities
33 changes: 33 additions & 0 deletions src/hdx/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,39 @@ def reorder_resources(
if hxl_update:
self.hxl_update()

def move_resource(
self,
resource_name: str,
insert_before: str,
) -> "Resource":
"""Move resource in dataset to be before the resource whose name starts
with the value of insert_before.
Args:
resource_name (str): Name of resource to move
insert_before (str): Resource to insert before
Returns:
Resource: The resource that was moved
"""
from_index = None
to_index = None
for i, resource in enumerate(self.resources):
res_name = resource["name"]
if res_name == resource_name:
from_index = i
elif res_name.startswith(insert_before):
to_index = i
if to_index is None:
# insert at the start if a manual resource for year cannot be found
to_index = 0
resource = self.resources.pop(from_index)
if from_index < to_index:
# to index was calculated while element was in front
to_index -= 1
self.resources.insert(to_index, resource)
return resource

def update_from_yaml(
self, path: str = join("config", "hdx_dataset_static.yaml")
) -> None:
Expand Down
Loading

0 comments on commit a1ee2e5

Please sign in to comment.