Exemple #1
0
    def __init__(self, protocol, strict=None, include_as=None):
        """Initialize the compiler.

        :param thriftrw.protocol.Protocol protocol:
           The protocol ot use to serialize and deserialize values.
        """
        if strict is None:
            strict = True
        if include_as is None:
            include_as = False
        self.protocol = protocol
        self.strict = strict
        self.include_as = include_as

        self.parser = Parser()

        # Mapping from absolute file path to ModuleSpec for all modules.
        self._module_specs = {}
Exemple #2
0
    def __init__(self, protocol, strict=True):
        """Initialize the compiler.

        :param thriftrw.protocol.Protocol protocol:
           The protocol ot use to serialize and deserialize values.
        """
        self.protocol = protocol
        self.strict = strict

        self.parser = Parser()

        # Mapping from absolute file path to ModuleSpec for all modules.
        self._module_specs = {}
Exemple #3
0
    def __init__(self, protocol, strict=None, include_as=None):
        """Initialize the compiler.

        :param thriftrw.protocol.Protocol protocol:
           The protocol ot use to serialize and deserialize values.
        """
        if strict is None:
            strict = True
        if include_as is None:
            include_as = False
        self.protocol = protocol
        self.strict = strict
        self.include_as = include_as

        self.parser = Parser()

        # Mapping from absolute file path to ModuleSpec for all modules.
        self._module_specs = {}
Exemple #4
0
def parse():
    """Parser for enum definitions."""
    return Parser(start='enum', silent=True).parse
Exemple #5
0
from thriftrw.spec.list import ListTypeSpec
from thriftrw.spec.map import MapTypeSpec
from thriftrw.spec.struct import StructTypeSpec
from thriftrw.spec.set import SetTypeSpec
from thriftrw.idl import Parser
from thriftrw.compile.scope import Scope

parse_struct = Parser(start='struct', silent=True).parse


def sstruct(fields=""):
    """This helper creates a mock spec for sstruct """
    struct_ast = parse_struct('struct RefStruct { %s }' % fields)
    spec = StructTypeSpec.compile(struct_ast)
    spec.link(Scope("test"))
    return spec


def smap(key, value):
    return MapTypeSpec(key, value)


def sset(value):
    return SetTypeSpec(value)


def slist(value):
    return ListTypeSpec(value)
Exemple #6
0
class Compiler(object):
    """Compiles IDLs into Python modules."""

    __slots__ = (
        'protocol', 'strict', 'parser', 'include_as', '_module_specs'
    )

    def __init__(self, protocol, strict=None, include_as=None):
        """Initialize the compiler.

        :param thriftrw.protocol.Protocol protocol:
           The protocol ot use to serialize and deserialize values.
        """
        if strict is None:
            strict = True
        if include_as is None:
            include_as = False
        self.protocol = protocol
        self.strict = strict
        self.include_as = include_as

        self.parser = Parser()

        # Mapping from absolute file path to ModuleSpec for all modules.
        self._module_specs = {}

    def compile(self, name, contents, path=None):
        """Compile the given Thrift document into a Python module.

        The generated module contains,

        .. py:attribute:: __services__

            A collection of generated classes for all services defined in the
            thrift file.

            .. versionchanged:: 1.0

                Renamed from ``services`` to ``__services__``.

        .. py:attribute:: __types__

            A collection of generated types for all types defined in the
            thrift file.

            .. versionchanged:: 1.0

                Renamed from ``types`` to ``__types__``.

        .. py:attribute:: __includes__

            A collection of modules included by this module.

            .. versionadded:: 1.0

        .. py:attribute:: __constants__

            A mapping of constant name to value for all constants defined in
            the thrift file.

            .. versionchanged:: 1.0

                Renamed from ``constants`` to ``__constants__``.

        .. py:attribute:: __thrift_source__

            Contents of the .thrift file from which this module was compiled.

            .. versionadded:: 1.1

        .. py:function:: dumps(obj)

            Serializes the given object using the protocol the compiler was
            instantiated with.

        .. py:function:: loads(cls, payload)

            Deserializes an object of type ``cls`` from ``payload`` using the
            protocol the compiler was instantiated with.

        .. py:function:: dumps.message(obj, seqid=0)

            Serializes the given request or response into a
            :py:class:`~thriftrw.wire.Message` using the protocol that the
            compiler was instantiated with.

            See :ref:`calling-apache-thrift`.

            .. versionadded:: 1.0

        .. py:function:: loads.message(service, payload)

            Deserializes a :py:class:`~thriftrw.wire.Message`  from
            ``payload`` using the protocol the compiler was instantiated with.
            A request or response of a method defined in the given service is
            parsed in the message body.

            See :ref:`calling-apache-thrift`.

            .. versionadded:: 1.0

        And one class each for every struct, union, exception, enum, and
        service defined in the IDL.

        Service classes have references to
        :py:class:`thriftrw.spec.ServiceFunction` objects for each method
        defined in the service.

        :param str name:
            Name of the Thrift document. This will be the name of the
            generated module.
        :param str contents:
            Thrift document to compile
        :param str path:
            Path to the Thrift file being compiled. If not specified, imports
            from within the Thrift file will be disallowed.
        :returns:
            ModuleSpec of the generated module.
        """
        assert name

        if path:
            path = os.path.abspath(path)
            if path in self._module_specs:
                return self._module_specs[path]

        module_spec = ModuleSpec(name, self.protocol, path, contents)
        if path:
            self._module_specs[path] = module_spec

        program = self.parser.parse(contents)

        header_processor = HeaderProcessor(self, module_spec, self.include_as)
        for header in program.headers:
            header.apply(header_processor)

        generator = Generator(module_spec.scope, strict=self.strict)
        for definition in program.definitions:
            generator.process(definition)

        return module_spec
