def execute_and_wait_with(args): global CTRL_C_PRESSED if CTRL_C_PRESSED: # Keyboard interrupt has happened! return time.sleep(0) datasources, outs_dir, options, suite_name, command, verbose, (argfile_index, argfile) = args datasources = [d.encode('utf-8') if PY2 and is_unicode(d) else d for d in datasources] outs_dir = os.path.join(outs_dir, argfile_index, suite_name) pool_id = _make_id() caller_id = uuid.uuid4().hex cmd = command + _options_for_custom_executor(options, outs_dir, suite_name, argfile, caller_id) + datasources cmd = [c if not any(bad in c for bad in _BOURNELIKE_SHELL_BAD_CHARS_WITHOUT_DQUOTE) else '"%s"' % c for c in cmd] os.makedirs(outs_dir) try: with open(os.path.join(outs_dir, 'stdout.txt'), 'w') as stdout: with open(os.path.join(outs_dir, 'stderr.txt'), 'w') as stderr: process, (rc, elapsed) = _run(cmd, stderr, stdout, suite_name, verbose, pool_id) except: print(sys.exc_info()[0]) if rc != 0: _write_with_id(process, pool_id, _execution_failed_message(suite_name, stdout, stderr, rc, verbose), Color.RED) if _PABOTLIBPROCESS or _PABOTLIBURI != '127.0.0.1:8270': Remote(_PABOTLIBURI).run_keyword('release_locks', [caller_id], {}) else: _write_with_id(process, pool_id, _execution_passed_message(suite_name, stdout, stderr, elapsed, verbose), Color.GREEN)
def decode_bytes_to_string(self, bytes, encoding, errors='strict'): """Decodes the given ``bytes`` to a Unicode string using the given ``encoding``. ``errors`` argument controls what to do if decoding some bytes fails. All values accepted by ``decode`` method in Python are valid, but in practice the following values are most useful: - ``strict``: fail if characters cannot be decoded (default) - ``ignore``: ignore characters that cannot be decoded - ``replace``: replace characters that cannot be decoded with a replacement character Examples: | ${string} = | Decode Bytes To String | ${bytes} | UTF-8 | | ${string} = | Decode Bytes To String | ${bytes} | ASCII | errors=ignore | Use `Encode String To Bytes` if you need to convert Unicode strings to byte strings, and `Convert To String` in ``BuiltIn`` if you need to convert arbitrary objects to Unicode strings. New in Robot Framework 2.7.7. """ if PY3 and is_unicode(bytes): raise TypeError('Can not decode strings on Python 3.') return bytes.decode(encoding, errors)
def string(self, string, escape=True, attr=False): if escape and string: if not is_unicode(string): string = unicode(string) escaper = html_escape if not attr else html_attr_escape string = escaper(string) return self._strings.add(string)
def _options_to_cli_arguments(opts): res = [] for k, v in opts.items(): if isinstance(v, str): res += ['--' + str(k), str(v)] elif PY2 and is_unicode(v): res += ['--' + str(k), v.encode('utf-8')] elif isinstance(v, bool) and (v is True): res += ['--' + str(k)] elif isinstance(v, list): for value in v: if PY2 and is_unicode(value): res += ['--' + str(k), value.encode('utf-8')] else: res += ['--' + str(k), str(value)] return res
def convert_to_title_case(self, string, exclude=None): """Converts string to title case. Uses the following algorithm: - Split the string to words from whitespace characters (spaces, newlines, etc.). - Exclude words that are not all lower case. This preserves, for example, "OK" and "iPhone". - Exclude also words listed in the optional ``exclude`` argument. - Title case the first alphabetical character of each word that has not been excluded. - Join all words together so that original whitespace is preserved. Explicitly excluded words can be given as a list or as a string with words separated by a comma and an optional space. Excluded words are actually considered to be regular expression patterns, so it is possible to use something like "example[.!?]?" to match the word "example" on it own and also if followed by ".", "!" or "?". See `BuiltIn.Should Match Regexp` for more information about Python regular expression syntax in general and how to use it in Robot Framework test data in particular. Examples: | ${str1} = | Convert To Title Case | hello, world! | | ${str2} = | Convert To Title Case | it's an OK iPhone | exclude=a, an, the | | ${str3} = | Convert To Title Case | distance is 1 km. | exclude=is, km.? | | Should Be Equal | ${str1} | Hello, World! | | Should Be Equal | ${str2} | It's an OK iPhone | | Should Be Equal | ${str3} | Distance is 1 km. | The reason this keyword does not use Python's standard [https://docs.python.org/library/stdtypes.html#str.title|title()] method is that it can yield undesired results, for example, if strings contain upper case letters or special characters like apostrophes. It would, for example, convert "it's an OK iPhone" to "It'S An Ok Iphone". New in Robot Framework 3.2. """ if not is_unicode(string): raise TypeError('This keyword works only with Unicode strings.') if is_string(exclude): exclude = [e.strip() for e in exclude.split(',')] elif not exclude: exclude = [] exclude = [re.compile('^%s$' % e) for e in exclude] def title(word): if any(e.match(word) for e in exclude) or not word.islower(): return word for index, char in enumerate(word): if char.isalpha(): return word[:index] + word[index].title() + word[index + 1:] return word tokens = re.split(r'(\s+)', string, flags=re.UNICODE) return ''.join(title(token) for token in tokens)
def _normalize_message(self, msg): if callable(msg): return msg if not is_unicode(msg): msg = unic(msg) if '\r\n' in msg: msg = msg.replace('\r\n', '\n') return msg
def _get_type(self, name, value): if not is_unicode(value): return None, None if name in self._argspec.types: return self._argspec.types[name], True if name in self._argspec.defaults: return type(self._argspec.defaults[name]), False return None, None
def _resolve_condition(self, unresolved_condition): condition, _ = VariableReplacer().replace( [unresolved_condition], (), variables=self._context.variables) resolved_condition = condition[0] if is_unicode(resolved_condition): return evaluate_expression(resolved_condition, self._context.variables) return bool(resolved_condition)
def should_be_unicode_string(self, item, msg=None): """Fails if the given ``item`` is not a Unicode string. On Python 3 this keyword behaves exactly the same way `Should Be String`. That keyword should be used instead and this keyword will be deprecated. """ if not is_unicode(item): self._fail(msg, "'%s' is not a Unicode string.", item)
def _should_run_branch(self, condition, recursive_dry_run=False): if self._context.dry_run: return not recursive_dry_run if not self._run: return False if condition is None: return True condition = self._context.variables.replace_scalar(condition) if is_unicode(condition): return evaluate_expression(condition, self._context.variables.current.store) return bool(condition)
def __init__(self, name, tests=None, suites=None, dynamictests=None): # type: (str, Optional[List[str]], Optional[List[str]], Optional[List[str]]) -> None assert (PY2 and isinstance(name, basestring)) or isinstance(name, str) self.name = name.encode("utf-8") if PY2 and is_unicode(name) else name testslist = [TestItem(t) for t in tests or [] ] # type: List[Union[TestItem, DynamicTestItem]] dynamictestslist = [ DynamicTestItem(t, self.name) for t in dynamictests or [] ] # type: List[Union[TestItem, DynamicTestItem]] self.tests = testslist + dynamictestslist self.suites = [SuiteItem(s) for s in suites or []]
def _to_string(self, value, allow_tuple=False, allow_none=False): if is_unicode(value): return value if is_bytes(value): return value.decode('UTF-8') if allow_tuple and is_list_like(value) and len(value) > 0: return tuple(value) if allow_none and value is None: return value or_tuple = ' or a non-empty tuple' if allow_tuple else '' raise DataError('Return value must be a string%s, got %s.' % (or_tuple, type_name(value)))
def should_be_unicode_string(self, item, msg=None): """Fails if the given ``item`` is not a Unicode string. Use `Should Be Byte String` if you want to verify the ``item`` is a byte string, or `Should Be String` if both Unicode and byte strings are fine. See `Should Be String` for more details about Unicode strings and byte strings. The default error message can be overridden with the optional ``msg`` argument. """ if not is_unicode(item): self._fail(msg, "'%s' is not a Unicode string.", item)
def _get_stdin(self, stdin): if not is_string(stdin): return stdin if stdin.upper() == 'NONE': return None if stdin == 'PIPE': return subprocess.PIPE path = os.path.normpath(os.path.join(self.cwd, stdin)) if os.path.isfile(path): return open(path) stdin_file = TemporaryFile() if is_unicode(stdin): stdin = console_encode(stdin, self.output_encoding, force=True) stdin_file.write(stdin) stdin_file.seek(0) return stdin_file
def execute_and_wait_with(args): global CTRL_C_PRESSED if CTRL_C_PRESSED: # Keyboard interrupt has happened! return time.sleep(0) datasources, outs_dir, options, suite_name, command, verbose, (argfile_index, argfile) = args datasources = [d.encode('utf-8') if PY2 and is_unicode(d) else d for d in datasources] outs_dir = os.path.join(outs_dir, argfile_index, suite_name) os.makedirs(outs_dir) caller_id = uuid.uuid4().hex cmd = command + _options_for_custom_executor(options, outs_dir, suite_name, argfile, caller_id) + datasources cmd = _mapOptionalQuote(cmd) _try_execute_and_wait(cmd, outs_dir,suite_name, verbose, _make_id(), caller_id) outputxml_preprocessing(options, outs_dir, suite_name, verbose, _make_id(), caller_id)
def execute_and_wait_with(args): global CTRL_C_PRESSED, _NUMBER_OF_ITEMS_TO_BE_EXECUTED is_last = _NUMBER_OF_ITEMS_TO_BE_EXECUTED == 1 _NUMBER_OF_ITEMS_TO_BE_EXECUTED -= 1 if CTRL_C_PRESSED: # Keyboard interrupt has happened! return time.sleep(0) datasources, outs_dir, options, execution_item, command, verbose, (argfile_index, argfile) = args datasources = [d.encode('utf-8') if PY2 and is_unicode(d) else d for d in datasources] outs_dir = os.path.join(outs_dir, argfile_index, execution_item.name) os.makedirs(outs_dir) caller_id = uuid.uuid4().hex cmd = command + _options_for_custom_executor(options, outs_dir, execution_item, argfile, caller_id, is_last) + datasources cmd = _mapOptionalQuote(cmd) _try_execute_and_wait(cmd, outs_dir, execution_item.name, verbose, _make_id(), caller_id) outputxml_preprocessing(options, outs_dir, execution_item.name, verbose, _make_id(), caller_id)
def _convert(self, name, value): if not is_unicode(value): return value if name in self._argspec.annotations: type_ = self._argspec.annotations[name] explicit_type = True elif name in self._argspec.default_values: type_ = type(self._argspec.default_values[name]) explicit_type = False else: return value converter = self._get_converter(type_) if not converter: return value # TODO: Clean this up. Most likely converters should be classes. # Also, consider allowing 'NONE' with enums. if value.upper() == 'NONE' and converter not in ( self._convert_bytes, self._convert_bytearray): return None return converter(name, value, explicit_type)
def __init__(self, name, suite): # type: (str, str) -> None self.name = name.encode("utf-8") if PY2 and is_unicode(name) else name self.suite = suite # type:str
def _to_string(self, value): if not is_string(value): raise DataError('Return value must be string.') return value if is_unicode(value) else unic(value, 'UTF-8')
def _to_string(self, value): if is_unicode(value): return value if is_bytes(value): return value.decode('UTF-8') raise DataError('Return value must be string.')
def string(self, string, escape=True, attr=False): if escape and string: if not is_unicode(string): string = unic(string) string = (html_escape if not attr else attribute_escape)(string) return self._strings.add(string)
def _to_string(self, value): if not (is_string(value) or is_bytes(value)): raise DataError("Return value must be string.") return value if is_unicode(value) else unic(value, "UTF-8")
def string(self, string, escape=True, attr=False): if escape and string: if not is_unicode(string): string = unicode(string) string = (html_escape if not attr else attribute_escape)(string) return self._strings.add(string)
def test_is_unicode(self): assert_true(is_unicode('Hyvä')) assert_true(is_unicode('Paha')) assert_false(is_unicode(b'xxx')) assert_false(is_unicode(42))