Skip to content

Commit

Permalink
move general command to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
purplenicole730 committed Dec 20, 2024
1 parent fd316a1 commit c615944
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
30 changes: 2 additions & 28 deletions src/viam/app/data_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import functools
import warnings
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple, Union
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union

import bson
from google.protobuf.struct_pb2 import Struct
Expand Down Expand Up @@ -82,36 +81,11 @@
StreamingDataCaptureUploadResponse,
UploadMetadata,
)
from viam.utils import ValueTypes, create_filter, datetime_to_timestamp, struct_to_dict
from viam.utils import ValueTypes, _alias_param, create_filter, datetime_to_timestamp, struct_to_dict

LOGGER = logging.getLogger(__name__)


def _alias_param(param_name: str, param_alias: str) -> Callable:
"""
Decorator for aliasing a param in a function. Intended for providing backwards compatibility on params with name changes.
Args:
param_name: name of param in function to alias
param_alias: alias that can be used for this param
Returns:
The input function, plus param alias.
"""
def decorator(func: Callable):
@functools.wraps(func)
def wrapper(*args, **kwargs):
alias_param_value = kwargs.get(param_alias)
if alias_param_value:
# Only use alias value if param is not given.
if not kwargs.get(param_name):
kwargs[param_name] = alias_param_value
del kwargs[param_alias]
result = func(*args, **kwargs)
return result
return wrapper
return decorator


class DataClient:
"""gRPC client for uploading and retrieving data from app.
Expand Down
27 changes: 26 additions & 1 deletion src/viam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import threading
from datetime import datetime
from typing import Any, Dict, List, Mapping, Optional, SupportsBytes, SupportsFloat, Type, TypeVar, Union
from typing import Any, Callable, Dict, List, Mapping, Optional, SupportsBytes, SupportsFloat, Type, TypeVar, Union

from google.protobuf.json_format import MessageToDict, ParseDict
from google.protobuf.message import Message
Expand Down Expand Up @@ -339,3 +339,28 @@ def create_filter(
bbox_labels=bbox_labels,
dataset_id=dataset_id if dataset_id else "",
)


def _alias_param(param_name: str, param_alias: str) -> Callable:
"""
Decorator for aliasing a param in a function. Intended for providing backwards compatibility on params with name changes.
Args:
param_name: name of param in function to alias
param_alias: alias that can be used for this param
Returns:
The input function, plus param alias.
"""
def decorator(func: Callable):
@functools.wraps(func)
def wrapper(*args, **kwargs):
alias_param_value = kwargs.get(param_alias)
if alias_param_value:
# Only use alias value if param is not given.
if not kwargs.get(param_name):
kwargs[param_name] = alias_param_value
del kwargs[param_alias]
result = func(*args, **kwargs)
return result
return wrapper
return decorator

0 comments on commit c615944

Please sign in to comment.