Skip to content

Commit

Permalink
[APP-4482] [API] Updated transforms to support new static methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
cblades-tc committed Apr 19, 2024
1 parent b82854c commit d3a492b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
3 changes: 3 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes

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

## 4.0.5

- APP-4442 - [Mitre] Updated MITRE module to support default value and make logging optional
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

0 comments on commit d3a492b

Please sign in to comment.