예제 #1
0
 def to_node(self):
     if self.is_tuple != '':
         elements = self.__create_elements()
         return ast.Tuple(elts=elements, ctx=ast.Load)
     elif self.is_list != '':
         elements = self.__create_elements()
         return ast.List(elts=elements, ctx=ast.Load)
     elif self.is_dict != '':
         if self.values is None:
             return ast.Dict(keys=[], values=[])
         elif self.__is_dict():
             keys, values = self.__create_keys_values()
             return ast.Dict(keys=keys, values=values)
         else:
             elements = self.__create_elements()
             return ast.Set(elts=elements, ctx=ast.Load)
     elif self.name is not None:
         return self.name.to_node()
     elif self.number is not None:
         return self.number.to_node()
     elif self.none is not None:
         return self.none.to_node()
     elif self.boolean is not None:
         return self.boolean.to_node()
     elif self.string is not None:
         return self.string[0].to_node()
예제 #2
0
    def replace_tc_pos(node):
        with switch(node):
            if ast.Expr(
                    value=ast.Call(func=func, args=args, keywords=keywords)):
                starred = [arg for arg in args if isinstance(arg, ast.Starred)]
                kwargs = [kw for kw in keywords if kw.arg is None]

                if len(kwargs):
                    kwargs = kwargs[0].value
                if len(starred):
                    starred = starred[0].value
                    with hq as code:
                        # get rid of starargs
                        return (TCOType.IGNORE, ast_literal[func],
                                (ast_literal[ast.List(args, ast.Load())] +
                                 list(ast_literal[starred])),
                                ast_literal[kwargs or ast.Dict([], [])])
                else:
                    with hq as code:
                        return (TCOType.IGNORE, ast_literal[func],
                                ast_literal[ast.List(args, ast.Load())],
                                ast_literal[kwargs or ast.Dict([], [])])
                return code
            elif ast.If(test=test, body=body, orelse=orelse):
                body[-1] = replace_tc_pos(body[-1])
                if orelse:
                    orelse[-1] = replace_tc_pos(orelse[-1])
                return ast.If(test, body, orelse)
            else:
                return node
예제 #3
0
 def test_dict(self):
     d = ast.Dict([], [ast.Name("x", ast.Load())])
     self.expr(d, "same number of keys as values")
     d = ast.Dict([None], [ast.Name("x", ast.Load())])
     self.expr(d, "None disallowed")
     d = ast.Dict([ast.Name("x", ast.Load())], [None])
     self.expr(d, "None disallowed")
예제 #4
0
    def return_replacer(tree, **kw):
        with switch(tree):
            if ast.Return(
                    value=ast.Call(func=func, args=args, keywords=keywords)):
                starred = [arg for arg in args if isinstance(arg, ast.Starred)]
                kwargs = [kw for kw in keywords if kw.arg is None]

                if len(kwargs):
                    kwargs = kwargs[0].value
                if len(starred):
                    starred = starred[0].value
                    with hq as code:
                        # get rid of starargs
                        return (TCOType.CALL, ast_literal[func],
                                (ast_literal[ast.List(args, ast.Load())] +
                                 list(ast_literal[starred])),
                                ast_literal[kwargs or ast.Dict([], [])])
                else:
                    with hq as code:
                        return (TCOType.CALL, ast_literal[func],
                                ast_literal[ast.List(args, ast.Load())],
                                ast_literal[kwargs or ast.Dict([], [])])

                return code
            else:
                return tree
예제 #5
0
 def to_ast(self):
     return ast.Call(func=super(Class, self).to_ast(), 
                     args=[ast.Str(s=self.name),
                           ast.Dict(keys=list(map(lambda x: ast.Str(s=x), self.members.keys())),
                                    values=list(map(lambda x: x.to_ast(), self.members.values()))),
                           ast.Dict(keys=list(map(lambda x: ast.Str(s=x), self.instance_members.keys())),
                                    values=list(map(lambda x: x.to_ast(), self.instance_members.values())))
                       ],
                     keywords=[], starargs=None, kwargs=None)
