def test_normalise_alias_format_string_error(self, mock): alias_list = ["Quite an experience to live in fear, isn't it?"] expected_msg = ("alias_format '%s' is neither a dictionary or string type." % repr(alias_list)) with self.assertRaises(TypeError) as cm: matching.normalise_alias_format_string(alias_list) self.assertEqual(str(cm), expected_msg)
def test_normalise_alias_format_string_error(self, mock): alias_list = ["Quite an experience to live in fear, isn't it?"] expected_msg = ("alias_format '%s' is neither a dictionary or string type." % repr(alias_list)) with self.assertRaises(TypeError) as cm: matching.normalise_alias_format_string(alias_list) self.assertEqual(cm.exception.message, expected_msg)
def test_normalise_alias_format_string(self, mock): result = matching.normalise_alias_format_string( 'Quite an experience to live in fear, isn\'t it?') self.assertEqual([result[0]], result[1]) self.assertEqual(result[0], "Quite an experience to live in fear, isn't it?")
def generate_helpstring_result(aliases, filter=None, pack=None, limit=0, offset=0): """ List help strings from a collection of alias objects. :param aliases: The list of aliases :type aliases: ``list`` of :class:`st2common.models.api.action.ActionAliasAPI` :param filter_: A search pattern. :type filter_: ``string`` :param pack: Name of a pack :type pack: ``string`` :param limit: The number of help strings to return in the list. :type limit: ``integer`` :param offset: The offset in the list to start returning help strings. :type limit: ``integer`` :return: A list of aliases help strings. :rtype: ``list`` of ``list`` """ matches = [] count = 0 if not (isinstance(limit, int) and isinstance(offset, int)): raise TypeError("limit or offset argument is not an integer") for alias in aliases: # Skip disable aliases. if not alias.enabled: continue # Skip packs which don't explicitly match the requested pack. if pack and pack != alias.pack: continue for format_ in alias.formats: display, _, _ = normalise_alias_format_string(format_) if display: # Skip help strings not containing keyword. if not re.search(filter or "", display, flags=re.IGNORECASE): continue # Skip over help strings not within the requested offset/limit range. if (offset == 0 and limit > 0) and count >= limit: count += 1 continue elif (offset > 0 and limit == 0) and count < offset: count += 1 continue elif (offset > 0 and limit > 0) and (count < offset or count >= offset + limit): count += 1 continue matches.append({ "pack": alias.pack, "display": display, "description": alias.description, }) count += 1 return {"available": count, "helpstrings": matches}
def generate_helpstring_result(aliases, filter_="", pack="", limit=0, offset=0): """ List help strings from a collection of alias objects. :param aliases: The list of aliases :type aliases: ``list`` of :class:`st2common.models.api.action.ActionAliasAPI` :param filter_: A search pattern. :type filter_: ``string`` :param pack: Name of a pack :type pack: ``string`` :param limit: The number of help strings to return in the list. :type limit: ``integer`` :param offset: The offset in the list to start returning help strings. :type limit: ``integer`` :return: A list of aliases help strings. :rtype: ``list`` of ``list`` """ matches = {} count = 0 if not (isinstance(limit, int) and isinstance(offset, int)): raise TypeError for alias in aliases: # Skip disable aliases. if not alias.enabled: continue # Skip packs which don't explicitly match the requested pack. if pack != alias.pack and pack != "": continue for format_ in alias.formats: display, _ = normalise_alias_format_string(format_) if display: # Skip help strings not containing keyword. if not re.search(filter_, display, flags=re.IGNORECASE): continue # Skip over help strings not within the requested offset/limit range. if (offset == 0 and limit > 0) and count >= limit: count += 1 continue elif (offset > 0 and limit == 0) and count < offset: count += 1 continue elif (offset > 0 and limit > 0) and (count < offset or count >= offset + limit): count += 1 continue if alias.pack not in matches: matches[alias.pack] = [] matches[alias.pack].append({ "display": display, "description": alias.description }) count += 1 return {"available": count, "helpstrings": matches}