Skip to content

Commit 3352541

Browse files
committed
Add kwargs checking, show warnings for unexpected
1 parent 005f423 commit 3352541

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

odm2api/ODM2/services/readService.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ def _get_columns(self, model):
139139

140140
return dict(columns)
141141

142+
def _check_kwargs(self, args, kwargs):
143+
"""Internal helper function to check for unused keyword arguments
144+
145+
Args:
146+
args (list): List of expected, valid arguments.
147+
kwargs (dict): Dictionary of keyword arguments from user
148+
Returns:
149+
None
150+
"""
151+
invkwd = filter(lambda x: x not in args, kwargs.keys())
152+
if invkwd:
153+
warnings.warn('Got unexpected keyword argument(s) {}'.format(','.join(invkwd)), stacklevel=2)
154+
142155
# Exists functions
143156
def resultExists(self, result):
144157
"""
@@ -170,6 +183,7 @@ def getAnnotations(self, annottype=None, codes=None, ids=None, **kwargs):
170183
"""
171184
# TODO What keywords do I use for type.
172185
a = Annotations
186+
self._check_kwargs(['type'], kwargs)
173187
if 'type' in kwargs:
174188
warnings.warn('The parameter \'type\' is deprecated. Please use the annottype parameter instead.',
175189
DeprecationWarning, stacklevel=2)
@@ -398,25 +412,27 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
398412
return None
399413

400414
# Method
401-
def getMethods(self, ids=None, codes=None, medtype=None, **kwargs):
415+
def getMethods(self, ids=None, codes=None, methodtype=None, **kwargs):
402416
"""
403417
* Pass nothing - returns full list of method objects
404418
* Pass a list of MethodIDs - returns a single method object for each given id
405419
* Pass a list of MethodCode - returns a single method object for each given code
406420
* Pass a MethodType - returns a list of method objects of the given MethodType
407421
408422
"""
423+
self._check_kwargs(['type'], kwargs)
409424
if 'type' in kwargs:
410425
warnings.warn('The parameter \'type\' is deprecated. Please use the medtype parameter instead.',
411426
DeprecationWarning, stacklevel=2)
412-
medtype = kwargs['type']
427+
methodtype = kwargs['type']
428+
413429
q = self._session.query(Methods)
414430
if ids:
415431
q = q.filter(Methods.MethodID.in_(ids))
416432
if codes:
417433
q = q.filter(Methods.MethodCode.in_(codes))
418-
if medtype:
419-
q = q.filter_by(MethodTypeCV=medtype)
434+
if methodtype:
435+
q = q.filter_by(MethodTypeCV=methodtype)
420436

421437
try:
422438
return q.all()
@@ -491,6 +507,7 @@ def getSamplingFeatures(self, ids=None, codes=None, uuids=None,
491507
>>> READ.getSamplingFeatures(type='Site', results=True)
492508
493509
"""
510+
self._check_kwargs(['type'], kwargs)
494511
if 'type' in kwargs:
495512
warnings.warn('The parameter \'type\' is deprecated. Please use the sftype parameter instead.',
496513
DeprecationWarning, stacklevel=2)
@@ -566,6 +583,7 @@ def getActions(self, ids=None, acttype=None, sfid=None, **kwargs):
566583
associated with that Sampling feature ID, Found through featureAction table
567584
568585
"""
586+
self._check_kwargs(['type'], kwargs)
569587
if 'type' in kwargs:
570588
warnings.warn('The parameter \'type\' is deprecated. Please use the acttype parameter instead.',
571589
DeprecationWarning, stacklevel=2)
@@ -615,6 +633,7 @@ def getUnits(self, ids=None, name=None, unittype=None, **kwargs):
615633
* Pass a type- returns a list of all objects of the given type
616634
617635
"""
636+
self._check_kwargs(['type'], kwargs)
618637
if 'type' in kwargs:
619638
warnings.warn('The parameter \'type\' is deprecated. Please use the unittype parameter instead.',
620639
DeprecationWarning, stacklevel=2)
@@ -753,7 +772,7 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
753772
754773
"""
755774
query = self._session.query(Results)
756-
775+
self._check_kwargs(['type', 'sfid'], kwargs)
757776
if 'type' in kwargs:
758777
warnings.warn('The parameter \'type\' is deprecated. Please use the restype parameter instead.',
759778
DeprecationWarning, stacklevel=2)
@@ -1052,6 +1071,7 @@ def getEquipment(self, codes=None, equiptype=None, sfid=None, actionid=None, **k
10521071
* Pass an ActionID - returns a single Equipment object
10531072
10541073
"""
1074+
self._check_kwargs(['type'], kwargs)
10551075
if 'type' in kwargs:
10561076
warnings.warn('The parameter \'type\' is deprecated. Please use the equiptype parameter instead.',
10571077
DeprecationWarning, stacklevel=2)
@@ -1151,6 +1171,7 @@ def getExtensionProperties(self, exptype=None, **kwargs):
11511171
11521172
"""
11531173
# Todo what values to use for extensionproperties type
1174+
self._check_kwargs(['type'], kwargs)
11541175
if 'type' in kwargs:
11551176
warnings.warn('The parameter \'type\' is deprecated. Please use the exptype parameter instead.',
11561177
DeprecationWarning, stacklevel=2)
@@ -1181,6 +1202,7 @@ def getExternalIdentifiers(self, eitype=None, **kwargs):
11811202
* Pass type- return a list of all objects of the given type
11821203
11831204
"""
1205+
self._check_kwargs(['type'], kwargs)
11841206
if 'type' in kwargs:
11851207
warnings.warn('The parameter \'type\' is deprecated. Please use the eitype parameter instead.',
11861208
DeprecationWarning, stacklevel=2)
@@ -1432,6 +1454,7 @@ def getRelatedModels(self, modid=None, code=None, **kwargs):
14321454
* Pass a ModelCode - get a list of converter objects related to the converter having ModeCode
14331455
14341456
"""
1457+
self._check_kwargs(['id'], kwargs)
14351458
if 'id' in kwargs:
14361459
warnings.warn('The parameter \'id\' is deprecated. Please use the modid parameter instead.',
14371460
DeprecationWarning, stacklevel=2)

0 commit comments

Comments
 (0)