18 from fnmatch
import fnmatchcase
19 from random
import randint
20 from string
import ascii_lowercase, ascii_uppercase, digits
24 from robot.utils import (FileReader, is_bytes, is_string, is_truthy,
25 parse_re_flags, safe_str, type_name)
50 ROBOT_LIBRARY_SCOPE =
'GLOBAL'
125 raise TypeError(
'This keyword works only with Unicode strings.')
127 exclude = [e.strip()
for e
in exclude.split(
',')]
130 exclude = [re.compile(
'^%s$' % e)
for e
in exclude]
133 if any(e.match(word)
for e
in exclude)
or not word.islower():
135 for index, char
in enumerate(word):
137 return word[:index] + word[index].title() + word[index+1:]
140 tokens = re.split(
r'(\s+)', string, flags=re.UNICODE)
141 return ''.join(title(token)
for token
in tokens)
164 return bytes(string.encode(encoding, errors))
187 raise TypeError(
'Cannot decode strings.')
188 return bytes.decode(encoding, errors)
214 if os.path.isabs(template)
and os.path.isfile(template):
215 template = template.replace(
'/', os.sep)
216 logger.info(
'Reading template from file <a href="%s">%s</a>.'
217 % (template, template), html=
True)
218 with FileReader(template)
as reader:
219 template = reader.read()
220 return template.format(*positional, **named)
224 count = len(string.splitlines())
225 logger.info(
'%d lines' % count)
251 lines = string.splitlines()[start:end]
252 logger.info(
'%d lines returned' % len(lines))
269 return string.splitlines()[line_number]
296 pattern = pattern.lower()
297 contains =
lambda line: pattern
in line.lower()
299 contains =
lambda line: pattern
in line
332 pattern = pattern.lower()
333 matches =
lambda line: fnmatchcase(line.lower(), pattern)
335 matches =
lambda line: fnmatchcase(line, pattern)
378 match = re.compile(pattern +
'$', flags=
parse_re_flags(flags)).match
382 lines = string.splitlines()
383 matching = [line
for line
in lines
if matches(line)]
384 logger.info(
'%d out of %d lines matched' % (len(matching), len(lines)))
385 return '\n'.join(matching)
423 groups = [self.
_parse_group_parse_group(g)
for g
in groups]
424 return [m.group(*groups)
for m
in regexp.finditer(string)]
454 return string.replace(search_for, replace_with, count)
482 return re.sub(pattern, replace_with, string, max(count, 0), flags=
parse_re_flags(flags))
504 for removable
in removables:
525 for pattern
in patterns:
553 return string.split(separator, max_split)
569 return string.rsplit(separator, max_split)
587 return string.split(marker)[0]
597 return string.split(marker)[-1]
628 if isinstance(length, str)
and re.match(
r'^\d+-\d+$', length):
629 min_length, max_length = length.split(
'-')
634 for name, value
in [(
'[LOWER]', ascii_lowercase),
635 (
'[UPPER]', ascii_uppercase),
636 (
'[LETTERS]', ascii_lowercase + ascii_uppercase),
637 (
'[NUMBERS]', digits)]:
638 chars = chars.replace(name, value)
639 maxi = len(chars) - 1
640 return ''.join(chars[randint(0, maxi)]
for _
in range(length))
658 return string[start:end]
683 method = {
'BOTH': string.strip,
684 'LEFT': string.lstrip,
685 'RIGHT': string.rstrip,
686 'NONE':
lambda characters: string}[mode.upper()]
688 raise ValueError(
"Invalid mode '%s'." % mode)
689 return method(characters)
697 self.
_fail_fail(msg,
"'%s' is %s, not a string.", item,
type_name(item))
705 self.
_fail_fail(msg,
"'%s' is a string.", item)
714 self.
_fail_fail(msg,
"'%s' is not a Unicode string.", item)
724 self.
_fail_fail(msg,
"'%s' is not a byte string.", item)
737 if not string.islower():
738 self.
_fail_fail(msg,
"'%s' is not lower case.", string)
751 if not string.isupper():
752 self.
_fail_fail(msg,
"'%s' is not upper case.", string)
785 self.
_fail_fail(msg,
"'%s' is not title case.", string)
798 raise ValueError(
"Cannot convert '%s' argument '%s' to an integer."
801 def _fail(self, message, default_template, *items):
803 message = default_template % tuple(
safe_str(item)
for item
in items)
A library for string manipulation and verification.
def format_string(self, template, *positional, **named)
Formats a template using the given positional and named arguments.
def should_be_byte_string(self, item, msg=None)
Fails if the given item is not a byte string.
def should_be_lower_case(self, string, msg=None)
Fails if the given string is not in lower case.
def fetch_from_left(self, string, marker)
Returns contents of the string before the first occurrence of marker.
def strip_string(self, string, mode='both', characters=None)
Remove leading and/or trailing whitespaces from the given string.
def remove_string(self, string, *removables)
Removes all removables from the given string.
def encode_string_to_bytes(self, string, encoding, errors='strict')
Encodes the given Unicode string to bytes using the given encoding.
def should_be_title_case(self, string, msg=None, exclude=None)
Fails if given string is not title.
def split_string(self, string, separator=None, max_split=-1)
Splits the string using separator as a delimiter string.
def get_regexp_matches(self, string, pattern, *groups, flags=None)
Returns a list of all non-overlapping matches in the given string.
def should_be_unicode_string(self, item, msg=None)
Fails if the given item is not a Unicode string.
def _parse_group(self, group)
def get_line_count(self, string)
Returns and logs the number of lines in the given string.
def _fail(self, message, default_template, *items)
def get_line(self, string, line_number)
Returns the specified line from the given string.
def get_lines_matching_pattern(self, string, pattern, case_insensitive=False)
Returns lines of the given string that match the pattern.
def _convert_to_integer(self, value, name)
def should_be_string(self, item, msg=None)
Fails if the given item is not a string.
def _convert_to_index(self, value, name)
def replace_string(self, string, search_for, replace_with, count=-1)
Replaces search_for in the given string with replace_with.
def split_string_to_characters(self, string)
Splits the given string to characters.
def _get_matching_lines(self, string, matches)
def replace_string_using_regexp(self, string, pattern, replace_with, count=-1, flags=None)
Replaces pattern in the given string with replace_with.
def convert_to_lower_case(self, string)
Converts string to lower case.
def get_lines_containing_string(self, string, pattern, case_insensitive=False)
Returns lines of the given string that contain the pattern.
def should_be_upper_case(self, string, msg=None)
Fails if the given string is not in upper case.
def fetch_from_right(self, string, marker)
Returns contents of the string after the last occurrence of marker.
def split_string_from_right(self, string, separator=None, max_split=-1)
Splits the string using separator starting from right.
def generate_random_string(self, length=8, chars='[LETTERS][NUMBERS]')
Generates a string with a desired length from the given chars.
def get_lines_matching_regexp(self, string, pattern, partial_match=False, flags=None)
Returns lines of the given string that match the regexp pattern.
def convert_to_upper_case(self, string)
Converts string to upper case.
def get_substring(self, string, start, end=None)
Returns a substring from start index to end index.
def remove_string_using_regexp(self, string, *patterns, flags=None)
Removes patterns from the given string.
def decode_bytes_to_string(self, bytes, encoding, errors='strict')
Decodes the given bytes to a Unicode string using the given encoding.
def convert_to_title_case(self, string, exclude=None)
Converts string to title case.
def should_not_be_string(self, item, msg=None)
Fails if the given item is a string.
def split_to_lines(self, string, start=0, end=None)
Splits the given string to lines.
def parse_re_flags(flags=None)
def is_truthy(item)
Returns True or False depending on is the item considered true or not.
def type_name(item, capitalize=False)
Return "non-technical" type name for objects and types.
def get_version(naked=False)