Skip to content

Commit e05024c

Browse files
committed
Move helper functions in proper utils modules
1 parent 8748c54 commit e05024c

3 files changed

Lines changed: 42 additions & 35 deletions

File tree

can/ctypesutil.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,8 @@ class HANDLE(ctypes.c_void_p):
101101

102102

103103
PHANDLE = ctypes.POINTER(HANDLE)
104+
105+
106+
def arg_to_c_uint(value):
107+
"""Convert a number or string to an C unsigned integer"""
108+
return ctypes.c_uint(int(value))

can/interfaces/vector/canlib.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
# Import Standard Python Modules
88
# ==============================
99
import ctypes
10-
import functools
1110
import logging
1211
import time
1312
import os
14-
import warnings
1513

1614
from typing import Optional, Tuple
1715

16+
1817
try:
1918
# Try builtin Python 3 Windows API
2019
from _winapi import WaitForSingleObject, INFINITE
@@ -33,7 +32,8 @@
3332
# Import Modules
3433
# ==============
3534
from can import BusABC, Message
36-
from can.util import len2dlc, dlc2len
35+
from can.ctypesutil import arg_to_c_uint
36+
from can.util import len2dlc, dlc2len, deprecated_args_alias
3737
from .exceptions import VectorError
3838

3939
# Define Module Logger
@@ -52,37 +52,6 @@
5252
LOG.warning("Could not import vxlapi: %s", exc)
5353

5454

55-
def deprecated_args_alias(**aliases):
56-
def deco(f):
57-
@functools.wraps(f)
58-
def wrapper(*args, **kwargs):
59-
rename_kwargs(f.__name__, kwargs, aliases)
60-
return f(*args, **kwargs)
61-
62-
return wrapper
63-
64-
return deco
65-
66-
67-
def rename_kwargs(func_name, kwargs, aliases):
68-
for alias, new in aliases.items():
69-
if alias in kwargs:
70-
warnings.warn(
71-
"{} is deprecated; use {}".format(alias, new), DeprecationWarning
72-
)
73-
if new in kwargs:
74-
raise TypeError(
75-
"{} received both {} (deprecated) and {}".format(
76-
func_name, alias, new
77-
)
78-
)
79-
kwargs[new] = kwargs.pop(alias)
80-
81-
82-
def arg_to_c_uint(value):
83-
return ctypes.c_uint(int(value))
84-
85-
8655
class VectorBus(BusABC):
8756
"""The CAN Bus implemented for the Vector interface."""
8857

can/util.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
22
Utilities and configuration file parsing.
33
"""
4-
4+
import functools
5+
import warnings
56
from typing import Dict, Optional, Union
67

78
from can import typechecking
@@ -279,6 +280,38 @@ def channel2int(channel: Optional[Union[typechecking.Channel]]) -> Optional[int]
279280
return None
280281

281282

283+
def deprecated_args_alias(**aliases):
284+
"""Allows to rename/deprecate a function kwarg(s) and
285+
have the deprecated kwarg(s) set as alias(es)
286+
"""
287+
288+
def deco(f):
289+
@functools.wraps(f)
290+
def wrapper(*args, **kwargs):
291+
rename_kwargs(f.__name__, kwargs, aliases)
292+
return f(*args, **kwargs)
293+
294+
return wrapper
295+
296+
return deco
297+
298+
299+
def rename_kwargs(func_name, kwargs, aliases):
300+
"""Helper function for `deprecated_args_alias`"""
301+
for alias, new in aliases.items():
302+
if alias in kwargs:
303+
warnings.warn(
304+
"{} is deprecated; use {}".format(alias, new), DeprecationWarning
305+
)
306+
if new in kwargs:
307+
raise TypeError(
308+
"{} received both {} (deprecated) and {}".format(
309+
func_name, alias, new
310+
)
311+
)
312+
kwargs[new] = kwargs.pop(alias)
313+
314+
282315
if __name__ == "__main__":
283316
print("Searching for configuration named:")
284317
print("\n".join(CONFIG_FILES))

0 commit comments

Comments
 (0)