Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APP-4482] [API] Updated transforms to support new static methods. #328

Merged
merged 2 commits into from
Apr 29, 2024
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
2 changes: 1 addition & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Release Notes

## 4.0.6

- APP-4482 - [API] Updated transforms to support new static methods.
- APP-4472 - [NAICS] Added NAICS module for lookup by id or name.

## 4.0.5
Expand Down
2 changes: 1 addition & 1 deletion tcex/__metadata__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""TcEx Framework Module"""

__license__ = 'Apache-2.0'
__version__ = '4.0.5'
__version__ = '4.0.6'
8 changes: 0 additions & 8 deletions tcex/api/tc/ti_transform/formatter.py

This file was deleted.

11 changes: 9 additions & 2 deletions tcex/api/tc/ti_transform/model/transform_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ def _always_array(value: list | str) -> list[str]:
return value


class PredefinedFunctionModel(BaseModel, extra=Extra.forbid):
"""Model Definitions"""

name: str = Field(..., description='The name of the static method.')
params: dict | None = Field({}, description='The parameters for the static method.')


# pylint: disable=no-self-argument
class TransformModel(BaseModel, extra=Extra.forbid):
"""Model Definition"""

filter_map: dict | None = Field(None, description='')
kwargs: dict | None = Field({}, description='')
method: Callable | None = Field(None, description='')
for_each: Callable | None = Field(None, description='')
method: Callable | PredefinedFunctionModel | None = Field(None, description='')
for_each: Callable | PredefinedFunctionModel | None = Field(None, description='')
static_map: dict | None = Field(None, description='')

@validator('filter_map', 'static_map', pre=True)
Expand Down
21 changes: 21 additions & 0 deletions tcex/api/tc/ti_transform/ti_predefined_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""TcEx Framework Module"""

# first-party
from tcex.util import Util

to_upper_case = str.upper
to_lower_case = str.lower
to_title_case = str.title

prepend = '{prefix} {}'.format
append = '{} {suffix}'.format


def replace(value: str, find: str, replace_with: str) -> str:
"""Replace value in string."""
return value.replace(find, replace_with)


def format_datetime(value: str):
"""Format datetime."""
return Util.any_to_datetime(value).strftime('%Y-%m-%dT%H:%M:%SZ')
10 changes: 9 additions & 1 deletion tcex/api/tc/ti_transform/transform_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from jmespath import functions

# first-party
from tcex.api.tc.ti_transform import ti_predefined_functions
from tcex.api.tc.ti_transform.model import AttributeTransformModel # TYPE-CHECKING
from tcex.api.tc.ti_transform.model import SecurityLabelTransformModel # TYPE-CHECKING
from tcex.api.tc.ti_transform.model import TagTransformModel # TYPE-CHECKING
Expand All @@ -26,6 +27,7 @@
AssociatedGroupTransform,
DatetimeTransformModel,
FileOccurrenceTransformModel,
PredefinedFunctionModel,
)
from tcex.logger.trace_logger import TraceLogger
from tcex.util import Util
Expand Down Expand Up @@ -491,10 +493,16 @@ def _transform_value(self, metadata: MetadataTransformModel | None) -> str | Non
return value

def _transform_value_callable(
self, value: dict | list | str, c: Callable, kwargs=None
self, value: dict | list | str, c: Callable | PredefinedFunctionModel, kwargs=None
) -> str | None | list[str] | None:
"""Transform values in the TI data."""
# find signature of method and call with correct args
if isinstance(c, PredefinedFunctionModel):
normalized_params = {
k.replace(' ', '_').lower(): v for k, v in PredefinedFunctionModel.params or {}
}
return getattr(ti_predefined_functions, c.name)(value, **normalized_params)

kwargs = kwargs or {}
try:
sig = signature(c, follow_wrapped=True)
Expand Down
Loading