Skip to content

Commit

Permalink
fix: add back the format_description method (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored Mar 23, 2024
2 parents 47cb326 + 4f1cf31 commit 054b045
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
38 changes: 38 additions & 0 deletions geetools/Asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Optional

import ee
from anyascii import anyascii

from geetools.accessors import _register_extention
from geetools.types import pathlike
Expand Down Expand Up @@ -710,3 +711,40 @@ def rglob(self, pattern: str) -> list:
asset.rglob("image_*")
"""
return [a for a in self.iterdir(recursive=True) if a.match(pattern)]

def as_description(self) -> str:
"""Transform the name of the Asset in to a description compatible string for a Task.
Returns:
The formatted description.
"""
return self.format_description(self.name)

@staticmethod
def format_description(description: str) -> str:
"""Format a name to be accepted as a Task description.
The rule is:
The description must contain only the following characters: a..z, A..Z,
0..9, ".", ",", ":", ";", "_" or "-". The description must be at most 100
characters long.
Args:
description: The description to format.
Returns:
The formatted description.
"""
replacements = [
[[" "], "_"],
[["/"], "-"],
[["?", "!", "¿", "*"], "."],
[["(", ")", "[", "]", "{", "}"], ":"],
]

desc = anyascii(description)
for chars, rep in replacements:
pattern = "|".join(re.escape(c) for c in chars)
desc = re.sub(pattern, rep, desc) # type: ignore

return desc[:100]
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies = [
"xee",
"yamlable",
"matplotlib",
"anyascii",
]

[[project.authors]]
Expand Down
4 changes: 4 additions & 0 deletions tests/test_Asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def test_as_uri(self):
asset = ee.Asset("projects/bar")
assert asset.as_uri() == "https://code.earthengine.google.com/?asset=projects/bar"

def test_as_description(self):
asset = ee.Asset(f"projects/{EARTHENGINE_PROJECT}/assets/a weird name")
assert asset.as_description() == "a_weird_name"


class TestOperations:
"""Test the operations that can be run on the asset."""
Expand Down

0 comments on commit 054b045

Please sign in to comment.