nopaque/app/corpora/cqi/client.py

47 lines
1.2 KiB
Python
Raw Normal View History

2020-03-23 08:10:35 +00:00
from .api import APIClient
from .models.corpora import CorpusCollection
2020-03-25 14:39:32 +00:00
class CQiClient:
2020-04-06 12:53:50 +00:00
"""
A client for communicating with a CQi server.
Example:
>>> import cqi
>>> client = cqi.CQiClient('127.0.0.1')
>>> client.connect()
{'code': 258, 'msg': 'CQI_STATUS_CONNECT_OK'}
>>> client.ping()
{'code': 260, 'msg': 'CQI_STATUS_PING_OK'}
>>> client.disconnect()
{'code': 259, 'msg': 'CQI_STATUS_BYE_OK'}
2020-04-07 14:36:27 +00:00
2020-04-06 12:53:50 +00:00
Attributes:
2020-04-07 14:36:27 +00:00
api (APIClient): An API client pointing to the specified CQP server.
2020-04-06 12:53:50 +00:00
"""
2020-03-23 08:10:35 +00:00
def __init__(self, host, port=4877):
2020-04-06 12:53:50 +00:00
"""
CQiClient constructor
Args:
host (str): URL to the CQP server. For example,
``cqpserver.localhost`` or ``127.0.0.1``.
port (int): Port the CQP server listens on. Default: ``4877``
"""
2020-03-25 14:39:32 +00:00
self.api = APIClient(host, port=port)
2020-03-23 08:10:35 +00:00
def connect(self, username='anonymous', password=''):
2020-03-29 10:38:24 +00:00
status = self.api.ctrl_connect(username, password)
return status
2020-03-23 08:10:35 +00:00
2020-03-25 14:39:32 +00:00
def disconnect(self):
2020-03-29 10:38:24 +00:00
return self.api.ctrl_bye()
2020-04-06 12:53:50 +00:00
def ping(self):
return self.api.ctrl_ping()
@property
def corpora(self):
return CorpusCollection(client=self)