示例#1
0
class SlackClient(object):
    def __init__(self, token):
        self.token = token
        self.server = Server(self.token, False)

    def rtm_connect(self):
        try:
            self.server.rtm_connect()
            return True
        except Exception as e:
            print e
            return False

    def api_call(self, method, **kwargs):
        return self.server.api_call(method, **kwargs)

    def rtm_read(self):
        #in the future, this should handle some events internally i.e. channel creation
        if self.server:
            json_data = self.server.websocket_safe_read()
            data = []
            if json_data != '':
                for d in json_data.split('\n'):
                    data.append(json.loads(d))
            return data
        else:
            raise SlackNotConnected

    def rtm_send_message(self, channel, message):
        if channel.startswith("D"):
            return self.server.ims.find(channel).send_message(message)
        else:
            return self.server.channels.find(channel).send_message(message)
示例#2
0
文件: _client.py 项目: 4c00h/limbo
class SlackClient(object):
    def __init__(self, token):
        self.token = token
        self.server = Server(self.token, False)

    def rtm_connect(self):
        try:
            self.server.rtm_connect()
            return True
        except:
            return False

    def api_call(self, method, **kwargs):
        return self.server.api_call(method, kwargs)

    def rtm_read(self):
        #in the future, this should handle some events internally i.e. channel creation
        if self.server:
            json_data = self.server.websocket_safe_read()
            data = []
            if json_data != '':
                for d in json_data.split('\n'):
                    data.append(json.loads(d))
            return data
        else:
            raise SlackNotConnected

    def rtm_send_message(self, channel, message):
        return self.server.channels.find(channel).send_message(message)
示例#3
0
 def __init__(self):
     super().__init__()
     self.waiting_task_list: Queue[dict]
     self.input = InputThread(callback_task_finish=self.stop_task,
                              callback_add_task=self.add_task,
                              callback_exit=self.callback_exit)
     self.input.daemon = True
     self.input.start()
     self.server = Server(interface=self)
     self.server.daemon = True
     self.server.start()
     self.timer = TimerThread(self.callback_timer_add_task)
     self.timer.daemon = True
     self.timer.start()
示例#4
0
文件: _client.py 项目: uditsrn28/POCS
class SlackClient(object):
    def __init__(self, token):
        self.token = token
        self.server = Server(self.token, False)

    def rtm_connect(self):
        try:
            self.server.rtm_connect()
            return True
        except:
            return False

    def api_call(self, method, **kwargs):
        return self.server.api_call(method, **kwargs)

    def rtm_read(self):
        # in the future, this should handle some events internally i.e. channel
        # creation
        if self.server:
            json_data = self.server.websocket_safe_read()
            data = []
            if json_data != '':
                for d in json_data.split('\n'):
                    data.append(json.loads(d))
            for item in data:
                self.process_changes(item)
            return data
        else:
            raise SlackNotConnected

    def rtm_send_message(self, channel, message):
        return self.server.channels.find(channel).send_message(message)

    def process_changes(self, data):
        if "type" in data.keys():
            if data["type"] == 'channel_created':
                channel = data["channel"]
                self.server.attach_channel(channel["name"], channel["id"], [])
            if data["type"] == 'im_created':
                channel = data["channel"]
                self.server.attach_channel(channel["user"], channel["id"], [])
            if data["type"] == "team_join":
                user = data["user"]
                self.server.parse_user_data([user])
            pass
示例#5
0
文件: _client.py 项目: uditsrn28/POCS
class SlackClient(object):
    def __init__(self, token):
        self.token = token
        self.server = Server(self.token, False)

    def rtm_connect(self):
        try:
            self.server.rtm_connect()
            return True
        except:
            return False

    def api_call(self, method, **kwargs):
        return self.server.api_call(method, **kwargs)

    def rtm_read(self):
        # in the future, this should handle some events internally i.e. channel
        # creation
        if self.server:
            json_data = self.server.websocket_safe_read()
            data = []
            if json_data != '':
                for d in json_data.split('\n'):
                    data.append(json.loads(d))
            for item in data:
                self.process_changes(item)
            return data
        else:
            raise SlackNotConnected

    def rtm_send_message(self, channel, message):
        return self.server.channels.find(channel).send_message(message)

    def process_changes(self, data):
        if "type" in data.keys():
            if data["type"] == 'channel_created':
                channel = data["channel"]
                self.server.attach_channel(channel["name"], channel["id"], [])
            if data["type"] == 'im_created':
                channel = data["channel"]
                self.server.attach_channel(channel["user"], channel["id"], [])
            if data["type"] == "team_join":
                user = data["user"]
                self.server.parse_user_data([user])
            pass
示例#6
0
文件: _client.py 项目: uditsrn28/POCS
 def __init__(self, token):
     self.token = token
     self.server = Server(self.token, False)
