Beispiel #1
0
class BaseTestCase(unittest.TestCase):
    def setUp(self):
        addr = 'inproc://test'
        self.client = Requester(addr)
        self.service = Responder(addr)
        self.service.register('divide', lambda x, y: x / y)
        self.service.register('echo', lambda x: x)

    def tearDown(self):
        self.client.socket.close()
        self.service.socket.close()
class DialogService(object):
    ''' Create an dialog cloud service for Chatbots'''
    def __init__(self,
                 engine,
                 address='ipc:///tmp/dialog-service.sock',
                 encoder=JSONEncoder):
        self.engine = engine
        self.address = address
        self.encoder = encoder
        self.service = Responder(self.address, encoder=self.encoder())
        self.service.register('reply', self.engine.reply)
Beispiel #3
0
class BaseTestCase(unittest.TestCase):

    def setUp(self):
        addr = 'inproc://test'
        self.client = Requester(addr)
        self.service = Responder(addr)
        self.service.register('divide', lambda x, y: x / y)
        self.service.register('echo', lambda x: x)

    def tearDown(self):
        self.client.socket.close()
        self.service.socket.close()
Beispiel #4
0
def main():
    global CONF

    time.sleep(random.random())
    logging.basicConfig(level=logging.DEBUG)

    conf_filepath = sys.argv[1]
    CONF = config.load(conf_filepath)

    service = Responder(
        'ipc:///tmp/auth-example-service.sock',
        authenticator=Authenticator(
            CONF.authenticator['secret'], hashlib.sha256
        )
    )

    service.register('generate_uuid', generate_uuid)
    service.start()
Beispiel #5
0
    def run(self):

        self.sock_addr = 'ipc:///tmp/service.sock'
        self.tcp_addr = 'tcp://127.0.0.1:9443'

        s = Responder(self.tcp_addr, timeouts=(None, None))
        s.register('echo', echo)
        s.register('node_cfg', get_node_cfg)
        s.register('offline', offline)
        s.register('wedged', wedged)
        s.start()
Beispiel #6
0
class IntentService(object):
    ''' Expose Intent cloud service.'''
    def __init__(self,
                 retrain=False,
                 address='ipc:///tmp/intent-service.sock',
                 encoder=JSONEncoder):
        self.engine = Intent(retrain=retrain)
        self.address = address
        self.encoder = encoder
        self.service = Responder(self.address, encoder=self.encoder())
        self.service.register('predict', self.predict)
        self.service.register('echo', self.echo)

    def predict(self, msg):
        response = self.engine.predict(msg)
        print response[0][0]
        return str(response[0][0])
    
    def echo(self, msg):
        return 'hello {}'.format(msg)
Beispiel #7
0
def start_service(addr, encoder, authenticator=None):
    """ Start a service with options """

    s = Responder(addr, encoder=encoder,
                  authenticator=authenticator, timeouts=(3000, 3000))
    s.register('none', lambda: None)
    s.register('divide', lambda x, y: x / y)
    s.register('upper', lambda dct: {k: v.upper() for k, v in dct.items()})
    s.start()
Beispiel #8
0
class ANNService(object):
    ''' Create an index cloud service for Deep Learning based vectors index'''
    def __init__(self, store, address='ipc:///tmp/ann-service.sock', encoder=JSONEncoder):
        self.store = store
        self.address = address
        self.encoder = encoder
        self.service = Responder(self.address, encoder=self.encoder())
        self.service.register('build_index', self.store.build_index)
        self.service.register('clean_index', self.store.clean_index)
        self.service.register('word_vector', self.store.get_word_vector)
        self.service.register('nn', self.store.nearest_neighbors)
Beispiel #9
0
def start_service(addr, encoder, authenticator=None):
    """ Start a service with options """

    s = Responder(addr,
                  encoder=encoder,
                  authenticator=authenticator,
                  timeouts=(3000, 3000))
    s.register('none', lambda: None)
    s.register('divide', lambda x, y: x / y)
    s.register('upper', lambda dct: {k: v.upper() for k, v in dct.items()})
    s.start()