예제 #6
0
def build_requests_command(request):
    # Method
    tree = ast.parse('requests.{}()'.format(request.command.lower()))
    call = tree.body[0].value
    call.keywords = []

    # URL
    call.args.append(ast.Str(request.url()))

    # Authorization (prepare)
    method, token = request.auth()

    # Headers
    header_keys = []
    header_values = []
    for header in sorted(request.headers):
        if header in EXCLUDE_HEADERS_REQUESTS:
            continue
        header_keys.append(ast.Str(header))
        header_values.append(ast.Str(request.headers[header]))
    if method != 'Basic' and 'Authorization' in request.headers:
        header_keys.append(ast.Str('Authorization'))
        header_values.append(ast.Str(request.headers['Authorization']))
    if header_keys and header_values:
        call.keywords.append(
            ast.keyword('headers', ast.Dict(header_keys, header_values)))

    # JSON or raw data
    data = maybe_str(request.data())
    if data:
        if request.headers.get('Content-Type') == 'application/json':
            json_keys = []
            json_values = []
            for k, v in data.items():
                json_keys.append(ast.Str(maybe_str(k)))
                v = maybe_str(v)
                if isinstance(v, str):
                    json_values.append(ast.Str(v))
                else:
                    json_values.append(ast.parse(str(v)).body[0].value)
            if json_keys and json_values:
                call.keywords.append(
                    ast.keyword('json', ast.Dict(json_keys, json_values)))
        else:
            call.keywords.append(ast.keyword('data', ast.Str(data)))

    # Authorization
    if method == 'Basic':
        token = maybe_str(token)
        call.keywords.append(
            ast.keyword('auth', ast.Tuple(
                tuple(map(ast.Str, token.split(':'))), None)))

    return astunparse.unparse(tree).strip()
def test_gen_arguments_py(monkeypatch):
    assert isinstance(rm.gen_arguments_py([]), types.GeneratorType)
    assert list(rm.gen_arguments_py([])) == []
    ret = rm.gen_arguments_py(my_parameters)
    assert ast.dump(ret.__next__().value) == ast.dump(
        ast.Dict(keys=[[ast.Constant(value="type")]],
                 values=[[ast.Constant(value="bool")]]))
    assert ast.dump(ret.__next__().value) == ast.dump(
        ast.Dict(
            keys=[[ast.Constant(value="required")],
                  [ast.Constant(value="type")]],
            values=[[ast.Constant(value=True)], [ast.Constant(value="int")]],
        ))
예제 #8
0
def ast_repr(x):
    """Similar to repr(), but returns an AST instead of a String, which when
    evaluated will return the given value."""
    tx = type(x)
    if tx in (int, float):
        return ast.Num(n=x)
    elif tx is bytes:
        return ast.Bytes(s=x)
    elif isinstance(x, str):
        return ast.Str(s=x)
    elif tx is list:
        return ast.List(elts=list(map(ast_repr, x)))
    elif tx is dict:
        return ast.Dict(keys=list(map(ast_repr, x.keys())),
                        values=list(map(ast_repr, x.values())))
    elif tx is set:
        return ast.Set(elts=list(map(ast_repr, x)))
    elif tx is Literal:
        return x.body
    elif tx is Captured:
        return compat.Call(ast.Name(id="Captured"),
                           [x.val, ast_repr(x.name)], [])
    elif tx in (bool, type(None)):
        return ast.NameConstant(value=x)
    elif isinstance(x, ast.AST):
        fields = [ast.keyword(a, ast_repr(b)) for a, b in ast.iter_fields(x)]
        # This hard-codes an expectation that ast classes will be
        # bound to the name `ast`.  There must be a better way.
        return compat.Call(
            ast.Attribute(value=ast.Name(id='ast', ctx=ast.Load()),
                          attr=x.__class__.__name__,
                          ctx=ast.Load()), [], fields)

    raise Exception("Don't know how to ast_repr this: ", x)
예제 #9
0
 def visit_Module(self, module):
     """`TestStrings.visit_Module` initializes the capture. After all the nodes are visit we append `create_test and test_update`
     to populate the `"__test__"` attribute.
     """
     self.strings = []
     module = self.visit_body(module)
     module.body += ([create_test] + [
         ast.copy_location(
             ast.Expr(
                 ast.Call(
                     func=test_update,
                     args=[
                         ast.Dict(
                             keys=[
                                 ast.Str("string-{}".format(node.lineno))
                             ],
                             values=[node],
                         )
                     ],
                     keywords=[],
                 )),
             node,
         ) for node in self.strings
     ] if self.strings else [])
     return module