Exemple #7
0
def parse():
    return Parser(start='struct', silent=True).parse
def parse():
    return Parser(start='map_type', silent=True).parse
def parse():
    return Parser().parse
def parse():
    return Parser(start='service', silent=True).parse
Exemple #11
0
def parse():
    return Parser(start='union', silent=True).parse
Exemple #12
0
def parse():
    return Parser(start='exception', silent=True).parse
Exemple #13
0
class Compiler(object):
    """Compiles IDLs into Python modules."""

    __slots__ = ('protocol', 'strict', 'parser', 'include_as', '_module_specs')

    def __init__(self, protocol, strict=None, include_as=None):
        """Initialize the compiler.

        :param thriftrw.protocol.Protocol protocol:
           The protocol ot use to serialize and deserialize values.
        """
        if strict is None:
            strict = True
        if include_as is None:
            include_as = False
        self.protocol = protocol
        self.strict = strict
        self.include_as = include_as

        self.parser = Parser()

        # Mapping from absolute file path to ModuleSpec for all modules.
        self._module_specs = {}

    def compile(self, name, contents, path=None):
        """Compile the given Thrift document into a Python module.

        The generated module contains,

        .. py:attribute:: __services__

            A collection of generated classes for all services defined in the
            thrift file.

            .. versionchanged:: 1.0

                Renamed from ``services`` to ``__services__``.

        .. py:attribute:: __types__

            A collection of generated types for all types defined in the
            thrift file.

            .. versionchanged:: 1.0

                Renamed from ``types`` to ``__types__``.

        .. py:attribute:: __includes__

            A collection of modules included by this module.

            .. versionadded:: 1.0

        .. py:attribute:: __constants__

            A mapping of constant name to value for all constants defined in
            the thrift file.

            .. versionchanged:: 1.0

                Renamed from ``constants`` to ``__constants__``.

        .. py:attribute:: __thrift_source__

            Contents of the .thrift file from which this module was compiled.

            .. versionadded:: 1.1

        .. py:function:: dumps(obj)

            Serializes the given object using the protocol the compiler was
            instantiated with.

        .. py:function:: loads(cls, payload)

            Deserializes an object of type ``cls`` from ``payload`` using the
            protocol the compiler was instantiated with.

        .. py:function:: dumps.message(obj, seqid=0)

            Serializes the given request or response into a
            :py:class:`~thriftrw.wire.Message` using the protocol that the
            compiler was instantiated with.

            See :ref:`calling-apache-thrift`.

            .. versionadded:: 1.0

        .. py:function:: loads.message(service, payload)

            Deserializes a :py:class:`~thriftrw.wire.Message`  from
            ``payload`` using the protocol the compiler was instantiated with.
            A request or response of a method defined in the given service is
            parsed in the message body.

            See :ref:`calling-apache-thrift`.

            .. versionadded:: 1.0

        And one class each for every struct, union, exception, enum, and
        service defined in the IDL.

        Service classes have references to
        :py:class:`thriftrw.spec.ServiceFunction` objects for each method
        defined in the service.

        :param str name:
            Name of the Thrift document. This will be the name of the
            generated module.
        :param str contents:
            Thrift document to compile
        :param str path:
            Path to the Thrift file being compiled. If not specified, imports
            from within the Thrift file will be disallowed.
        :returns:
            ModuleSpec of the generated module.
        """
        assert name

        if path:
            path = os.path.abspath(path)
            if path in self._module_specs:
                return self._module_specs[path]

        module_spec = ModuleSpec(name, self.protocol, path, contents)
        if path:
            self._module_specs[path] = module_spec

        program = self.parser.parse(contents)

        header_processor = HeaderProcessor(self, module_spec, self.include_as)
        for header in program.headers:
            header.apply(header_processor)

        generator = Generator(module_spec.scope, strict=self.strict)
        for definition in program.definitions:
            generator.process(definition)

        return module_spec