本文整理汇总了Python中inbox.api.kellogs.APIEncoder.cereal方法的典型用法代码示例。如果您正苦于以下问题:Python APIEncoder.cereal方法的具体用法?Python APIEncoder.cereal怎么用?Python APIEncoder.cereal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inbox.api.kellogs.APIEncoder
的用法示例。
在下文中一共展示了APIEncoder.cereal方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: streaming_change_generator
# 需要导入模块: from inbox.api.kellogs import APIEncoder [as 别名]
# 或者: from inbox.api.kellogs.APIEncoder import cereal [as 别名]
def streaming_change_generator(namespace_id, poll_interval, timeout,
transaction_pointer, exclude_types=None):
"""
Poll the transaction log for the given `namespace_id` until `timeout`
expires, and yield each time new entries are detected.
Arguments
---------
namespace_id: int
Id of the namespace for which to check changes.
poll_interval: float
How often to check for changes.
timeout: float
How many seconds to allow the connection to remain open.
transaction_pointer: int, optional
Yield transaction rows starting after the transaction with id equal to
`transaction_pointer`.
"""
encoder = APIEncoder()
start_time = time.time()
while time.time() - start_time < timeout:
with session_scope() as db_session:
deltas, new_pointer = format_transactions_after_pointer(
namespace_id, transaction_pointer, db_session, 100,
_format_transaction_for_delta_sync, exclude_types)
if new_pointer is not None and new_pointer != transaction_pointer:
transaction_pointer = new_pointer
for delta in deltas:
yield encoder.cereal(delta) + '\n'
else:
gevent.sleep(poll_interval)
示例2: format_output
# 需要导入模块: from inbox.api.kellogs import APIEncoder [as 别名]
# 或者: from inbox.api.kellogs.APIEncoder import cereal [as 别名]
def format_output(public_snapshot, include_body):
# Because we're using a snapshot of the message API representation in the
# transaction log, we can just return that directly (without the 'body'
# field if include_body is False).
encoder = APIEncoder()
return encoder.cereal({k: v for k, v in public_snapshot.iteritems()
if k != 'body' or include_body})
示例3: streaming_change_generator
# 需要导入模块: from inbox.api.kellogs import APIEncoder [as 别名]
# 或者: from inbox.api.kellogs.APIEncoder import cereal [as 别名]
def streaming_change_generator(
namespace,
poll_interval,
timeout,
transaction_pointer,
exclude_types=None,
include_types=None,
exclude_folders=True,
exclude_metadata=True,
exclude_account=True,
expand=False,
is_n1=False,
):
"""
Poll the transaction log for the given `namespace_id` until `timeout`
expires, and yield each time new entries are detected.
Arguments
---------
namespace_id: int
Id of the namespace for which to check changes.
poll_interval: float
How often to check for changes.
timeout: float
How many seconds to allow the connection to remain open.
transaction_pointer: int, optional
Yield transaction rows starting after the transaction with id equal to
`transaction_pointer`.
"""
encoder = APIEncoder(is_n1=is_n1)
start_time = time.time()
while time.time() - start_time < timeout:
with session_scope(namespace.id) as db_session:
deltas, new_pointer = format_transactions_after_pointer(
namespace,
transaction_pointer,
db_session,
100,
exclude_types,
include_types,
exclude_folders,
exclude_metadata,
exclude_account,
expand=expand,
is_n1=is_n1,
)
if new_pointer is not None and new_pointer != transaction_pointer:
transaction_pointer = new_pointer
for delta in deltas:
yield encoder.cereal(delta) + "\n"
else:
yield "\n"
gevent.sleep(poll_interval)
示例4: g
# 需要导入模块: from inbox.api.kellogs import APIEncoder [as 别名]
# 或者: from inbox.api.kellogs.APIEncoder import cereal [as 别名]
def g():
encoder = APIEncoder()
with session_scope(self.account_id) as db_session:
for imap_uids in self._search(db_session, search_query):
query = db_session.query(Message) \
.join(ImapUid) \
.filter(ImapUid.account_id == self.account_id,
ImapUid.msg_uid.in_(imap_uids))\
.order_by(desc(Message.received_date))\
yield encoder.cereal(query.all()) + '\n'
示例5: g
# 需要导入模块: from inbox.api.kellogs import APIEncoder [as 别名]
# 或者: from inbox.api.kellogs.APIEncoder import cereal [as 别名]
def g():
encoder = APIEncoder()
with session_scope(self.account_id) as db_session:
yield encoder.cereal(self.search_threads(db_session, search_query)) + '\n'
示例6: WebhookService
# 需要导入模块: from inbox.api.kellogs import APIEncoder [as 别名]
# 或者: from inbox.api.kellogs.APIEncoder import cereal [as 别名]
class WebhookService():
"""Asynchronously consumes the transaction log and executes registered
webhooks."""
def __init__(self, poll_interval=1, chunk_size=22):
self.workers = defaultdict(set)
self.log = get_logger()
self.poll_interval = poll_interval
self.chunk_size = chunk_size
self.minimum_id = -1
self.poller = None
self.polling = False
self.encoder = APIEncoder()
self._on_startup()
@property
def all_active_workers(self):
worker_sets = self.workers.values()
if not worker_sets:
return set()
return set.union(*worker_sets)
def register_hook(self, namespace_id, parameters):
"""Register a new webhook.
Parameters
----------
namespace_id: int
ID for the namespace to apply the webhook on.
parameters: dictionary
Dictionary of the hook parameters.
"""
# TODO(emfree) do more meaningful parameter validation here
# (or in the calling code in the API)
if urlparse.urlparse(parameters.get('callback_url')).scheme != 'https':
raise ValueError('callback_url MUST be https!')
with session_scope() as db_session:
lens = Lens(
namespace_id=namespace_id,
subject=parameters.get('subject'),
thread_public_id=parameters.get('thread'),
to_addr=parameters.get('to'),
from_addr=parameters.get('from'),
cc_addr=parameters.get('cc'),
bcc_addr=parameters.get('bcc'),
any_email=parameters.get('any_email'),
started_before=parameters.get('started_before'),
started_after=parameters.get('started_after'),
last_message_before=parameters.get('last_message_before'),
last_message_after=parameters.get('last_message_after'),
filename=parameters.get('filename'))
hook = Webhook(
namespace_id=namespace_id,
lens=lens,
callback_url=parameters.get('callback_url'),
failure_notify_url=parameters.get('failure_notify_url'),
include_body=parameters.get('include_body', False),
active=parameters.get('active', True),
min_processed_id=self.minimum_id)
db_session.add(hook)
db_session.add(lens)
db_session.commit()
if hook.active:
self._start_hook(hook, db_session)
return self.encoder.cereal(hook, pretty=True)
def start_hook(self, hook_public_id):
with session_scope() as db_session:
hook = db_session.query(Webhook). \
filter_by(public_id=hook_public_id).one()
self._start_hook(hook, db_session)
def _start_hook(self, hook, db_session):
self.log.info('Starting hook with public id {}'.format(hook.public_id))
if any(worker.id == hook.id for worker in self.all_active_workers):
# Hook already has a worker
return 'OK hook already running'
hook.min_processed_id = self.minimum_id
hook.active = True
namespace_id = hook.namespace_id
worker = WebhookWorker(hook)
self.workers[namespace_id].add(worker)
if not worker.started:
worker.start()
db_session.commit()
if not self.polling:
self._start_polling()
return 'OK hook started'
def stop_hook(self, hook_public_id):
self.log.info('Stopping hook with public id {}'.format(hook_public_id))
with session_scope() as db_session:
hook = db_session.query(Webhook). \
filter_by(public_id=hook_public_id).one()
hook.active = False
#.........这里部分代码省略.........