예제 #10
0
 def _py2ast(v, ctx):
     if isinstance(v, (int, long, float)):
         return ast.Num(v)
     elif isinstance(v, str):
         return ast.Str(v)
     elif isinstance(v, list):
         return ast.List([BaseASTOptimizer._py2ast(x, ctx) for x in v], ctx)
     elif isinstance(v, dict):
         items = v.items()
         keys = [BaseASTOptimizer._py2ast(k, ctx) for k, v in items]
         vals = [BaseASTOptimizer._py2ast(v, ctx) for k, v in items]
         return ast.Dict(keys, vals)
     elif isinstance(v, tuple):
         return ast.Tuple([BaseASTOptimizer._py2ast(x, ctx) for x in v],
                          ctx)
     elif isinstance(v, set):
         assert isinstance(ctx, ast.Load)
         return ast.Set([BaseASTOptimizer._py2ast(x, ctx) for x in v])
     elif v is None:
         assert isinstance(ctx, ast.Load)
         return ast.Name('None', ctx)
     elif v is True:
         assert isinstance(ctx, ast.Load)
         return ast.Name('True', ctx)
     elif v is False:
         assert isinstance(ctx, ast.Load)
         return ast.Name('False', ctx)
     else:
         assert False, ('_py2ast', v)
예제 #11
0
    def prepare(self):

        for i in self.children:
            i.prepare()

        # Compile the keywords.

        keyword_values = {}
        keyword_keys = []
        keyword_exprs = []

        for k, expr in self.keyword:

            node = py_compile(expr, 'eval', ast_node=True)

            if is_constant(node):
                keyword_values[k] = py_eval_bytecode(compile_expr(node))
            else:
                keyword_keys.append(ast.Str(s=k))
                keyword_exprs.append(node)

        if keyword_values:
            self.keyword_values = keyword_values
        else:
            self.keyword_values = None

        if keyword_keys:
            node = ast.Dict(keys=keyword_keys, values=keyword_exprs)
            ast.copy_location(node, keyword_exprs[0])
            self.keyword_exprs = compile_expr(node)
        else:
            self.keyword_exprs = None
예제 #12
0
def ast_repr(x):
    """Similar to repr(), but returns an AST instead of a String, which when
    evaluated will return the given value."""
    if type(x) in (int, float): return ast.Num(n=x)
    elif type(x) in (str, unicode): return ast.Str(s=x)
    elif type(x) is list: return ast.List(elts=map(ast_repr, x))
    elif type(x) is dict:
        return ast.Dict(keys=map(ast_repr, x.keys()),
                        values=map(ast_repr, x.values()))
    elif type(x) is set:
        return ast.Set(elts=map(ast_repr, x))
    elif type(x) is Literal:
        return x.body
    elif x is None:
        return ast.Name(id="None")
    elif x is True:
        return ast.Name(id="True")
    elif x is False:
        return ast.Name(id="False")
    elif isinstance(x, ast.AST):
        fields = [ast.keyword(a, ast_repr(b)) for a, b in ast.iter_fields(x)]
        return ast.Call(func=ast.Name(id=x.__class__.__name__),
                        args=[],
                        keywords=fields,
                        starargs=None,
                        kwargs=None)
    raise Exception("Don't know how to ast_repr this: ", x)
예제 #13
0
def value2pyliteral(val):  # noqa: C901
    """
    Transforms a python value into a literal in AST form.
    """
    if val is None:
        return ast.NameConstant(value=None)
    elif val is ...:
        return ast.Ellipsis()
    elif val is True:
        return ast.NameConstant(value=True)
    elif val is False:
        return ast.NameConstant(value=False)
    elif isinstance(val, int):
        return ast.Num(n=val)
    elif isinstance(val, float):
        return ast.Num(n=val)
    elif isinstance(val, str):
        return ast.Str(s=val)
    elif isinstance(val, bytes):
        return ast.Bytes(s=val)
    elif isinstance(val, tuple):
        return ast.Tuple(elts=[value2pyliteral(item) for item in val])
    elif isinstance(val, list):
        return ast.List(elts=[value2pyliteral(item) for item in val])
    elif isinstance(val, dict):
        return ast.Dict(
            # .keys() and .values() have been documented to return things in the
            # same order since Py2
            keys=[value2pyliteral(item) for item in val.keys()],
            values=[value2pyliteral(item) for item in val.values()],
        )
    elif isinstance(val, set):
        return ast.Set(elts=[value2pyliteral(item) for item in val])
    else:
        raise ValueError(f"Can't translate {val!r} into a literal")
