API Reference¶
thriftrw¶
-
thriftrw.load(self, path, name=None)¶ Parses and compiles the given Thrift file.
Uses the binary protocol to serialize and deserialize values.
For more advanced use, see
thriftrw.loader.Loader.Parameters: - path (str) – Path to the
.thriftfile. - name (str) – Name of the generated module. Defaults to the basename of the file.
- force (bool) – Whether to ignore the cache and load the file anew. Defaults to False.
Raises: thriftrw.errors.ThriftCompilerError – If there was an error compiling the Thrift file.
- path (str) – Path to the
-
thriftrw.install(path, name=None)¶ Compiles a Thrift file and installs it as a submodule of the caller.
Given a tree organized like so:
foo/ __init__.py bar.py my_service.thrift
You would do,
my_service = thriftrw.install('my_service.thrift')
To install
my_serviceas a submodule of the module from which you made the call. If the call was made infoo/bar.py, the compiled Thrift file will be installed asfoo.bar.my_service. If the call was made infoo/__init__.py, the compiled Thrift file will be installed asfoo.my_service. This allows other modules to importfromthe compiled module like so,from foo.my_service import MyService
New in version 0.2.
Parameters: - path – Path of the Thrift file. This may be an absolute path, or a path relative to the Python module making the call.
- name (str) – Name of the submodule. Defaults to the basename of the Thrift file.
Returns: The compiled module
thriftrw.loader¶
-
class
thriftrw.loader.Loader(protocol=None, strict=None, include_as=None)¶ Loads and compiles Thrift files.
-
load(path, name=None)¶ Load and compile the given Thrift file.
Parameters: - path (str) – Path to the
.thriftfile. - name (str) – Name of the generated module. Defaults to the base name of the file.
Returns: The compiled module.
- path (str) – Path to the
-
loads(name, document)¶ Parse and compile the given Thrift document.
Parameters: - name (str) – Name of the Thrift document.
- document (str) – The Thrift IDL as a string.
-
thriftrw.compile¶
-
class
thriftrw.compile.Compiler(protocol, strict=None, include_as=None)¶ Compiles IDLs into Python modules.
-
compile(name, contents, path=None)¶ Compile the given Thrift document into a Python module.
The generated module contains,
-
__services__¶ A collection of generated classes for all services defined in the thrift file.
Changed in version 1.0: Renamed from
servicesto__services__.
-
__types__¶ A collection of generated types for all types defined in the thrift file.
Changed in version 1.0: Renamed from
typesto__types__.
-
__includes__¶ A collection of modules included by this module.
New in version 1.0.
-
__constants__¶ A mapping of constant name to value for all constants defined in the thrift file.
Changed in version 1.0: Renamed from
constantsto__constants__.
-
__thrift_source__¶ Contents of the .thrift file from which this module was compiled.
New in version 1.1.
-
dumps(obj)¶ Serializes the given object using the protocol the compiler was instantiated with.
-
loads(cls, payload)¶ Deserializes an object of type
clsfrompayloadusing the protocol the compiler was instantiated with.
-
dumps.message(obj, seqid=0)¶ Serializes the given request or response into a
Messageusing the protocol that the compiler was instantiated with.New in version 1.0.
-
loads.message(service, payload)¶ Deserializes a
Messagefrompayloadusing 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.New in version 1.0.
And one class each for every struct, union, exception, enum, and service defined in the IDL.
Service classes have references to
thriftrw.spec.ServiceFunctionobjects for each method defined in the service.Parameters: - name (str) – Name of the Thrift document. This will be the name of the generated module.
- contents (str) – Thrift document to compile
- path (str) – 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.
-
-
thriftrw.protocol¶
Protocols¶
-
class
thriftrw.protocol.Protocol¶ Base class for all protocol implementations.
Changed in version 1.0: Removed
dumpsandloadsmethods and addedserialize_messageanddeserialize_message.-
deserialize_message(self, bytes s) → Message¶ Deserialize a
Message.Parameters: s – Bytes to decode Returns: Parsed Messagecontaining aValuein its body.
-
deserialize_value(self, int typ, bytes s) → Value¶ Parse a
Valueof the given type.Parameters: - typ – Type code of the value to parse.
- s – Bytes to decode.
Returns: Parsed
Value.Raises: thriftrw.errors.ThriftProtocolError – If the object failed to deserialize.
-
reader(self, ReadBuffer buff) → ProtocolReader¶
-
serialize_message(self, Message message) → bytes¶ Serialize a
Message.The message body must be a
Value.Parameters: message – Message to serialize. Returns: Serialized message.
-
serialize_value(self, Value value) → bytes¶ Serialize the given
Value.Parameters: value (Value) – Value to serialize. Returns: Serialized value.
-
writer(self, WriteBuffer buff) → ProtocolWriter¶
-
thriftrw.spec¶
Specs¶
Specs represent compiled ASTs and act as the specification of behavior for
the types or values they represent. All specs have a name and may have a
surface attribute which, if not None, is exposed in the generated module
at the top-level.
-
class
thriftrw.spec.TypeSpec¶ Base class for classes representing TypeSpecs.
A TypeSpec knows how to convert values of the corresponding type to and from
thriftrw.wire.Valueobjects.-
from_primitive(self, prim_value)¶ Converts a primitive value into a value of this type.
A primitive value is a text, binary, integer, or float value, or a list or dict of other primitive values.
New in version 0.4.
Parameters: prim_value – A primitive value as produced by to_primitive.Returns: A value matching this TypeSpec.
-
from_wire(self, Value wire_value)¶ Converts the given
thriftrw.wire.Valueback into the original type.Parameters: wire_value (thriftr.wire.Value) – Value to convert. Raises: ValueError – If the type of the wire value does not have the correct Thrift type for this type spec.
-
link(self, scope) → TypeSpec¶
-
name¶ Name of the type referenced by this type spec.
-
read_from(self, ProtocolReader reader)¶ Read a primitive value of this type from
thriftrw.protocol.ProtocolReader.
-
surface¶ The surface of a type spec.
The surface of a type spec, if non-None is the value associated with its name at the top-level in the generated module.
-
to_primitive(self, value)¶ Converts a value matching this type spec into a primitive value.
A primitive value is a text, binary, integer, or float value, or a list or dict of other primitive values.
New in version 0.4.
Parameters: value – Value matching this TypeSpec. Returns: A representation of that value using only primitive types, lists, and maps.
-
to_wire(self, value) → Value¶ Converts the given value into a
thriftrw.wire.Valueobject.Returns thriftrw.wire.Value: Wire representation of the value.
-
ttype_code¶ ttype_code: ‘int8_t’
-
validate(self, o) → void¶ Whether an instance of this spec is valid.
Parameters: instance – An instance of the type described by this spec.
Raises: - TypeError – If the value did not match the type expected by this TypeSpec.
- ValueError – If the value matched the type but did not hold the correct set of acceptable values.
-
write_to(self, ProtocolWriter writer, value) → void¶ Writes a value directly to
thriftrw.protocol.ProtocolWriter.
-
Native types¶
-
thriftrw.spec.BoolTypeSpec¶ TypeSpec for boolean values.
-
thriftrw.spec.ByteTypeSpec¶ TypeSpec for single-byte integers.
-
thriftrw.spec.DoubleTypeSpec¶ TypeSpec for floating point numbers with 64 bits of precision.
-
thriftrw.spec.I16TypeSpec¶ TypeSpec for 16-bit integers.
-
thriftrw.spec.I32TypeSpec¶ TypeSpec for 32-bit integers.
-
thriftrw.spec.I64TypeSpec¶ TypeSpec for 64-bit integers.
-
thriftrw.spec.BinaryTypeSpec¶ TypeSpec for binary blobs.
Changed in version 0.4.0: Automatically coerces text values into binary.
-
thriftrw.spec.TextTypeSpec¶ TypeSpec for unicode data.
Values will be decoded/encoded using UTF-8 encoding before/after being serialized/deserialized.
Changed in version 0.3.1: Allows passing binary values directly.
-
class
thriftrw.spec.ListTypeSpec(vspec)¶ Spec for list types.
-
vspec¶ TypeSpec for the kind of values lists conforming to this spec must contain.
-
from_primitive(self, prim_value)¶
-
from_wire(self, Value wire_value)¶
-
link(self, scope) → TypeSpec¶
-
linked¶ linked: ‘bool’
-
name¶
-
read_from(self, ProtocolReader reader)¶
-
surface¶ alias of
list
-
to_primitive(self, value)¶
-
to_wire(self, value) → Value¶
-
validate(self, instance) → void¶
-
vspec vspec: thriftrw.spec.base.TypeSpec
-
write_to(self, ProtocolWriter writer, value) → void¶
-
-
class
thriftrw.spec.MapTypeSpec(kspec, vspec)¶ Spec for map types.
-
kspec¶ TypeSpec for the kind of keys matching maps must contain.
-
vspec¶ TypeSpec for the kind of values matching maps must contain.
-
from_primitive(self, prim_value)¶
-
from_wire(self, Value wire_value)¶
-
kspec kspec: thriftrw.spec.base.TypeSpec
-
link(self, scope) → TypeSpec¶
-
linked¶ linked: ‘bool’
-
name¶
-
read_from(self, ProtocolReader reader)¶
-
surface¶ alias of
dict
-
to_primitive(self, value)¶
-
to_wire(self, value) → Value¶
-
validate(self, instance) → void¶
-
vspec vspec: thriftrw.spec.base.TypeSpec
-
write_to(self, ProtocolWriter writer, value) → void¶
-
-
class
thriftrw.spec.SetTypeSpec(vspec)¶ Parameters: vspec (TypeSpec) – TypeSpec of values stored in the set. -
from_primitive(self, prim_value)¶
-
from_wire(self, Value wire_value)¶
-
link(self, scope) → TypeSpec¶
-
linked¶ linked: ‘bool’
-
name¶
-
read_from(self, ProtocolReader reader)¶
-
to_primitive(self, value)¶
-
to_wire(self, value) → Value¶
-
validate(self, instance) → void¶
-
vspec¶ vspec: thriftrw.spec.base.TypeSpec
-
write_to(self, ProtocolWriter writer, value) → void¶
-
Custom Types¶
-
class
thriftrw.spec.EnumTypeSpec(name, items)¶ TypeSpec for enum types.
-
name¶ Name of the enum class.
-
items¶ Mapping of enum item names to item values.
-
values_to_names¶ Mapping of enum item values to a list of enum item names with that value.
Changed in version 1.1: Changed to a list of names.
-
surface¶ The surface for this spec.
The surface for enum types is a class with the following:
-
type_spec¶ EnumTypeSpecfor the type.
-
items A tuple of the names of all items in this enum.
-
values¶ A tuple of the values of all items in this enum in the same order as
items.
-
classmethod
name_of(value)¶ Finds the name of an enum item by its value. If multiple enum items have this value, any of the matching item names could can be returned.
Parameters: value (int) – A value for an item defined in this enum. Returns: Name of the item with that value or None if no such item exists.
And one attribute for each enum item that has the same name as the attribute and points to the value for that item.
Given the definition,:
enum Role { User, Admin }
The generated class is roughly equivalent to,
class Role(object): User = 0 Admin = 1 items = ('User', 'Admin') values = (0, 1) type_spec = # ... def name_of(self, value): # ...
Changed in version 1.1: Added support for multiple enum items with the same value.
-
compile(type cls, enum)¶
-
from_primitive(self, prim_value)¶
-
from_wire(self, Value wire_value)¶
-
link(self, scope) → TypeSpec¶
-
linked¶ linked: ‘bool’
-
read_from(self, ProtocolReader reader)¶
-
surface surface: object
-
to_primitive(self, value)¶
-
to_wire(self, value) → Value¶
-
validate(self, instance) → void¶
-
write_to(self, ProtocolWriter writer, value) → void¶
-
-
class
thriftrw.spec.StructTypeSpec(name, fields, base_cls=None)¶ A struct is a collection of named fields.
-
name¶ Name of the struct.
The surface for struct types is a class with the following:
-
type_spec¶ StructTypeSpecfor the type.
-
__init__(self, *args, **kwargs)¶ Accepts all fields of the struct as arguments. Required arguments are placed first and may be specified as positional arguments (in the same order as they appear in the IDL). Optional arguments and required arguments come next. A TypeError will be raised if the required arguments for the struct are not filled with non-None values.
-
to_primitive(self)¶ Converts the struct into a dictionary mapping field names to primitive representation of field values.
Only the following types are used in primitive representations:
bool,bytes,float,str(unicodein Python < 3),int,long,dict,list.New in version 0.4.
-
classmethod
from_primitive(cls, value)¶ Converts a dictionary holding a primitive representation of a value of this type (as returned by
to_primitive) into an instance of this class.New in version 0.4.
And obvious definitions of
__str__and__eq__.Given the definition,:
struct User { 1: required string name 2: optional string email 3: required bool isActive = true }
A class roughly equivalent to the following is generated,
class User(object): __slots__ = ('name', 'email', 'isActive') type_spec = # ... def __init__(name, email=None, isActive=True): # ...
-
compile(type cls, struct, require_requiredness=True)¶
-
from_primitive(self, prim_value)
-
from_wire(self, Value wire_value)¶
-
link(self, scope) → TypeSpec¶
-
linked¶ linked: ‘bool’
-
read_from(self, ProtocolReader reader)¶
-
surface¶ surface: object
-
to_primitive(self, struct)
-
to_wire(self, struct) → Value¶
-
validate(self, instance) → void¶
-
write_to(self, ProtocolWriter writer, struct) → void¶
-
-
class
thriftrw.spec.ExceptionTypeSpec(*args, **kwargs)¶ Spec for
exceptiontypes defined in the Thrift file.This is exactly the same as
thriftrw.spec.StructTypeSpecexcept that the generated class inherits theExceptionclass.
-
class
thriftrw.spec.UnionTypeSpec(name, fields, allow_empty=None)¶ Spec for Thrift unions.
The surface for union types is a class with the following:
-
type_spec¶ UnionTypeSpecfor the type.
-
__init__(self, *args, **kwargs)¶ Accepts all fields of the unions as keyword arguments but only one of them is allowed to be non-None. Positional arguments are not accepted.
-
to_primitive(self)¶ Converts the union into a dictionary mapping field names to primitive representation of field values.
Only the following types are used in primitive representations:
bool,bytes,float,str(unicodein Python < 3),int,long,dict,list.New in version 0.4.
-
classmethod
from_primitive(cls, value)¶ Converts a dictionary holding a primitive representation of a value of this type (as returned by
to_primitive) into an instance of this class.New in version 0.4.
And obvious definitions of
__str__and__eq__.Given the definition,:
union Body { 1: string plainText 2: binary richText }
A class roughly equivalent to the following is generated,
class Body(object): __slots__ = ('plainText', 'richText') def __init__(self, plainText=None, richText=None): # Only one of plainText and richText may be non-None. # ...
-
compile(type cls, union)¶
-
from_primitive(self, prim_value)
-
from_wire(self, Value wire_value)¶
-
link(self, scope) → TypeSpec¶
-
linked¶ linked: ‘bool’
-
read_from(self, ProtocolReader reader)¶
-
surface¶ surface: object
-
to_primitive(self, union)
-
to_wire(self, union) → Value¶
-
validate(self, instance) → void¶
-
write_to(self, ProtocolWriter writer, struct) → void¶
-
-
class
thriftrw.spec.FieldSpec(id, name, spec, required, default_value=None)¶ Specification for a single field on a struct.
-
id¶ Field identifier of this field.
-
name¶ Name of the field.
-
required¶ Whether this field is required or not.
-
default_value¶ Default value of the field if any. None otherwise.
-
compile(type cls, field, struct_name, require_requiredness=True)¶
-
default_value default_value: object
-
from_wire(self, wire_value)¶
-
link(self, scope)¶
-
linked¶ linked: ‘bool’
-
spec spec: thriftrw.spec.base.TypeSpec
-
to_wire(self, value)¶
-
ttype_code¶
-
-
class
thriftrw.spec.TypedefTypeSpec(name, target_spec)¶ Typedefs are aliases for other types.
Typedefs resolve themselves to the target type at link time and eliminate themselves from the tree.
-
compile(type cls, typedef)¶
-
link(self, scope) → TypeSpec¶
-
target_spec¶ target_spec: thriftrw.spec.base.TypeSpec
-
Services¶
-
class
thriftrw.spec.ServiceSpec(name, functions, parent)¶ Spec for a single service.
-
name¶ Name of the service.
-
functions¶ Collection of
FunctionSpecobjects.
-
parent¶ ServiceSpecof the parent service or None if this service does not inherit any service.
The
surfacefor aServiceSpecis a class that has the following attributes:-
service_spec¶ ServiceSpecfor this service.
And a reference to one
ServiceFunctionobject for each function defined in the service.-
compile(type cls, service)¶
-
functions functions: list
-
link(self, scope) → ServiceSpec¶
-
linked¶ linked: ‘bool’
-
lookup(self, name)¶ Look up a function with the given name.
Returns the function spec or None if no such function exists.
New in version 1.0.
-
parent parent: object
-
surface¶ surface: object
-
-
class
thriftrw.spec.ServiceFunction¶ Represents a single function on a service.
-
name¶ Name of the function.
-
request¶ Class representing requests for this function.
-
response¶ Class representing responses for this function, or
Noneif this function isoneway.
-
spec¶ thriftrw.spec.FunctionSpecfor this function.
Changed in version 0.3: Added the
specattribute.-
-
class
thriftrw.spec.FunctionSpec(name, args_spec, result_spec, oneway)¶ Specification of a single function on a service.
-
name¶ Name of the function.
-
service¶ The
ServiceSpecthat contains this function. This value is available only after the spec has been linked.New in version 1.1.
-
args_spec¶ FunctionArgsSpecspecifying the arguments accepted by this function as a struct.
-
result_spec¶ FunctionResultSpecspecifying the output of this function as a union of the return type and the exceptions raised by this function.The return type of the function (if any) is a field in the union with field ID 0 and name ‘success’.
This value is None if the function is oneway.
-
oneway¶ Whether this function is oneway or not.
The
surfacefor a FunctionSpec is aServiceFunctionobject. Unlike thesurfacefor other specs, a FunctionSpec’s surface is attached to the service class as a class attribute.Changed in version 0.3: Added the
onewayattribute.-
args_spec args_spec: thriftrw.spec.service.FunctionArgsSpec
-
compile(type cls, func, service_name)¶
-
link(self, scope, ServiceSpec service) → FunctionSpec¶
-
linked¶ linked: ‘bool’
-
result_spec result_spec: thriftrw.spec.service.FunctionResultSpec
-
service service: thriftrw.spec.service.ServiceSpec
-
surface¶ surface: object
-
-
class
thriftrw.spec.FunctionArgsSpec(name, params)¶ Represents the parameters of a service function.
-
function¶ The
FunctionSpecwhose arguments this spec represents. This value is available only after the spec has been linked.
The parameters of a function implicitly form a struct which contains the parameters as its fields, which are optional by default.
The surface for this is the same as
StructTypeSpecexcept that the generated class also includes the following class attributes.-
result_type¶ A reference to the class representing the result type for
function, or None if the function was oneway.
Changed in version 1.0: Added the
functionattribute.Changed in version 1.1: Added
result_typeclass attribute to the surface.-
compile(type cls, parameters, service_name, function_name)¶ Compiles a parameter list into a FunctionArgsSpec.
Parameters: - parameters – Collection of
thriftrw.idl.Fieldobjects. - service_name (str) – Name of the service under which the function was defined.
- function_name (str) – Name of the function whose parameter list is represented by this object.
- parameters – Collection of
-
function function: thriftrw.spec.service.FunctionSpec
-
link(self, scope, function)¶
-
-
class
thriftrw.spec.FunctionResultSpec(name, return_spec, exceptions)¶ Represents the result of a service function.
-
return_spec¶ thriftrw.spec.TypeSpecof the return type or None if the function does not return anything.
-
exception_specs¶ Collection of
thriftrw.spec.FieldSpecobjects defining the exceptions that this function can raise.
-
function¶ The
FunctionSpecwhose result type this spec represents. This value is available only after the spec has been linked.
The return value of a function and the exceptions raised by it implicitly form a union which contains the return value at field ID
0and the exceptions on the remaining field IDs.Changed in version 0.2: Expose the class and add the
return_specandexception_specsattributes.Changed in version 0.5: When deserializing, if an unrecognized exception is found, a
thriftrw.errors.UnknownExceptionErroris raised.Changed in version 1.0: Added the
functionattribute.-
compile(type cls, return_type, exceptions, service_name, function_name)¶ Compiles information from the AST into a FunctionResultSpec.
Parameters: - return_type – A
thriftrw.idl.Typerepresenting the return type or None if the function doesn’t return anything. - exceptions – Collection of
thriftrw.idl.Fieldobjects representing raised by the function. - service_name (str) – Name of the service under which the function was defined.
- function_name (str) – Name of the function whose result this object represents.
- return_type – A
-
exception_ids¶ exception_ids: object
-
exception_specs exception_specs: list
-
from_wire(self, Value wire_value)¶
-
function function: thriftrw.spec.service.FunctionSpec
-
link(self, scope, function)¶
-
read_from(self, ProtocolReader reader)¶
-
return_spec return_spec: thriftrw.spec.base.TypeSpec
-
Constants¶
-
class
thriftrw.spec.ConstSpec(name, value_spec, type_spec, save=None)¶ Spec for a constant value defined in the Thrift file.
The surface for a ConstSpec is the value defined in the Thrift file.
The following,:
const list<i32> foo = [1, 2, 3]
Roughly translates to,
foo = [1, 2, 3]
In the generated module.
-
name¶ Name of the constant.
-
thriftrw.wire¶
Types¶
Different TType codes supported by Thrift.
-
thriftrw.wire.ttype.BOOL= 2¶
-
thriftrw.wire.ttype.BYTE= 3¶
-
thriftrw.wire.ttype.DOUBLE= 4¶
-
thriftrw.wire.ttype.I16= 6¶
-
thriftrw.wire.ttype.I32= 8¶
-
thriftrw.wire.ttype.I64= 10¶
-
thriftrw.wire.ttype.BINARY= 11¶
-
thriftrw.wire.ttype.STRUCT= 12¶
-
thriftrw.wire.ttype.MAP= 13¶
-
thriftrw.wire.ttype.SET= 14¶
-
thriftrw.wire.ttype.LIST= 15¶
Value¶
-
class
thriftrw.wire.Value¶ Base class for Value classes.
Value classes define an intermediate representation of Thrift types as they are sent and received over the wire.
-
apply(self, ValueVisitor visitor)¶ Apply the value to the given visitor.
The appropriate visit_* method will be called and its result returned.
Parameters: visitor (ValueVisitor) – Visitor on the value. Returns: Value returned by the corresponding visit_*method.
-
-
class
thriftrw.wire.ValueVisitor¶ Visitor on different value types.
The
visit_*functions are not given the*Valueobjects but the actual values contained in them.The idea is that when something needs to take an arbitrary Thrift value and act on all cases, it extends this type. Each Thrift value knows which function to call on the visitor based on the value type. The intention here is to avoid
isinstancechecks.
Value Types¶
-
class
thriftrw.wire.BoolValue¶ Wrapper for boolean values.
-
class
thriftrw.wire.ByteValue¶ Wrapper for byte values.
-
class
thriftrw.wire.DoubleValue¶ Wrapper for double values.
-
class
thriftrw.wire.I16Value¶ Wrapper for 16-bit integer values.
-
class
thriftrw.wire.I32Value¶ Wrapper for 32-bit integer values.
-
class
thriftrw.wire.I64Value¶ Wrapper for 64-bit integer values.
-
class
thriftrw.wire.BinaryValue¶ Wrapper for binary blobs.
Note that Thrift does not differentiate between text and binary blobs over the wire. UTF-8 text should be encoded/decoded manually.
-
class
thriftrw.wire.FieldValue¶ A single field in a struct.
-
id¶ Field identifier.
-
ttype¶ TTypeof the value held in this field.
-
value¶ Value for this field.
-
-
class
thriftrw.wire.StructValue(list fields)¶ A struct value is a collection of fields of different types.
-
fields¶ Collection of
FieldValueobjects.
-
-
class
thriftrw.wire.MapValue¶ A mapping of two different kinds of values.
This object may be treated as a map.
-
key_ttype¶ Type of the keys stored in the map. This must be a value from TType.
-
value_ttype¶ Type of the values stored in the map. This must be a value from TType.
-
pairs¶ Collection of
thriftrw.wire.MapItemobjects.
-
-
class
thriftrw.wire.MapItem¶ An item in a map.
-
key¶ Key of the item
-
value¶ Value associated with the key
-
Message¶
-
class
thriftrw.wire.Message¶ A Message envelope for Thrift requests and responses.
-
name¶ Name of the method being called.
-
seqid¶ ID of the message used by the client to match responses to requests. The server’s contract is to return the same
seqidin the response that it received in the request.
-
message_type¶ Message type of the message. See
thriftrw.wire.ttype.
-
body¶ Message payload. The value here depends on the message type.
Requests and responses must be wrapped in a
Messageif you are making requests to Apache Thrift services or receiving requests from clients generated by Apache Thrift.See Calling Apache Thrift for more information.
New in version 1.0.
-
The types of messages envelopes supported by Thrift.
-
thriftrw.wire.mtype.CALL= 1¶ An outgoing call to a specific method.
Message.bodyis a struct containing the request arguments.
-
thriftrw.wire.mtype.REPLY= 2¶ A response to a
CALLmessage.Message.bodyis a union of the return value (with field ID 0) and the application exceptions that the message can raise.
-
thriftrw.wire.mtype.EXCEPTION= 3¶ An unexpected exception in response to a
CALLmessage.Message.bodyis a struct representing the exception.Note that exceptions that are defined in the IDL are returned as part of the
REPLYmessage. This message type is used for unexpected exceptions that were not defined in the IDL but which the server and client have agreed upon as standard exceptions that they both recognize.
-
thriftrw.wire.mtype.ONEWAY= 4¶ An outgoing request to a specific
onewaymethod.Message.bodyis the same as aCALLmessage but noREPLYorEXCEPTIONis expected in response.
New in version 1.0.
thriftrw.idl¶
A parser for Thrift IDL files.
Parser¶
Adapted from thriftpy.parser.
-
class
thriftrw.idl.Parser(**kwargs)¶ Parser for Thrift IDL files.
-
parse(input, **kwargs)¶ Parse the given input.
Parameters: input – String containing the text to be parsed. Raises: thriftrw.errors.ThriftParserError – For parsing errors.
-
AST¶
-
class
thriftrw.idl.Program¶ The top-level object representing the full Thrift IDL.
The following attributes are available:
Headers¶
-
class
thriftrw.idl.Include¶ A request to include the Thrift file at the given path.
include "./common.thrift" struct User { 1: required common.UUID uuid }
thriftrw also supports a custom “include-as” syntax if explicitly enabled.
include t "./types.thrift" struct User { 1: required t.UUID uuid }
Note that this is a custom addition to the Thrift grammar and will break compatbility with Apache Thrift.
-
name¶ If the “include-as” form was used, this is the custom name specified for the imported module.
-
path¶ Path to the file to be included.
Changed in version 1.1: Added
nameattribute.-
Definitions¶
-
class
thriftrw.idl.Const¶ A constant defined in the Thrift IDL.
const i32 DEFAULT_ID = 0;
-
name¶ Name of the constant.
-
value_type¶ Type of value held by the constant.
-
value¶ Value specified for the constant.
-
-
class
thriftrw.idl.Typedef¶ Typedefs define a new type which is an alias for another type.
typedef string UUID
-
name¶ Name of the new defined type.
-
target_type¶ Type being aliased.
-
annotations¶ Annotations for this type. See
Annotation.
-
-
class
thriftrw.idl.Enum¶ Enums define a new type which is a set of named integer values.
enum Role { USER = 1, ADMIN, }
-
name¶ Name of the enum type.
-
annotations¶ Annotations for this type. See
Annotation.
-
-
class
thriftrw.idl.EnumItem¶ An item defined in an
Enumdefinition.-
name¶ Name of the item.
-
value¶ Value specified for this item, if any.
Noneothewise.
-
annotations¶ Annotations for this item. See
Annotation.
-
-
class
thriftrw.idl.Struct¶ A struct is a collection of named fields.
-
name¶ Name of the struct.
-
annotations¶ Annotations for this type. See
Annotation.
-
-
class
thriftrw.idl.Union¶ A union is a sum of different types.
-
name¶ Name of the union.
-
annotations¶ Annotations for this type. See
Annotation.
-
-
class
thriftrw.idl.Exc¶ A Thrift exception definition.
-
name¶ Name of the exception class.
-
annotations¶ Annotations for this type. See
Annotation.
-
-
class
thriftrw.idl.Service¶ A service definition.
-
name¶ Name of the service.
-
parent¶ ServiceReferenceto the parent service orNoneif this service dosen’t have a parent service.
-
annotations¶ Annotations for this service. See
Annotation.
-
-
class
thriftrw.idl.ServiceReference¶ A reference to another service.
-
name¶ Name of the referenced service.
-
-
class
thriftrw.idl.Function¶ A function defined inside a service.
-
name¶ Name of the function.
-
return_type¶ The type of value returned by this method.
-
oneway¶ Whether this method is
onewayor not.
-
annotations¶ Annotations for this method. See
Annotation.
-
-
class
thriftrw.idl.Field¶ A field defined inside a struct, union, exception, or parameter list.
-
id¶ The numeric field identifier. None if not specified.
-
name¶ Name of the field.
-
field_type¶ Type of value held by the field.
-
requiredness¶ Trueif this field wasrequired,Falseifoptional.Noneif required or optional was not specified.
-
default¶ Default value of the field.
Noneif not specified.
-
annotations¶ Annotations for this field. See
Annotation.
-
Types¶
-
class
thriftrw.idl.PrimitiveType¶ Reference to primitive types.
-
name¶ Name of the primitive type.
-
annotations¶ Annotations for this type. See
Annotation.
-
-
class
thriftrw.idl.MapType¶ A
map<key, value>type.-
key_type¶ Type of the keys in the map.
-
value_type¶ Type of the values in the map.
-
annotations¶ Annotations for this type. See
Annotation.
-
-
class
thriftrw.idl.SetType¶ A
set<item>type.-
value_type¶ Type of items in the set.
-
annotations¶ Annotations for this type. See
Annotation.
-
-
class
thriftrw.idl.ListType¶ A
list<item>type.-
value_type¶ Type of items in the list.
-
annotations¶ Annotations for this type. See
Annotation.
-
thriftrw.errors¶
-
class
thriftrw.errors.ThriftError¶ Base class for all exceptions raised by thriftrw.
-
class
thriftrw.errors.ThriftParserError¶ Exception raised by the parser or lexer in case of errors.
-
class
thriftrw.errors.ThriftCompilerError¶ Exception raised during IDL compilation.
-
class
thriftrw.errors.ThriftProtocolError¶ Exceptions raised by Protocol implementations for errors encountered during serialization or deserialization.
-
class
thriftrw.errors.EndOfInputError¶ The input was shorter than expected.
-
class
thriftrw.errors.UnknownExceptionError(message, thrift_response=None)¶ We parsed an unknown exception in a function response.