Example #1
0
    def _decode_refs(self, data, namespace):
        """``data`` is {} components in json expression from the inner most"""

        if not _is_mxdata(data):
            return data

        module = data["__module"]
        type_ = data["__type"]
        encode = data["__encoding"]
        value = data["__value"]

        if encode == "None":
            return None
        if encode == "Sequence":
            return self._get_type(type_)(value)
        elif encode == "Mapping":
            type_ = self._get_type(type_)
            return type_({key: value for key, value in value})
        elif encode == "Interface":
            return _RefData(evalrepr=rel_to_abs(value, namespace))
        elif encode == "Module":
            return importlib.import_module(value)
        elif encode == "Value":
            return self._get_type(type_)(value)
        else:
            raise RuntimeError("must not happen")
Example #2
0
    def get_instruction(self):

        if self.target == "_formula":
            # lambda formula definition
            method = "set_formula"
            val = self.atok.get_text(self.node.value)
            if val == "None":
                val = None

            kwargs = {"formula": val}
            return Instruction.from_method(
                    obj=self.impl,
                    method=method,
                    kwargs=kwargs
                )

        elif self.target == "_bases":

            bases = [
                rel_to_abs(base, self.obj.parent.fullname)
                for base in ast.literal_eval(
                    self.atok.get_text(self.node.value))
            ]

            def bases_hook(inst):
                args = [mx.get_object(base) for base in inst.args]
                return args, inst.kwargs

            return Instruction.from_method(
                obj=self.obj,
                method="add_bases",
                args=bases,
                arghook=bases_hook)

        elif self.target == "_spaces":
            self.reader.result = json.loads(
                self.atok.get_text(self.node.value)
            )
            return

        elif self.target == "_allow_none":
            value = ast.literal_eval(self.atok.get_text(self.node.value))
            return Instruction.from_method(
                obj=self.obj,
                method="set_property",
                args=("allow_none", value)
            )

        else:
            raise RuntimeError("unknown attribute assignment")
Example #3
0
 def decode(self):
     return rel_to_abs(self.elm(1), self.obj.fullname)
Example #4
0
        def parse_stmt(node):
            """Return (list of) instructions"""
            if isinstance(node, ast.FunctionDef):
                if node.name == "_formula":
                    method = "set_formula"
                else:
                    method = "new_cells"

                funcdef = atok.get_text(node)

                # The code below is just for adding back comment in the last line
                # such as:
                # def foo():
                #     return 0  # Comment
                nxtok = node.last_token.index + 1
                if nxtok < len(atok.tokens) and (
                    atok.tokens[nxtok].type == tokenize.COMMENT
                )and node.last_token.line == atok.tokens[nxtok].line:
                    deflines = funcdef.splitlines()
                    deflines.pop()
                    deflines.append(node.last_token.line.rstrip())
                    funcdef = "\n".join(deflines)

                return [_Instruction(
                    path_=path_,
                    obj=impl,
                    method=method,
                    kwargs={"formula": funcdef}
                )]

            if isinstance(node, ast.Assign):

                if node.first_token.string == "_name":
                    method = "rename"
                    if "name" in self.kwargs and self.kwargs["name"]:
                        val = self.kwargs["name"]
                    else:
                        val = ast.literal_eval(atok.get_text(node.value))
                    _Instruction(
                        path_=path_,
                        obj=obj,
                        method=method,
                        args=(val,),
                        kwargs={"rename_old": True}).run()
                    return []

                elif node.first_token.string == "_formula":
                    # lambda formula definition
                    method = "set_formula"
                    val = atok.get_text(node.value)
                    if val == "None":
                        val = None
                    kwargs = {"formula": val}
                    return [
                        _Instruction(
                            path_=path_,
                            obj=impl,
                            method=method,
                            kwargs=kwargs)
                    ]

                elif node.first_token.string == "_refs":

                    def bound_decode_refs(data):
                        return self._decode_refs(data, obj.fullname)

                    refs = json.loads(
                        atok.get_text(node.value),
                        object_hook=bound_decode_refs
                    )

                    result = []
                    for key, val in refs.items():
                        result.append(_Instruction(
                            path_=path_,
                            obj=obj,
                            method="__setattr__",
                            args=(key, val),
                            arghook=refhook
                        ))
                    return result

                elif node.first_token.string == "_bases":

                    bases = [
                        _RefData(rel_to_abs(base, obj.fullname)) for base in
                        ast.literal_eval(atok.get_text(node.value))
                    ]

                    return [_Instruction(
                        path_=path_,
                        obj=obj,
                        method="add_bases",
                        args=bases,
                        arghook=basehook)]

                elif node.first_token.string == "_method":

                    _method = json.loads(
                        atok.get_text(node.value)
                    )
                    return [_Instruction(
                        path_=path_,
                        obj=impl,
                        method=_method["method"],
                        args=_method["args"],
                        kwargs=_method["kwargs"]
                    )]

                elif node.first_token.string == "_allow_none":
                    args = json.loads(atok.get_text(node.value))
                    return [_Instruction(
                        path_=path_,
                        obj=obj,
                        method="set_property",
                        args=["allow_none", args]
                    )]

                else:
                    # lambda cells definition
                    return [_Instruction(
                        path_=path_,
                        obj=impl,
                        method="new_cells",
                        kwargs={
                            "name": atok.get_text(node.targets[0]),
                            "formula": atok.get_text(node.value)
                        }
                    )]