Conditions | 9 |
Total Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
61 | def validate_trigger_parameters(trigger_type_ref, parameters): |
||
62 | """ |
||
63 | This function validates parameters for system and user-defined triggers. |
||
64 | |||
65 | :param trigger_type_ref: Reference of a trigger type. |
||
66 | :type trigger_type_ref: ``str`` |
||
67 | |||
68 | :param parameters: Trigger parameters. |
||
69 | :type parameters: ``dict`` |
||
70 | |||
71 | :return: Cleaned parameters on success, None if validation is not performed. |
||
72 | """ |
||
73 | if not trigger_type_ref: |
||
74 | return None |
||
75 | |||
76 | is_system_trigger = trigger_type_ref in SYSTEM_TRIGGER_TYPES |
||
77 | if is_system_trigger: |
||
78 | # System trigger |
||
79 | parameters_schema = SYSTEM_TRIGGER_TYPES[trigger_type_ref]['parameters_schema'] |
||
80 | else: |
||
81 | trigger_type_db = triggers.get_trigger_type_db(trigger_type_ref) |
||
82 | if not trigger_type_db: |
||
83 | # Trigger doesn't exist in the database |
||
84 | return None |
||
85 | |||
86 | parameters_schema = getattr(trigger_type_db, 'parameters_schema', {}) |
||
87 | if not parameters_schema: |
||
88 | # Parameters schema not defined for the this trigger |
||
89 | return None |
||
90 | |||
91 | # We only validate non-system triggers if config option is set (enabled) |
||
92 | if not is_system_trigger and not cfg.CONF.system.validate_trigger_parameters: |
||
93 | LOG.debug('Got non-system trigger "%s", but trigger parameter validation for non-system' |
||
|
|||
94 | 'triggers is disabled, skipping validation.' % (trigger_type_ref)) |
||
95 | return None |
||
96 | |||
97 | cleaned = util_schema.validate(instance=parameters, schema=parameters_schema, |
||
98 | cls=util_schema.CustomValidator, use_default=True, |
||
99 | allow_default_none=True) |
||
100 | |||
101 | # Additional validation for CronTimer trigger |
||
102 | # TODO: If we need to add more checks like this we should consider abstracting this out. |
||
103 | if trigger_type_ref == CRON_TIMER_TRIGGER_REF: |
||
104 | # Validate that the user provided parameters are valid. This is required since JSON schema |
||
105 | # allows arbitrary strings, but not any arbitrary string is a valid CronTrigger argument |
||
106 | # Note: Constructor throws ValueError on invalid parameters |
||
107 | try: |
||
108 | CronTrigger(**parameters) |
||
109 | except: |
||
110 | msg = ('Cron trigger parameters do not match expected format.') |
||
111 | LOG.exception(msg) |
||
112 | raise TriggerParametersValidationException(msg) |
||
113 | |||
114 | return cleaned |
||
115 | |||
158 |