예제 #14
0
    def visit_Assign(self, node):
        print("  in MyTransformer.visit_Assign()")
        # print("   targets =", node.targets[0].id)
        # print("   value   =", node.value)

        # create new node | Assign(expr* targets, expr value)
        new_node = node

        # assign random new value to this variable
        rand_num = random.randint(0, 8)

        new_value_dict = {
            0:
            ast.Num(0),
            1:
            ast.Str("random \x00 string"),
            2:
            ast.NameConstant(False),
            3:
            ast.NameConstant(True),
            4:
            ast.List(elts=[
                ast.Num(0),
                ast.Num(1),
                ast.Str("2"),
                ast.Num(3),
                ast.Num(999999)
            ],
                     ctx=ast.Load()),
            # print("how did this happen?")
            5:
            ast.Call(func=ast.Name(id='print', ctx=ast.Load()),
                     args=[ast.Str("how did this happen?")],
                     keywords=[]),
            # infinity
            6:
            ast.Call(func=ast.Name(id='float', ctx=ast.Load()),
                     args=[ast.Str("inf")],
                     keywords=[]),
            # -infinity
            7:
            ast.Call(func=ast.Name(id='float', ctx=ast.Load()),
                     args=[ast.Str("-inf")],
                     keywords=[]),
            # random dictionary
            8:
            ast.Dict(keys=[ast.Num(0), ast.Num(1),
                           ast.Str("key3")],
                     values=[ast.Str("Zero"),
                             ast.Str("One"),
                             ast.Num(3)])
        }

        new_value = new_value_dict[rand_num]
        print("   new_value =", new_value)

        new_node.value = new_value
        ast.copy_location(new_node, node)
        ast.fix_missing_locations(new_node)
        return new_node
예제 #15
0
    def visit_Assign(self, stmt):
        if self._is_vararg(stmt.targets[0]):
            # x = dict(foo='bar', ...)
            if isinstance(stmt.value, ast.Call) and \
               isinstance(stmt.value.func, ast.Name) and \
               stmt.value.func.id == 'dict':
                keys, vals = unzip([(kw.arg, kw.value)
                                    for kw in stmt.value.keywords])
                stmt.value = ast.Dict([ast.Str(k) for k in keys], vals)

                self.change = True
                return self.visit(stmt)

            # x = {'foo': 'bar', ...}
            elif isinstance(stmt.value, ast.Dict) and len(stmt.value.keys) > 0:
                name = stmt.targets[0].id
                new_stmts = [parse_stmt(f'{name} = {{}}')]
                for k, v in zip(stmt.value.keys, stmt.value.values):
                    assgn = parse_stmt(f'{name}[_] = _')
                    assgn.targets[0].slice.value = k
                    assgn.value = v
                    new_stmts.append(assgn)

                self.change = True
                return [self.visit(stmt) for stmt in new_stmts]

        self.generic_visit(stmt)
        return stmt
def _param_ast(param: ServiceParameter) -> ast.arg:
    """Create the ast node for the parameter of a function.

    This can be typed.

    """
    if param.name.startswith("/"):
        annotation = ast.Dict(keys=None, values=None)

        annotation = ast.Subscript(
            value=ast.Attribute(value=ast.Name(id="typing"), attr="Dict"),
            slice=ast.Index(value=ast.Tuple(
                elts=[ast.Name(
                    id="str"), ast.Name(id="str")])),
        )

        return ast.arg(
            arg=snakeit(param.extra_data["(placeholderParam)"]["paramName"]),
            annotation=annotation,
        )

    annotation = None
    if param.type == "string":
        if param.multiple:
            annotation = ast.Name(id="OptionalListStr")
        else:
            annotation = ast.Name(id="str")
    elif param.type == "number":
        annotation = ast.Name(id="int")
    elif param.type == "boolean":
        annotation = ast.Name(id="bool")
    elif param.type == "file":
        annotation = ast.Name(id="typing.BinaryIO")
    return ast.arg(arg=snakeit(param.name), annotation=annotation)
