RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
DatatypeAssert.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
5 # Copyright 2016 RAPP
6 
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 
11  #http://www.apache.org/licenses/LICENSE-2.0
12 
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 
19 # Authors: Konstantinos Panayiotou
20 # contact: klpanagi@gmail.com
21 
22 
23 ## @file Utils/DataTypeAssert.py
24 #
25 # @copyright Rapp Projecty EU 2016
26 # @author Konstantinos Panayiotou, [klpanagi@gmail.com]
27 #
28 
29 
30 # Import functools.wraps to wrap functions
31 from functools import wraps
32 import inspect
33 
34 
35 def add_to_args(*args):
36  """! Add values to *args
37 
38  Returns the updated *args
39  """
40  return args
41 
42 
43 def add_to_kwargs(**kwargs):
44  """! Add values to **kwargs
45 
46  Returns the updated **kwargs
47  """
48  return kwargs
49 
50 
51 def datatypeassert(*decargs, **deckwargs):
52  """! Data type assertion decorator implementation.
53 
54  Check and assert on function input arguments.
55 
56  @param *args - Data types for the function to assert.
57  @param **kwargs - Data types for the function to assert.
58  """
59 
60  def decorador(fn):
61 
62  @wraps(fn)
63  def wrapper(*args, **kwargs):
64  # Bind *args and **kwargs to the input function
65  _boundFn = inspect.getcallargs(fn, *args, **kwargs)
66  if 'self' in _boundFn:
67  # decorKwargs = add_to_kwargs(self=_boundFn['self'], **deckwargs)
68  # Add self to the decorator arguments so it can be bound to the
69  # calling function. Will be remove after binding (hacky!)
70  decorArgs = add_to_args(_boundFn['self'], *decargs)
71  else:
72  # decorKwargs = deckwargs
73  decorArgs = decargs
74 
75  # Map decorators *args and **kwargs to the function declaration *args
76  # and **kwargs.
77  _boundDec = inspect.getcallargs(fn, *decorArgs, **deckwargs)
78  if 'self' in _boundDec:
79  # Remove 'self' from bound arguments
80  del _boundDec['self']
81 
82  # Iterate through _boundDec: Decorators arguments
83  for key, value in _boundDec.iteritems():
84  if key not in _boundFn:
85  # If keyword decorador argument does not exist in
86  # function kwargs, skip.
87  continue
88  if not isinstance(_boundFn[key], value):
89  # Raise TypeError Exception if passed argument type
90  # does not correspond to the value forced by the
91  # decorator
92  raise TypeError(
93  'Argument {0} must be of data type {1}'.format(key, value)
94  )
95 
96  return fn(*args, **kwargs)
97  return wrapper
98  return decorador
99 
def datatypeassert
Data type assertion decorator implementation.
def add_to_kwargs
Add values to **kwargs.
def add_to_args
Add values to *args.