def parse(cls, obj): cls._known_funcs.add(obj.name) return "\n".join([ "{}() {{".format(obj.name), parse(obj.args), parse_body(obj.body), "}", ])
def __init__(self, mock=None, side_effect=None): if mock is not None: self.mock = mock else: self.mock = Mock() if side_effect is not None: self.side_effect_src = side_effect else: self.side_effect_src = "ss.noop()" self.side_effect = parse_body(ast.parse(self.side_effect_src).body)
def parse(cls, obj): out_lines = [] if isinstance(obj.test, BoolOp): conds = " {} ".format(parse(obj.test.op)).join( [cls.parse_conditional(val) for val in obj.test.values]) else: conds = cls.parse_conditional(obj.test) out_lines.append("if {conds}; then".format(conds=conds)) out_lines.append( "{body}".format(body=parse_body(obj.body)) ) if obj.orelse: out_lines.append("else") out_lines.append( "{body}".format(body=parse_body(obj.orelse)) ) out_lines.append("fi") return out_lines
def convert_source(source_file, include_source=False, allow_errors=False): ConvertContext.include_source = include_source ConvertContext.allow_errors = allow_errors with open(source_file, 'r') as f: lines = f.readlines() ConvertContext.lines = lines parsed = parse_body(ast.parse("".join(lines)).body) body = "#!" + ConvertContext.shebang + "\n" body += get_testing_mocks() if ConvertContext.colors_used: body += _get_color_init() body += parsed return body
def assert_parsed_raises(self, source, exc=Unparseable): with self.assertRaises(exc): parse_body(ast.parse(source).body)
def assert_parsed(self, source, output): parsed = parse_body(ast.parse(source).body) self.assertEqual(parsed, output.strip())