def handler_filter(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> None """Run filter on STDIN to STDOUT.""" ucr = ConfigRegistry() ucr.load() stdout = sys.stdout if six.PY2 else sys.stdout.buffer # type: IO[bytes] # type: ignore stdout.write(run_filter(sys.stdin.read(), ucr, opts=opts))
def ucr_run_filter(string, additional=None): ucr = _UCR if additional: # memory only ucr. not saved. # if we would... NEVER __setitem__ on ucr! ucr = deepcopy(ucr) for k, v in additional.iteritems(): ucr[k] = v return run_filter(string, ucr)
def ucr_run_filter(string, additional=None): ucr = _UCR if additional: # memory only ucr. not saved. # if we would... NEVER __setitem__ on ucr! ucr = deepcopy(ucr) for k, v in additional.items(): ucr[k] = v value = run_filter(string, ucr) if six.PY2: return value return value.decode('UTF-8')
def _eval_default(self, default): # type: (str) -> str """ Recursively evaluate default value. :param default: Default value. :returns: Substituted value. """ try: value = run_filter(default, self, opts={'disallow-execution': True}) except RuntimeError: # maximum recursion depth exceeded value = b'' if six.PY2: return value return value.decode("UTF-8")
def handler_filter(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> None """Run filter on STDIN to STDOUT.""" ucr = ConfigRegistry() ucr.load() sys.stdout.write(run_filter(sys.stdin.read(), ucr, opts=opts))
def __getitem__(self, key): # type: ignore value = super(_DefaultConfigRegistry, self).__getitem__(key) try: return run_filter(value, self.parent, opts={'disallow-execution': True}) except RuntimeError: # maximum recursion depth exceeded return ''
def handler_filter(args, opts=dict()): """Run filter on STDIN to STDOUT.""" ucr = ConfigRegistry() ucr.load() sys.stdout.write(run_filter(sys.stdin.read(), ucr, opts=opts))
def test_filter_warning(tmpl, line, ucrf): assert line in ucrh.run_filter(tmpl, ucrf, {"01head", "02tail"})
def test_filter_script(mocker, ucrf): Popen = mocker.patch("subprocess.Popen") Popen.return_value.communicate.return_value = ("42", "") assert ucrh.run_filter("@!@print(42)@!@", ucrf) == "42" Popen.assert_called_once() Popen.return_value.communicate.assert_called_once()
def test_filter_var(tmpl, out, ucrf): assert ucrh.run_filter(tmpl, ucrf) == out
def test_filter_var(tmpl, out, ucrf): assert ucrh.run_filter(tmpl, ucrf) == out.encode('ASCII')
def ucr_run_filter(string): return run_filter(string, _UCR)
def test_configfile_python_compatibility(ucr_config_file, python_versions, dpkg): with open(ucr_config_file) as fd: template = fd.read() msg = [] python = {} for (pyver, fn) in python_versions: sys.executable = fn["py"] run_filter(template, ucr) data = fn["out"].read_text('ISO8859-1').rstrip('\n') ret = int(fn["ret"].read().strip()) cmd_ucr = ["python{0}".format(pyver), "-m", "coverage", "report"] cov = subprocess.check_output(cmd_ucr).decode('UTF-8', 'replace') try: line, = [ line for line in cov.splitlines() if str(fn["tmp"]) in line ] _name, _stmts, _miss, coverage = line.split() except ValueError: coverage = "?" python[pyver] = { 'success': ret == 0, 'compiled': data, 'coverage': coverage, } if ucr_config_file.endswith('.json'): try: python[pyver]['compiled'] = json.dumps(json.loads(data), sort_keys=True) except ValueError: python[pyver]['json_failed'] = True if os.path.exists('.coverage'): os.unlink('.coverage') msg.append('Py%s:|%s|%s' % ( pyver, '✅' if ret == 0 else '❎', coverage, )) print('\t'.join(msg), end='\t') try: diff = ''.join( unified_diff( *(python[pyver]["compiled"].splitlines(keepends=True) for pyver in VERSIONS), *(str(pyver) for pyver in VERSIONS), )) except LookupError: diff = "" details = { 'python': python, 'package': dpkg.get(ucr_config_file, ""), 'diff': diff, } print(diff) ignore = IGNORE.get(ucr_config_file, set()) assert all(res['success'] for pyver, res in python.items() if pyver not in ignore), details assert len({ res['compiled'] for pyver, res in python.items() if pyver not in ignore }) == 1, details