Skip to content

Commit

Permalink
allow string units
Browse files Browse the repository at this point in the history
  • Loading branch information
gjedlicska committed Aug 30, 2023
1 parent cca7b18 commit 102850b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
22 changes: 9 additions & 13 deletions src/specklepy/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from stringcase import pascalcase

from specklepy.logging.exceptions import SpeckleException
from specklepy.logging.exceptions import SpeckleException, SpeckleInvalidUnitException
from specklepy.objects.units import Units, get_units_from_string
from specklepy.transports.memory import MemoryTransport

Expand Down Expand Up @@ -322,7 +322,7 @@ class Base(_RegisteringBase):
id: Union[str, None] = None
totalChildrenCount: Union[int, None] = None
applicationId: Union[str, None] = None
_units: Union[Units, None] = None
_units: Union[None, str] = None

def __init__(self, **kwargs) -> None:
super().__init__()
Expand Down Expand Up @@ -463,22 +463,18 @@ def add_detachable_attrs(self, names: Set[str]) -> None:

@property
def units(self) -> Union[str, None]:
if self._units:
return self._units.value
return None
return self._units

@units.setter
def units(self, value: Union[str, Units, None]):
if value is None:
units = value
if isinstance(value, str) or value is None:
self._units = value
elif isinstance(value, Units):
units: Units = value
self._units = value.value
else:
units = get_units_from_string(value)
self._units = units
# except SpeckleInvalidUnitException as ex:
# warn(f"Units are reset to None. Reason {ex.message}")
# self._units = None
raise SpeckleInvalidUnitException(
f"Unknown type {type(value)} received for units"
)

def get_member_names(self) -> List[str]:
"""Get all of the property names on this object, dynamic or not"""
Expand Down
11 changes: 8 additions & 3 deletions src/specklepy/objects/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,18 @@ def get_units_from_encoding(unit: int):
)


def get_encoding_from_units(unit: Union[Units, None]):
def get_encoding_from_units(unit: Union[Units, str, None]):
maybe_sanitized_unit = unit
if isinstance(unit, str):
for unit_enum, aliases in UNITS_STRINGS.items():
if unit in aliases:
maybe_sanitized_unit = unit_enum
try:
return UNITS_ENCODINGS[unit]
return UNITS_ENCODINGS[maybe_sanitized_unit]
except KeyError as e:
raise SpeckleException(
message=(
f"No encoding exists for unit {unit}."
f"No encoding exists for unit {maybe_sanitized_unit}."
f"Please enter a valid unit to encode (eg {UNITS_ENCODINGS})."
)
) from e
2 changes: 1 addition & 1 deletion tests/intergration/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_serialize(self, base):
deserialized = operations.deserialize(serialized)

assert base.get_id() == deserialized.get_id()
assert base.units == "mm"
assert base.units == "millimetres"
assert isinstance(base.test_bases[0], Base)
assert base["@revit_thing"].speckle_type == "SpecialRevitFamily"
assert base["@detach"].name == deserialized["@detach"].name
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ def test_speckle_type_cannot_be_set(base: Base) -> None:

def test_setting_units():
b = Base(units="foot")
assert b.units == "ft"
assert b.units == "foot"

with pytest.raises(SpeckleInvalidUnitException):
b.units = "big"
# with pytest.raises(SpeckleInvalidUnitException):
b.units = "big"
assert b.units == "big"

with pytest.raises(SpeckleInvalidUnitException):
b.units = 7 # invalid args are skipped
assert b.units == "ft"
assert b.units == "big"

b.units = None # None should be a valid arg
assert b.units is None
Expand Down

0 comments on commit 102850b

Please sign in to comment.