class RecommendService(object):
    ''' Create an cache service for User-Room Vectors for Introduction radars'''
    def __init__(self,
                 index,
                 address='ipc:///tmp/recommend-service.sock',
                 encoder=JSONEncoder):
        self.index = index
        self.address = address
        self.encoder = encoder
        self.service = Responder(self.address, encoder=self.encoder())
        self.service.register('add_index', self.index.add_index)
        self.service.register('build_index', self.index.build_index)
        self.service.register('build_index_db', self.index.build_index_from_db)
        self.service.register('clean_index', self.index.clean_index)
        self.service.register('word_vector', self.index.get_word_vector)
        self.service.register('nn', self.index.nearest_neighbors)
Beispiel #11
0
# A simple service for testing purposes
from nanoservice import Responder


def ping():
    return 'pong'


def add(x, y):
    return x + y


addr = 'ipc:///tmp/math-test-service.sock'
s = Responder(addr)
s.register('ping', ping)
s.register('add', add)

print('Starting serice on address {}'.format(addr))
s.start()
Beispiel #12
0
class ConfigService(object):
    '''Expose NLP cloud service.'''
    def __init__(self,
                 nlp,
                 address='ipc:///tmp/nlp-service.sock',
                 encoder=JSONEncoder):
        self.nlp = nlp
        self.address = address
        self.encoder = encoder
        #self.service = Responder(self.address, encoder=self.encoder)
        self.service = Responder(self.address, encoder=self.encoder())
        # Introduction Planner: build the cognitive map
        self.service.register('similar', self.nlp.similar)
        # understand the action and predict the bot to perform the action
        self.service.register('intent', self.nlp.intent)
        # predict the next action your bot should perform
        self.service.register('converse', self.nlp.converse)
        # (Understand) : parse a message into structured data :->
        # extract event, datetime, location, person, numbers, ...
        self.service.register('all', self.nlp.get_all)
        # the full parse
        self.service.register('parse', self.nlp.parse)
        # word vector
        self.service.register('vector', self.nlp.vector)
        # validate parser
        self.service.register('validate_email', self.nlp.validate_email)
        self.service.register('validate_person', self.nlp.validate_person)
        self.service.register('validate_date', self.nlp.validate_date)
        self.service.register('validate_address', self.nlp.validate_address)

        self.service.register('echo', self.nlp.echo)
