| 1 |  |  | # Licensed to the StackStorm, Inc ('StackStorm') under one or more | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # contributor license agreements.  See the NOTICE file distributed with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # this work for additional information regarding copyright ownership. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # The ASF licenses this file to You under the Apache License, Version 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # (the "License"); you may not use this file except in compliance with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # the License.  You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | #     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | # Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | import six | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | from st2common.exceptions.content import ParseException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | from st2common.models.utils.action_alias_utils import extract_parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | __all__ = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     'list_format_strings_from_aliases', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     'normalise_alias_format_string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     'match_command_to_alias' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | def list_format_strings_from_aliases(aliases): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     ''' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     List patterns from a collection of alias objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     :param aliases: The list of aliases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     :type  aliases: ``list`` of :class:`st2common.models.api.action.ActionAliasAPI` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     :return: A description of potential execution patterns in a list of aliases. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     :rtype: ``list`` of ``list`` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ''' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     patterns = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     for alias in aliases: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         for format_ in alias.formats: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             display, representations = normalise_alias_format_string(format_) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             patterns.extend([(display, representation) for representation in representations]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     return patterns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | def normalise_alias_format_string(alias_format): | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     ''' | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     StackStorm action aliases can have two types; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         1. A simple string holding the format | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         2. A dictionary which hold numerous alias format "representation(s)" | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |            With a single "display" for help about the action alias. | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     This function processes both forms and returns a standardized form. | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     :param alias_format: The alias format | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     :type  alias_format: ``str`` or ``dict`` | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     :return: The representation of the alias | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     :rtype: ``tuple`` of (``str``, ``str``) | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     ''' | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     display = None | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     representation = [] | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     if isinstance(alias_format, six.string_types): | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         display = alias_format | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         representation.append(alias_format) | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     elif isinstance(alias_format, dict): | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         display = alias_format['display'] | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         representation = alias_format['representation'] | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     else: | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         raise TypeError("alias_format '%s' is neither a dictionary or string type." | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                         % repr(alias_format)) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     return (display, representation) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | def match_command_to_alias(command, aliases): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     Match the text against an action and return the action reference. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     results = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     for alias in aliases: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         format_strings = list_format_strings_from_aliases([alias]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         for format_string in format_strings: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 extract_parameters(format_str=format_string[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                                    param_stream=command) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             except ParseException: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 continue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             results.append((alias, format_string[0], format_string[1])) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |     return results | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |