Coverage for curator/validators/filters.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-16 15:27 -0600

1from voluptuous import Any, In, Required, Schema 

2from curator.defaults import settings, filtertypes 

3from curator.exceptions import ConfigurationError 

4from curator.validators import SchemaCheck 

5import logging 

6logger = logging.getLogger(__name__) 

7 

8def filtertype(): 

9 return { 

10 Required('filtertype'): Any( 

11 In(settings.all_filtertypes()), 

12 msg='filtertype must be one of {0}'.format( 

13 settings.all_filtertypes() 

14 ) 

15 ) 

16 } 

17 

18def structure(): 

19 # This is to first ensure that only the possible keys/filter elements are 

20 # there, and get a dictionary back to work with. 

21 retval = settings.structural_filter_elements() 

22 retval.update(filtertype()) 

23 return Schema(retval) 

24 

25def single(action, data): 

26 try: 

27 ft = data['filtertype'] 

28 except KeyError: 

29 raise ConfigurationError('Missing key "filtertype"') 

30 f = filtertype() 

31 for each in getattr(filtertypes, ft)(action, data): 

32 f.update(each) 

33 return Schema(f) 

34 

35def Filters(action, location=None): 

36 def f(v): 

37 def prune_nones(mydict): 

38 return dict([(k,v) for k, v in mydict.items() if v != None and v != 'None']) 

39 # This validator method simply validates all filters in the list. 

40 for idx in range(0, len(v)): 

41 pruned = prune_nones(v[idx]) 

42 filter_dict = SchemaCheck( 

43 pruned, 

44 single(action, pruned), 

45 'filter', 

46 '{0}, filter #{1}: {2}'.format(location, idx, pruned) 

47 ).result() 

48 logger.debug('Filter #{0}: {1}'.format(idx, filter_dict)) 

49 v[idx] = filter_dict 

50 # If we've made it here without raising an Exception, it's valid 

51 return v 

52 return f