예제 #17
0
    def dict_word_visitor(node: concat.parse.DictWordNode):
        """Converts a DictWordNode to a Python expression.

        The expression looks like
        `push({(Quotation([...1])(stack,stash),stack.pop())[-1]:(Quotation([...2])(stack,stash),stack.pop())[-1],......})`."""
        load = ast.Load()
        pairs = []
        for key, value in node.dict_children:
            key_quote = to_transpiled_quotation(key, node.location, visitors)
            value_quote = to_transpiled_quotation(
                value, node.location, visitors
            )
            stack = ast.Name(id='stack', ctx=load)
            stash = ast.Name(id='stash', ctx=load)
            key_quote_call = ast.Call(
                func=key_quote, args=[stack, stash], keywords=[]
            )
            value_quote_call = ast.Call(
                func=value_quote, args=[stack, stash], keywords=[]
            )
            py_key = pack_expressions([key_quote_call, pop_stack()])
            py_value = pack_expressions([value_quote_call, pop_stack()])
            pairs.append((py_key, py_value))
        dictionary = ast.Dict(
            keys=[*dict(pairs)], values=[*dict(pairs).values()]
        )
        push_func = ast.Name(id='push', ctx=load)
        py_node = ast.Call(func=push_func, args=[dictionary], keywords=[])
        py_node.lineno, py_node.col_offset = node.location
        return py_node
    def _create_discriminator_field(self, type_obj):
        items = {}
        for child in type_obj.get_all_children():
            items[
                child.
                discriminator_value] = f"commercetools.schemas.{child.package_name}.{child.name}Schema"

        field = type_obj.get_discriminator_field()
        self.generator.add_import_statement(self.resource.package_name,
                                            "commercetools", "helpers")

        return ast.Call(
            func=ast.Name(id="helpers.Discriminator"),
            args=[],
            keywords=[
                ast.keyword(
                    arg="discriminator_field",
                    value=ast.Tuple(elts=[
                        ast.Str(s=field.name),
                        ast.Str(s=field.attribute_name)
                    ]),
                ),
                ast.keyword(
                    arg="discriminator_schemas",
                    value=ast.Dict(
                        keys=[ast.Str(s=v) for v in items.keys()],
                        values=[ast.Str(s=v) for v in items.values()],
                    ),
                ),
                ast.keyword(arg="unknown",
                            value=ast.Name(id="marshmallow.EXCLUDE")),
                ast.keyword(arg="allow_none", value=ast.NameConstant(True)),
            ],
        )
예제 #19
0
def obj_to_ast(obj):
    if isinstance(obj, tuple):
        return ast.Tuple(elts=tuple(map(obj_to_ast, obj)))
    elif isinstance(obj, dict):
        k, v = unzip([(obj_to_ast(k), obj_to_ast(v)) for k, v in obj.items()])
        return ast.Dict(k, v)
    elif isinstance(obj, list):
        return ast.List(list(map(obj_to_ast, obj)))
    elif isinstance(obj, type):
        return ast.Name(id=obj.__name__)
    elif isinstance(obj, int):
        return ast.Num(obj)
    elif isinstance(obj, str):
        return ast.Str(obj)
    elif obj is None:
        return ast.NameConstant(None)
    elif isinstance(obj, (typing._GenericAlias, typing._SpecialForm)):
        # TODO: types
        # issue was in pandas, where importing pandas._typing.Axis would
        # resolve module to typing, attempt to do "from typing import Axis"
        return ast.NameConstant(None)
    elif isinstance(obj, float) and math.isinf(obj):
        return parse_expr('float("inf")')
    elif isinstance(obj, bytes):
        return ast.Bytes(s=obj)
    else:
        raise ObjConversionException(f"No converter for {obj}")
예제 #20
0
 def astify_json_obj(obj):
     obj = maybe_str(obj)
     if isinstance(obj, str):
         return ast.Str(obj)
     elif obj is None:
         return ast.Name(obj, ast.Load())
     elif isinstance(obj, bool):
         return ast.Name(obj, ast.Load())
     elif isinstance(obj, int):
         return ast.Name(obj, ast.Load())
     elif isinstance(obj, float):
         return ast.Name(obj, ast.Load())
     elif isinstance(obj, list):
         json_values = []
         for v in obj:
             json_values.append(astify_json_obj(v))
         return ast.List(json_values, ast.Load())
     elif isinstance(obj, dict):
         json_values = []
         json_keys = []
         for k, v in obj.items():
             json_keys.append(ast.Str(maybe_str(k)))
             json_values.append(astify_json_obj(v))
         return ast.Dict(json_keys, json_values)
     else:
         raise Exception('Cannot astify {0:s}'.format(str(obj)))
예제 #21
0
def size_container_folding(value):
    """
    Convert value to ast expression if size is not too big.

    Converter for sized container.
    """
    if len(value) < MAX_LEN:
        if isinstance(value, list):
            return ast.List(map(to_ast, value), ast.Load())
        elif isinstance(value, tuple):
            return ast.Tuple(map(to_ast, value), ast.Load())
        elif isinstance(value, set):
            return ast.Set(map(to_ast, value))
        elif isinstance(value, dict):
            keys = map(to_ast, value.iterkeys())
            values = map(to_ast, value.itervalues())
            return ast.Dict(keys, values)
        elif isinstance(value, numpy.ndarray):
            return ast.Call(func=ast.Attribute(ast.Name('numpy', ast.Load()),
                                               'array', ast.Load()),
                            args=[to_ast(value.tolist())],
                            keywords=[],
                            starargs=None,
                            kwargs=None)
        else:
            raise ConversionError()
    else:
        raise ToNotEval()
예제 #22
0
    def __init__(self,
                 signature,
                 func,
                 args,
                 keywords=None,
                 py_func=None,
                 **kw):
        if py_func and not kw.get('name', None):
            kw['name'] = py_func.__name__
        if signature is None:
            signature = minitypes.FunctionType(return_type=object_,
                                               args=[object_] * len(args))
            if keywords:
                signature.args.extend([object_] * len(keywords))

        super(ObjectCallNode, self).__init__(signature, args)
        assert func is not None
        self.function = func
        self.py_func = py_func

        self.args_tuple = ast.Tuple(elts=args, ctx=ast.Load())
        self.args_tuple.variable = Variable(
            numba_types.TupleType(size=len(args)))

        if keywords:
            keywords = [(ConstNode(k.arg), k.value) for k in keywords]
            keys, values = zip(*keywords)
            self.kwargs_dict = ast.Dict(list(keys), list(values))
            self.kwargs_dict.variable = Variable(minitypes.object_)
        else:
            self.kwargs_dict = NULL_obj

        self.type = signature.return_type
예제 #23
0
 def _init_non_primitive_nested_class(node_assign, object_, prop):
     """
     If the nested list is non-primitive, initialise sub-classes in a list comp
     If the nest is primitive, we can simply get it
     Marshmallow will do the type marshalling
     """
     return ast.ListComp(
         elt=ast.Call(
             func=ast.Name(id=ObjectGenerator._nesting_class(node_assign)),
             args=[ast.Name(id="el")],
             keywords=[],
         ),
         generators=[
             ast.comprehension(
                 target=ast.Name(id="el"),
                 iter=ast.Call(
                     func=ast.Attribute(value=ast.Name(id=object_),
                                        attr="get"),
                     args=[ast.Str(s=prop),
                           ast.Dict(keys=[], values=[])],
                     keywords=[],
                 ),
                 ifs=[],
                 is_async=0,
             )
         ],
     )
예제 #24
0
def _new_constant(node, value):
    if isinstance(value, ast.AST):
        # convenient shortcut: return the AST object unchanged
        return value

    # FIXME: test the config directly here?

    if value is None:
        new_node = ast.Constant(value=None)
    elif isinstance(value, (bool, int, float, complex, str, bytes)):
        new_node = ast.Constant(value=value)
    elif isinstance(value, (tuple, frozenset)):
        if not _is_constant(value):
            raise TypeError("container items are not constant")
        new_node = ast.Constant(value=value)
    elif isinstance(value, list):
        elts = [_new_constant(node, elt) for elt in value]
        new_node = ast.List(elts=elts, ctx=ast.Load())
    elif isinstance(value, dict):
        keys = []
        values = []
        for key, value in value.items():
            keys.append(_new_constant(node, key))
            values.append(_new_constant(node, value))
        new_node = ast.Dict(keys=keys, values=values, ctx=ast.Load())
    elif isinstance(value, set):
        elts = [_new_constant(node, elt) for elt in value]
        new_node = ast.Set(elts=elts, ctx=ast.Load())
    else:
        raise TypeError("unknown type: %s" % type(value).__name__)

    copy_lineno(node, new_node)
    return new_node
