import os import thriftrw this_dir = os.path.dirname(__file__) cadence_thrift_file = os.path.join(this_dir, "thrift/cadence.thrift") cadence_thrift = thriftrw.load(cadence_thrift_file)
def load(path): """Loads the Thrift file at the given path.""" return _wrap(thriftrw.load(path))
from __future__ import absolute_import, unicode_literals, print_function import os.path import requests import thriftrw ping = thriftrw.load(os.path.join(os.path.dirname(__file__), "ping.thrift")) def main(): req = ping.Ping.ping.request("world") response = requests.post("http://127.0.0.1:8888/thrift", data=ping.dumps.message(req, seqid=42)) reply = ping.loads.message(ping.Ping, response.content) assert reply.name == "ping" assert reply.seqid == 42 resp = reply.body print(resp) if __name__ == "__main__": main()
# -*- coding: UTF-8 -*- import os import thriftrw file_dir = os.path.dirname(__file__) uno_service = thriftrw.load(os.path.join(file_dir, 'protocol.thrift')) class Card(uno_service.Card): ''' monkeypatch thrift class ''' @property def cost(self): '''Карты с цифрой — стоимость по значению на карте. Карты действий (кроме черных) — 20 баллов. Черные карты действий — 50 баллов. ''' if self.color == uno_service.Color.BLACK: return 50 if self.action is not None: return 20 return self.number @classmethod def simple_str(cls, card): if card is not None: return '{},{},{},{}'.format( uno_service.Color.name_of(card.color), card.number, uno_service.CardAction.name_of(card.action), uno_service.Color.name_of(card.required_color)) else:
def load(path, service=None, hostport=None, module_name=None): """Loads the Thrift file at the specified path. The file is compiled in-memory and a Python module containing the result is returned. It may be used with ``TChannel.thrift``. For example, .. code-block:: python from tchannel import TChannel, thrift # Load our server's interface definition. donuts = thrift.load(path='donuts.thrift') # We need to specify a service name or hostport because this is a # downstream service we'll be calling. coffee = thrift.load(path='coffee.thrift', service='coffee') tchannel = TChannel('donuts') @tchannel.thrift.register(donuts.DonutsService) @tornado.gen.coroutine def submitOrder(request): args = request.body if args.coffee: yield tchannel.thrift( coffee.CoffeeService.order(args.coffee) ) # ... The returned module contains, one top-level type for each struct, enum, union, exeption, and service defined in the Thrift file. For each service, the corresponding class contains a classmethod for each function defined in that service that accepts the arguments for that function and returns a ``ThriftRequest`` capable of being sent via ``TChannel.thrift``. For more information on what gets generated by ``load``, see `thriftrw <http://thriftrw.readthedocs.org/en/latest/>`_. Note that the ``path`` accepted by ``load`` must be either an absolute path or a path relative to the *the current directory*. If you need to refer to Thrift files relative to the Python module in which ``load`` was called, use the ``__file__`` magic variable. .. code-block:: python # Given, # # foo/ # myservice.thrift # bar/ # x.py # # Inside foo/bar/x.py, path = os.path.join( os.path.dirname(__file__), '../myservice.thrift' ) The returned value is a valid Python module. You can install the module by adding it to the ``sys.modules`` dictionary. This will allow importing items from this module directly. You can use the ``__name__`` magic variable to make the generated module a submodule of the current module. For example, .. code-block:: python # foo/bar.py import sys from tchannel import thrift donuts = = thrift.load('donuts.thrift') sys.modules[__name__ + '.donuts'] = donuts This installs the module generated for ``donuts.thrift`` as the module ``foo.bar.donuts``. Callers can then import items from that module directly. For example, .. code-block:: python # foo/baz.py from foo.bar.donuts import DonutsService, Order def baz(tchannel): return tchannel.thrift( DonutsService.submitOrder(Order(..)) ) :param str service: Name of the service that the Thrift file represents. This name will be used to route requests through Hyperbahn. :param str path: Path to the Thrift file. If this is a relative path, it must be relative to the current directory. :param str hostport: Clients can use this to specify the hostport at which the service can be found. If omitted, TChannel will route the requests through known peers. This value is ignored by servers. :param str module_name: Name used for the generated Python module. Defaults to the name of the Thrift file. """ # TODO replace with more specific exceptions # assert service, 'service is required' # assert path, 'path is required' # Backwards compatibility for callers passing in service name as first arg. if not path.endswith('.thrift'): service, path = path, service module = thriftrw.load(path=path, name=module_name) return TChannelThriftModule(service, module, hostport)
from __future__ import absolute_import, unicode_literals, print_function import time import os.path from tornado import web from tornado.ioloop import IOLoop from tornado.httpserver import HTTPServer import thriftrw ping = thriftrw.load( os.path.join(os.path.dirname(__file__), 'ping.thrift'), ) class ThriftRequestHandler(web.RequestHandler): def post(self): assert self.request.body message = ping.loads.message(ping.Ping, self.request.body) method, handler = self._METHODS[message.name] args = message.body resp = method.response(success=handler(self, args)) reply = ping.dumps.message(resp, seqid=message.seqid) self.write(reply) def handle_ping(self, args):