示例#7
0
class Muppet(Process, ServerCallBackInterface):
    waiting_task_list = Queue()
    waiting_task_set = set()
    waiting_task_set_lock: Lock = Lock()
    processing_task_dic: Dict[str, TaskThread] = {}
    processing_task_dic_lock: Lock = Lock()
    _status = True

    def __init__(self):
        super().__init__()
        self.waiting_task_list: Queue[dict]
        self.input = InputThread(callback_task_finish=self.stop_task,
                                 callback_add_task=self.add_task,
                                 callback_exit=self.callback_exit)
        self.input.daemon = True
        self.input.start()
        self.server = Server(interface=self)
        self.server.daemon = True
        self.server.start()
        self.timer = TimerThread(self.callback_timer_add_task)
        self.timer.daemon = True
        self.timer.start()

    def run(self) -> None:
        try:
            while self._status:
                if len(self.processing_task_dic) < (
                        cpu_count() / 2 - 1 if cpu_count() / 2 - 1 > 0 else 1):
                    i = self.task_pop()
                    if i['task'] == "":
                        continue
                    if i['task'] not in self.processing_task_dic.keys():
                        flag = True
                        for j in self.processing_task_dic.keys():
                            if config.get_projectName(
                                    i['task']) == config.get_projectName(j):
                                flag = False
                                break
                        if flag:
                            self.run_task(i['task'], i['svn_check'])
                        else:
                            self.waiting_task_list.put(i, block=True)
                    else:
                        print("Duplicated tasks error", i)
        except EOFError:
            print("Muppet closing")
        finally:
            self.clean_tasks()

    def terminate(self) -> None:
        self.clean_tasks()
        super().terminate()

    def __del__(self):
        self.waiting_task_list.close()

    def clean_tasks(self):
        self.processing_task_dic_lock.acquire()
        for _, j in self.processing_task_dic.items():
            j.terminate()
        self.processing_task_dic_lock.release()

    def run_task(self, task: str, svn_check):
        print("Task", task, "running")
        self.server.broadcast_task_status_change(task, "running")
        self.processing_task_dic_lock.acquire()
        self.processing_task_dic[task] = TaskThread(
            task,
            finish=self.stop_task,
            svn_check=svn_check,
            push=self.server.broadcast_logs)
        self.processing_task_dic[task].daemon = True
        self.processing_task_dic_lock.release()
        self.processing_task_dic[task].start()

    def task_push(self, task: str, svn_check=False):
        self.waiting_task_list.put({
            'task': task,
            'svn_check': svn_check
        },
                                   block=True)
        self.waiting_task_set_lock.acquire()
        self.waiting_task_set.add(task)
        self.waiting_task_set_lock.release()

    def task_pop(self) -> dict:
        task = self.waiting_task_list.get(block=True)
        self.waiting_task_set_lock.acquire()
        self.waiting_task_set.remove(task['task'])
        self.waiting_task_set_lock.release()
        return task

    def callback_exit(self):
        self._status = False
        self.task_push("")

    def callback_timer_add_task(self, task: str):
        self.task_push(task, True)

    def get_waiting_list(self) -> list:
        data = []
        self.waiting_task_set_lock.acquire()
        for i in self.waiting_task_set:
            data.append(i)
        self.waiting_task_set_lock.release()
        return data

    def get_processing_list(self) -> list:
        data = []
        self.processing_task_dic_lock.acquire()
        for i in self.processing_task_dic.keys():
            data.append(i)
        self.processing_task_dic_lock.release()
        return data

    def add_task(self, line: str) -> (bool, str):
        i = line.find("execute ")
        if i >= 0:
            task = line[i + len("execute "):].strip('\n').strip('\r').strip()
            self.task_push(task)
            self.server.broadcast_task_status_change(task, "waiting")
            return True, task
        return False, None

    def stop_task(self, task: str, flag=True) -> None:
        self.processing_task_dic_lock.acquire()
        if task in self.processing_task_dic.keys():
            self.server.broadcast_task_status_change(task, "stopping")
            if flag is True:
                self.processing_task_dic.pop(task)
                self.server.broadcast_task_finish(task, "finished")
                print("Task:", task, "finished")
            else:
                self.processing_task_dic[task].terminate()
                self.processing_task_dic.pop(task)
                self.server.broadcast_task_finish(task, "terminated")
                print("Task", task, "terminated")
        else:
            print("Task", task, "already removed")
        self.processing_task_dic_lock.release()
示例#8
0
 def __init__(self, token):
     self.token = token
     self.server = Server(self.token, False)
示例#9
0
文件: _client.py 项目: mvztools/slask
 def __init__(self, token):
     self.log = logging.getLogger("SLASK")
     self.token = token
     self.server = Server(self.token, False)
示例#10
0
#-------------------------------------------------------------------------------
# Copyright (c) 2014 Gael Honorez.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the GNU Public License v3.0
# which accompanies this distribution, and is available at
# http://www.gnu.org/licenses/gpl.html
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#-------------------------------------------------------------------------------


from _server import ServerMain as Server
# Initialize all important globals

SERVER_PORT = 10001

instance = Server()