Beispiel #13
0
class Program(BaseProgram):
    """ Long running program with a nanoservice endpoint.

    `service` - nanoservice Responder object
    `config` - the configuration parsed from --config <filepath> """
    def __init__(self, description, address):
        super(Program, self).__init__(description, address)

        self.continue_event = threading.Event()
        self.continue_event.set()
        self.service = Responder(self.address)
        self.service.continue_event = self.continue_event
        self.service.stop_cleanup = self.stop_cleanup
        self.restart_requested = False

        if self.service is None:
            return

        self.cli_sessions = Sessions(self)
        self.svc_sessions = Sessions(self)
        self.service.get_cli_session = self.cli_sessions.session_get
        if not self.auth_token:
            self.auth_token = str(uuid.uuid4())
        self.cli_sessions.session_add(None, self.session_uuid, self.auth_token)
        self.session = self.cli_sessions.sessions[self.session_uuid]
        self.cli_sessions._tag_set(self.session, 'domain', 'system')

        # Add default service worker, which will respond to ctl commands
        # Other workers will perform other kind of work, such as
        # fetching resources from the web, etc
        self.workers.append(worker.ServiceWorker(self.service))

        # Add default commands
        self.add_command('ping', lambda _: 'pong')
        self.add_command('help', self.help_function)
        self.add_command('stop', self.stop_function)
        self.add_command('restart', self.restart_function, 'Restart service')
        self.add_command(
            'login', self.cli_sessions.login,
            'Login with session_uuid, username and password '
            'to obtain an auth token for the new session_uuid')
        self.add_command('logout', self.cli_sessions.logout)
        self.add_command('cli_session_add', self.cli_sessions.session_add,
                         'Add session for client with provided uuid')
        self.add_command('cli_session_del', self.cli_sessions.session_del)
        self.add_command('cli_session_auth_token_add',
                         self.cli_sessions.auth_token_add)
        self.add_command('cli_session_auth_token_del',
                         self.cli_sessions.auth_token_del)
        self.add_command('cli_session_tag_set', self.cli_sessions.tag_set)
        self.add_command('cli_session_tag_del', self.cli_sessions.tag_del)
        self.add_command('cli_session_tag_get', self.cli_sessions.tag_get)
        self.add_command('cli_session_cap_set', self.cli_sessions.cap_set)
        self.add_command('cli_session_cap_del', self.cli_sessions.cap_del)
        self.add_command('cli_session_cap_get', self.cli_sessions.cap_get)
        # self.add_command('remote', self.remote_func)
        self.add_command('queue_hello', self.queue_hello_function)
        self.add_command('noop', self.noop_function)
        self.file_operation = FileOperation('/')
        self.add_command('file_make_archive', self.file_operation.make_archive,
                         shutil.make_archive.__doc__)
        self.add_command('file_get_archive_formats',
                         self.file_operation.get_archive_formats)
        self.add_command('file_get', self.file_operation.file_get),
        self.add_command('file_get_size', self.file_operation.file_get_size),
        self.add_command('file_put', self.file_operation.file_put),
        self.create_worker_pool()

    def stop_cleanup(self):
        for w in self.workers:
            self.cli_sessions.queue.put('stop')

    def create_worker_pool(self):
        for i in range(10):
            self.workers.append(
                worker.QueueWorker(program=self, sessions=self.cli_sessions))

    def queue_hello_function(self, ctx, who):
        import time

        def hello(who, other='test'):
            logging.debug('start hello for {} from {}'.format(
                ctx.session.session_uuid, other))
            time.sleep(5)
            logging.debug('finished hello for ' + ctx.session.session_uuid)
            return 'hello {} from {}'.format(ctx.session.session_uuid, other)

        ctx.func = hello
        ctx.args = [who]
        ctx.kwargs = dict(other='test3')
        self.cli_sessions.enqueue_ctx(ctx)

    def noop_function(self, ctx):
        pass

    def help_function(self, ctx, command=None):
        """ Show help for all available commands or just a single one """
        if command:
            return self.registered[command].get('description',
                                                'No help available')
        return ', '.join(sorted(self.registered))

    def ping_function(self, ctx):
        return 'pong'

    def stop_function(self, ctx):
        if ctx.session.tags['domain'] != 'system':
            raise AuthorisationError('tag domain:system required')
        self.continue_event.clear()

    def restart_function(self, ctx):
        if ctx.session.tags['domain'] != 'system':
            raise AuthorisationError('tag domain:system required')
        self.restart_requested = True
        self.stop_function(ctx)

    def restart(self):
        os.execv(sys.argv[0], sys.argv)

    def add_command(self, command, function, description=None):
        """ Register a new function for command """
        super(Program, self).add_command(command, function, description)
        self.service.register(command, function)

    def run(self, args=None):
        """ Parse comand line arguments/flags and run program """

        args = args or self.parser.parse_args()
        super(Program, self).run(args)

        # Start workers then wait until they finish work
        [w.start() for w in self.workers]

        while self.continue_event.wait(1):
            [w.join() for w in self.workers]

        if self.restart_requested:
            self.restart()
Beispiel #14
0
import logging
from nanoservice import Responder


def greet(name):
    return 'Hello {}'.format(name)


def add(x, y):
    return x + y


s = Responder('ipc:///tmp/service.sock')
s.register('greet', greet)
s.register('add', add)
s.start()
Beispiel #15
0
# A simple service for testing purposes
from nanoservice import Responder


def ping():
    return "pong"


def add(x, y):
    return x + y


addr = "ipc:///tmp/math-test-service.sock"
s = Responder(addr)
s.register("ping", ping)
s.register("add", add)

print("Starting serice on address {}".format(addr))
s.start()
Beispiel #16
0
# A simple service for testing purposes
from nanoservice import Responder
from nanoservice.encoder import JSONEncoder


def ping():
    return 'pong'


def uppercase(string):
    return string.upper()


addr = 'ipc:///tmp/string-test-service.sock'
s = Responder(addr, encoder=JSONEncoder())
s.register('ping', ping)
s.register('uppercase', uppercase)

print('Starting serice on address {}'.format(addr))
s.start()
Beispiel #17
0
 def start_service(self, addr, authenticator=None):
     s = Responder(addr, authenticator=authenticator, timeouts=(3000, 3000))
     s.register('divide', lambda x, y: x / y)
     s.start()