예제 #25
0
 def add_aux_db_engine(self, bind_name, backend_version):
     for dbe in self.find_aux_db_engine_discovery_map():
         dict_keys = [ast.Str(s='backend_version')]
         dict_values = [ast.Str(s=backend_version)]
         _dict = ast.Dict(keys=dict_keys, values=dict_values)
         dbe.value.keys.append(ast.Str(s=bind_name))
         dbe.value.values.append(_dict)
예제 #26
0
    def visit_ClassDef(self, node: ast.ClassDef) -> Any:
        """
        visit class def
        """
        clz = 'Main'
        base_clz = 'ViewFlow'
        func_name = 'create_nodes'
        self.clz = self.clz_as if self.clz_as else self.clz
        if node.name == clz and node.bases[0].id == base_clz:
            for x in node.body:
                if type(x).__name__ == FUNCTION_DEF and x.name == func_name:
                    for y in x.body:
                        if type(y).__name__ == RETURN:
                            # check the node ID for duplicates
                            for z in y.value.elts:
                                # valid node
                                self.valid_node(z)
                                # update node
                                if z.values[1].s == self.node_id:
                                    raise CommandError(
                                        'Run failed, duplicate node id')

                            dict_node = ast.Dict(keys=[
                                ast.Str(s='cls'),
                                ast.Str(s='id'),
                                ast.Str(s='next'),
                                ast.Num(n='x'),
                                ast.Num(n='y')
                            ],
                                                 values=[
                                                     ast.Name(id=self.clz,
                                                              ctx=ast.Load()),
                                                     ast.Str(s=self.node_id),
                                                     self.process_NameConstant(
                                                         self.next_node_id),
                                                     ast.Num(n=self.coord_x),
                                                     ast.Num(n=self.coord_y),
                                                 ])

                            # try update relate node info
                            for idx, item in enumerate(y.value.elts):
                                try:
                                    if item.values[2].s and item.values[
                                            2].s == self.next_node_id:
                                        item.values[2].s = self.node_id
                                except AttributeError:
                                    continue

                            # insert target node info
                            if self.next_node_id:
                                for idx, item in enumerate(y.value.elts):
                                    if self.next_node_id == item.values[1].s:
                                        y.value.elts.insert(idx, dict_node)
                                        break
                            else:
                                y.value.elts.append(dict_node)
                            break
                    break

        return node
 def build_local_args(self):
     """
     build a local var named args that will be given as input to the command.
     """
     keys, values = self.get_keys_values(self.input_args[0])
     self.args.append(
         ast_mod.Assign(targets=[ast_name('args', ctx=ast_mod.Store())],
                        value=ast_mod.Dict(keys=keys, values=values)))
    def visit_Dict(self, node: Dict, *args, **kwargs) -> C.Dict:
        keys = self.visit(node.keys, *args, **kwargs)
        values = self.visit(node.values, *args, **kwargs)

        return C.Dict(
            keys=keys,
            values=values,
        )
def make_dict(cursor_trail, tree):
    """Dict(expr* keys, expr* values)
    """
    selected_node = core_logic.get_node_at_cursor(cursor_trail, tree)
    expr = selected_node if isinstance(selected_node,
                                       ast.expr) else make_expression()

    return ast.Dict(keys=[ast.Name(id="key", ctx=ast.Load())], values=[expr])
예제 #30
0
    def _to_ast_keyword(self, hps):
        """Returns an ``ast.keyword`` for the ``hyperparameters`` kwarg if there are any."""
        if hps:
            keys, values = zip(*six.iteritems(hps))
            return ast.keyword(arg="hyperparameters",
                               value=ast.Dict(keys=keys, values=values))

        return None