diff --git a/tmcl/bus.py b/tmcl/bus.py index eb34c45..e3e15ef 100644 --- a/tmcl/bus.py +++ b/tmcl/bus.py @@ -1,6 +1,45 @@ import struct from .motor import Motor from .reply import Reply, TrinamicException +import functools +import inspect +import warnings + +# NOTE(kgriffs): We don't want our deprecations to be ignored by default, +# so create our own type. +class DeprecatedWarning(UserWarning): + pass + +def deprecated(instructions): + """Flags a method as deprecated. + Args: + instructions: A human-friendly string of instructions, such + as: 'Please migrate to add_proxy() ASAP.' + By: Kurt Griffiths (kgriffs), https://gist.github.com/kgriffs/8202106 + """ + def decorator(func): + '''This is a decorator which can be used to mark functions + as deprecated. It will result in a warning being emitted + when the function is used.''' + @functools.wraps(func) + def wrapper(*args, **kwargs): + message = 'Call to deprecated function {}. {}'.format( + func.__name__, + instructions) + + frame = inspect.currentframe().f_back + + warnings.warn_explicit(message, + category=DeprecatedWarning, + filename=inspect.getfile(frame.f_code), + lineno=frame.f_lineno) + + return func(*args, **kwargs) + + return wrapper + + return decorator + # MSG_STRUCTURE = ">BBBBIB" @@ -17,8 +56,9 @@ REPLY_LENGTH_IIC = 8 -class Bus (object): +class Bus (object): + def __init__( self, serial, CAN = False ): self.CAN = CAN self.serial = serial @@ -53,6 +93,17 @@ def _handle_reply (self, reply): raise TrinamicException(reply) return reply + @deprecated("Please use `get_module` in the future.") + def get_motor (self, address): + """ + Deprecated! + Although the name suggests otherwise, this function retuns a handle + for a driver module (rather than a motor). + Use `get_module` instead, which also allows to select the default motor + driver on the module. + """ + return Motor(self, address) + def get_module (self, module_address = 1, motor = 0): """ Returns object addressing motor number 'motor' on module 'module_address'. diff --git a/tmcl/info.txt b/tmcl/info.txt deleted file mode 100644 index 2a02ea2..0000000 --- a/tmcl/info.txt +++ /dev/null @@ -1,4 +0,0 @@ -Big fuckup: address! -It is the address of the module, should be set when connecting to it -rather than every time when doing something on the bus... -Also, the motor cannot be addressed directly.