Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2############################################################################## 

3## SDCFileNameHelpers 

4## Helper functions for getting and transforming file names 

5## 

6############################################################################## 

7""" 

8 

9from jinja2 import Template 

10 

11 

12class SDCFileNameHelpers(): 

13 """ Helper functions for naming files""" 

14 

15 @staticmethod 

16 def get_file_name_from_schema(schema_, kwargs=None): 

17 """ Returns a string representing the file_name after any 

18 file_name_params in the schema have been substituted into 

19 the filename from values in kwargs dictionary for key 

20 'file_name_params'. 

21 

22 param: schema_: schema containing a file_info key that is a dictionary containing a file_name key, and optionally file_name_params key 

23 type: schema_: dict 

24 param: kwargs: contains an optional file_name_params key with value a dictionary of key value pairs to be substituted in the file_name. 

25 type: kwargs: dict 

26 raises: Exception: if file_name does not exist in schema 

27 return: a file_name defined by the schema with any file_name_params substituted 

28 rtype: str 

29 """ 

30 

31 if kwargs is None: 

32 kwargs = {} 

33 

34 file_name = None 

35 file_name_params = None 

36 

37 if schema_['info'].get('type') == "local_disk": 

38 file_name = schema_['info']['access'].get('file_name', None) 

39 file_name_params = schema_['info']['access'].get('file_name_params', None) 

40 else: 

41 file_name = schema_['info']['file_info']['opts'].get('file_name', None) 

42 file_name_params = schema_['info']['file_info']['opts'].get('file_name_params', None) 

43 

44 if file_name_params: 

45 return SDCFileNameHelpers.get_file_name(file_name, file_name_params_=file_name_params, kwargs=kwargs) 

46 return SDCFileNameHelpers.get_file_name(file_name, kwargs=kwargs) 

47 

48 @staticmethod 

49 def get_file_name(file_name_, file_name_params_=None, kwargs=None): 

50 """ Returns a string representing the query associated with query_name after any query_params (if passed) 

51 have been substituted into the query from values in kwargs dictionary for key 'query_params'. 

52 

53 param: file_name_: name of the file to get 

54 type: file_name:_ str 

55 param: file_name_params_: list of valid parameters for the file_name 

56 type: file_name_params_: list 

57 param: kwargs: contains an optional file_params key with value a dictionary of key value pairs to be substituted in the file_name. 

58 type: kwargs: dict 

59 raises: Exception: if file_name_params is not a list 

60 raises: Exception: keys in kwargs['file_name_params'] are not in file_name_params list 

61 raises: Exception: if file_name_params list is non-empty, but kwargs['file_name_params'] is empty 

62 return: a file_name defined by the schema with any file_name_params substituted 

63 rtype: str 

64 """ 

65 

66 if file_name_params_ is None: 

67 file_name_params_ = [] 

68 

69 if kwargs is None: 

70 kwargs = {} 

71 

72 raw_file_name = file_name_ 

73 

74 if not isinstance(file_name_params_, list): 

75 raise Exception('Bad file_name_params in schema: must be a list') 

76 

77 try: 

78 kwargs['file_name_params'] 

79 if not isinstance(kwargs['file_name_params'], dict): 

80 raise Exception('Bad file_name_params: must be a dictionary') 

81 for param in kwargs['file_name_params']: 

82 if param not in file_name_params_: 

83 raise Exception('Passed parameter "' + param + '" not expected by schema') 

84 except KeyError: 

85 if file_name_params_: 

86 raise Exception('Required parameters are missing') 

87 

88 for param in file_name_params_: 

89 if param not in kwargs['file_name_params']: 

90 raise Exception('Failed to pass parameter "' + param + '" expected by schema') 

91 

92 file_name = None 

93 if file_name_params_ and kwargs['file_name_params']: 

94 template = Template(raw_file_name) 

95 file_name = template.render(kwargs['file_name_params']) 

96 else: 

97 file_name = raw_file_name 

98 

99 return file_name