コード例 #1
0
 def join_workspace(self, data, owner, workspace, dir_to_make=None):
     d = data['response']
     if dir_to_make:
         if d:
             d = dir_to_make
             utils.mkdir(d)
         else:
             d = ''
     if d == '':
         return self.get_input('Save workspace files to: ', G.PROJECT_PATH,
                               self.join_workspace, owner, workspace)
     d = os.path.realpath(os.path.expanduser(d))
     if not os.path.isdir(d):
         if dir_to_make:
             return msg.error("Couldn't create directory %s" % dir_to_make)
         prompt = '%s is not a directory. Create it? ' % d
         return self.get_input(prompt,
                               '',
                               self.join_workspace,
                               owner,
                               workspace,
                               dir_to_make=d,
                               y_or_n=True)
     try:
         G.PROJECT_PATH = d
         utils.mkdir(os.path.dirname(G.PROJECT_PATH))
         self.remote_connect(owner, workspace)
     except Exception as e:
         return msg.error("Couldn't create directory %s: %s" %
                          (G.PROJECT_PATH, str(e)))
コード例 #2
0
 def val_path(self, domain):
     dirpath = 'models/' + domain
     if utils.isdir(dirpath):
         return (dirpath)
     else:
         utils.mkdir('models/', domain)
         return dirpath
コード例 #3
0
    def create_directory(self, full_path, events_file_id, wrong_file_id=None):
        full_path = unicodedata.normalize('NFC', full_path)

        with self._storage.create_session(read_only=False,
                                          locked=True) as session:
            assert exists(dirname(full_path))
            file = self._storage.get_known_file(full_path,
                                                True,
                                                session=session)

            if file is None:
                mkdir(full_path)
                file = self._storage.get_new_file(full_path,
                                                  True,
                                                  session=session)
            elif events_file_id and file.events_file_id and \
                        file.events_file_id != events_file_id and \
                        wrong_file_id:
                logger.error("Wrong file id for %s. Expected %s. Got %s",
                             full_path, events_file_id,
                             file.events_file_id if file else None)
                raise wrong_file_id(full_path, events_file_id,
                                    file.events_file_id)

            file.events_file_id = events_file_id
            self._storage.save_file(file, session=session)
コード例 #4
0
ファイル: listener.py プロジェクト: hd-deman/floobits-sublime
def save_buf(buf):
    path = utils.get_full_path(buf['path'])
    utils.mkdir(os.path.split(path)[0])
    with open(path, 'wb') as fd:
        if buf['encoding'] == 'utf8':
            fd.write(buf['buf'].encode('utf-8'))
        else:
            fd.write(buf['buf'])
コード例 #5
0
def save_buf(buf):
    path = utils.get_full_path(buf['path'])
    utils.mkdir(os.path.split(path)[0])
    with open(path, 'wb') as fd:
        if buf['encoding'] == 'utf8':
            fd.write(buf['buf'].encode('utf-8'))
        else:
            fd.write(buf['buf'])
コード例 #6
0
 def on_rename_buf(self, data):
     new = utils.get_full_path(data['path'])
     old = utils.get_full_path(data['old_path'])
     new_dir = os.path.dirname(new)
     if new_dir:
         utils.mkdir(new_dir)
     view = self.get_view(data['id'])
     self.FLOO_BUFS[data['id']]['path'] = data['path']
     if view:
         view.rename(new)
     else:
         os.rename(old, new)
コード例 #7
0
ファイル: protocol.py プロジェクト: Wilfred/floobits-emacs
    def on_room_info(self, data):
        # Success! Reset counter
        self.workspace_info = data
        self.perms = data['perms']

        if 'patch' not in data['perms']:
            msg.log('We don\'t have patch permission. Setting buffers to read-only')

        utils.mkdir(G.PROJECT_PATH)

        floo_json = {
            'url': utils.to_workspace_url({
                'host': self.agent.host,
                'owner': self.agent.owner,
                'port': self.agent.port,
                'workspace': self.agent.workspace,
                'secure': self.agent.secure,
            })
        }
        with open(os.path.join(G.PROJECT_PATH, '.floo'), 'w') as floo_fd:
            floo_fd.write(json.dumps(floo_json, indent=4, sort_keys=True))

        for buf_id, buf in data['bufs'].iteritems():
            buf_id = int(buf_id)  # json keys must be strings
            buf_path = utils.get_full_path(buf['path'])
            new_dir = os.path.dirname(buf_path)
            utils.mkdir(new_dir)
            self.FLOO_BUFS[buf_id] = buf
            self.FLOO_PATHS_TO_BUFS[buf_path] = buf_id
            try:
                buf_fd = open(buf_path, 'r')
                buf_buf = buf_fd.read().decode('utf-8')
                md5 = hashlib.md5(buf_buf.encode('utf-8')).hexdigest()
                if md5 == buf['md5']:
                    msg.debug('md5 sums match. not getting buffer')
                    buf['buf'] = buf_buf
                else:
                    raise Exception('different md5')
            except Exception:
                try:
                    open(buf_path, "a").close()
                except Exception as e:
                    msg.debug("couldn't touch file: %s becuase %s" % (buf_path, e))
                self.agent.send_get_buf(buf_id)

        msg.debug(G.PROJECT_PATH)

        self.agent.on_auth()
コード例 #8
0
    def on_room_info(self, data):
        # Success! Reset counter
        self.workspace_info = data
        self.perms = data['perms']

        if 'patch' not in data['perms']:
            msg.log('We don\'t have patch permission. Setting buffers to read-only')

        utils.mkdir(G.PROJECT_PATH)

        floo_json = {
            'url': utils.to_workspace_url({
                'host': self.agent.host,
                'owner': self.agent.owner,
                'port': self.agent.port,
                'workspace': self.agent.workspace,
                'secure': self.agent.secure,
            })
        }
        with open(os.path.join(G.PROJECT_PATH, '.floo'), 'w') as floo_fd:
            floo_fd.write(json.dumps(floo_json, indent=4, sort_keys=True))

        for buf_id, buf in data['bufs'].iteritems():
            buf_id = int(buf_id)  # json keys must be strings
            buf_path = utils.get_full_path(buf['path'])
            new_dir = os.path.dirname(buf_path)
            utils.mkdir(new_dir)
            self.FLOO_BUFS[buf_id] = buf
            try:
                buf_fd = open(buf_path, 'r')
                buf_buf = buf_fd.read().decode('utf-8')
                md5 = hashlib.md5(buf_buf.encode('utf-8')).hexdigest()
                if md5 == buf['md5']:
                    msg.debug('md5 sums match. not getting buffer')
                    buf['buf'] = buf_buf
                else:
                    raise Exception('different md5')
            except Exception:
                try:
                    open(buf_path, "a").close()
                except Exception as e:
                    msg.debug("couldn't touch file: %s becuase %s" % (buf_path, e))
                self.agent.send_get_buf(buf_id)

        msg.debug(G.PROJECT_PATH)

        self.agent.on_auth()
コード例 #9
0
    def on_rename_buf(self, data):
        buf_id = int(data['id'])
        new = utils.get_full_path(data['path'])
        old = utils.get_full_path(data['old_path'])
        new_dir = os.path.dirname(new)
        if new_dir:
            utils.mkdir(new_dir)
        buf = self.FLOO_BUFS[buf_id]
        old_path = buf['path']
        del self.FLOO_PATHS_TO_BUFS[old_path]
        self.FLOO_PATHS_TO_BUFS[new] = buf_id
        self.FLOO_BUFS[buf_id]['path'] = new

        view = self.get_view(buf_id)
        if view:
            view.rename(new)
        else:
            os.rename(old, new)
コード例 #10
0
ファイル: emacs_handler.py プロジェクト: mattsnyder/emacsd
 def join_workspace(self, data, owner, workspace, dir_to_make=None):
     d = data['response']
     if dir_to_make:
         if d:
             d = dir_to_make
             utils.mkdir(d)
         else:
             d = ''
     if d == '':
         return self.get_input('Save workspace files to: ', G.PROJECT_PATH, self.join_workspace, owner, workspace)
     d = os.path.realpath(os.path.expanduser(d))
     if not os.path.isdir(d):
         if dir_to_make:
             return msg.error("Couldn't create directory %s" % dir_to_make)
         prompt = '%s is not a directory. Create it? ' % d
         return self.get_input(prompt, '', self.join_workspace, owner, workspace, dir_to_make=d, y_or_n=True)
     try:
         G.PROJECT_PATH = d
         utils.mkdir(os.path.dirname(G.PROJECT_PATH))
         self.remote_connect(owner, workspace)
     except Exception as e:
         return msg.error("Couldn't create directory %s: %s" % (G.PROJECT_PATH, str(e)))
コード例 #11
0
ファイル: emacs_handler.py プロジェクト: jeffsack/emacs-live
 def join_workspace(self, data, owner, workspace, dir_to_make=None):
     d = data["response"]
     if dir_to_make:
         if d:
             d = dir_to_make
             utils.mkdir(d)
         else:
             d = ""
     if d == "":
         return self.get_input(
             "Give me a directory to sync data to: ", G.PROJECT_PATH, self.join_workspace, owner, workspace
         )
     d = os.path.realpath(os.path.expanduser(d))
     if not os.path.isdir(d):
         if dir_to_make:
             return msg.error("Couldn't create directory %s" % dir_to_make)
         prompt = "%s is not a directory. Create it? " % d
         return self.get_input(prompt, "", self.join_workspace, owner, workspace, dir_to_make=d, y_or_n=True)
     try:
         G.PROJECT_PATH = d
         utils.mkdir(os.path.dirname(G.PROJECT_PATH))
         self.remote_connect(owner, workspace)
     except Exception as e:
         return msg.error("Couldn't create directory %s: %s" % (G.PROJECT_PATH, str(e)))
コード例 #12
0
def make_targets(domain=DOMAIN, targets: list = None, set='result_set'):
    """ Check if domain exists in stream. (eg. file system directory, kafka topic, etc.)
    :param domain: (str) the lexical problem space
    :param targets: (list) labels, if supervised
    :param set: (str) may be one of 'train_test_set' or 'result_set'
    :return:
    """
    if set == 'train_set':
        train_test = DATA_DIR + '/train_test'
        if not utils.dir_exists(train_test):
            utils.mkdir(train_test)
        for target in targets:
            if not utils.dir_exists(train_test + '/' + target):
                utils.mkdir(train_test + '/', target)
    elif set == 'result_set':
        results = RESULTS
        if not utils.isdir(results):
            utils.mkdir(results)
        for target in targets:
            if not utils.isdir(results + '/' + target):
                utils.mkdir(results, target)
コード例 #13
0
    def handler(self, name, data):
        if name == "patch":
            Listener.apply_patch(data)
        elif name == "get_buf":
            buf_id = data["id"]
            buf = listener.BUFS.get(buf_id)
            if not buf:
                return msg.warn("no buf found: %s.  Hopefully you didn't need that" % data)
            timeout_id = buf.get("timeout_id")
            if timeout_id:
                utils.cancel_timeout(timeout_id)

            if data["encoding"] == "base64":
                data["buf"] = base64.b64decode(data["buf"])
            # forced_patch doesn't exist in data, so this is equivalent to buf['forced_patch'] = False
            listener.BUFS[buf_id] = data
            view = listener.get_view(buf_id)
            if view:
                Listener.update_view(data, view)
            else:
                listener.save_buf(data)
        elif name == "create_buf":
            if data["encoding"] == "base64":
                data["buf"] = base64.b64decode(data["buf"])
            listener.BUFS[data["id"]] = data
            listener.PATHS_TO_IDS[data["path"]] = data["id"]
            listener.save_buf(data)
            cb = listener.CREATE_BUF_CBS.get(data["path"])
            if cb:
                del listener.CREATE_BUF_CBS[data["path"]]
                try:
                    cb(data["id"])
                except Exception as e:
                    print(e)
        elif name == "rename_buf":
            del listener.PATHS_TO_IDS[data["old_path"]]
            listener.PATHS_TO_IDS[data["path"]] = data["id"]
            new = utils.get_full_path(data["path"])
            old = utils.get_full_path(data["old_path"])
            new_dir = os.path.split(new)[0]
            if new_dir:
                utils.mkdir(new_dir)
            os.rename(old, new)
            view = listener.get_view(data["id"])
            if view:
                view.retarget(new)
            listener.BUFS[data["id"]]["path"] = data["path"]
        elif name == "delete_buf":
            path = utils.get_full_path(data["path"])
            listener.delete_buf(data["id"])
            try:
                utils.rm(path)
            except Exception:
                pass
            user_id = data.get("user_id")
            username = self.get_username_by_id(user_id)
            msg.log("%s deleted %s" % (username, path))
        elif name == "room_info":
            Listener.reset()
            G.JOINED_WORKSPACE = True
            # Success! Reset counter
            self.reconnect_delay = self.INITIAL_RECONNECT_DELAY
            self.retries = self.MAX_RETRIES

            self.workspace_info = data
            G.PERMS = data["perms"]

            if "patch" not in data["perms"]:
                msg.log("No patch permission. Setting buffers to read-only")
                if sublime.ok_cancel_dialog(
                    "You don't have permission to edit this workspace. All files will be read-only.\n\nDo you want to request edit permission?"
                ):
                    self.put({"name": "request_perms", "perms": ["edit_room"]})

            project_json = {"folders": [{"path": G.PROJECT_PATH}]}

            utils.mkdir(G.PROJECT_PATH)
            with open(os.path.join(G.PROJECT_PATH, ".sublime-project"), "wb") as project_fd:
                project_fd.write(json.dumps(project_json, indent=4, sort_keys=True).encode("utf-8"))

            floo_json = {
                "url": utils.to_workspace_url(
                    {
                        "host": self.host,
                        "owner": self.owner,
                        "port": self.port,
                        "workspace": self.workspace,
                        "secure": self.secure,
                    }
                )
            }
            with open(os.path.join(G.PROJECT_PATH, ".floo"), "w") as floo_fd:
                floo_fd.write(json.dumps(floo_json, indent=4, sort_keys=True))

            for buf_id, buf in data["bufs"].items():
                buf_id = int(buf_id)  # json keys must be strings
                buf_path = utils.get_full_path(buf["path"])
                new_dir = os.path.dirname(buf_path)
                utils.mkdir(new_dir)
                listener.BUFS[buf_id] = buf
                listener.PATHS_TO_IDS[buf["path"]] = buf_id
                # TODO: stupidly inefficient
                view = listener.get_view(buf_id)
                if view and not view.is_loading() and buf["encoding"] == "utf8":
                    view_text = listener.get_text(view)
                    view_md5 = hashlib.md5(view_text.encode("utf-8")).hexdigest()
                    if view_md5 == buf["md5"]:
                        msg.debug("md5 sum matches view. not getting buffer %s" % buf["path"])
                        buf["buf"] = view_text
                        G.VIEW_TO_HASH[view.buffer_id()] = view_md5
                    elif self.get_bufs:
                        Listener.get_buf(buf_id)
                    # TODO: maybe send patch here?
                else:
                    try:
                        buf_fd = open(buf_path, "rb")
                        buf_buf = buf_fd.read()
                        md5 = hashlib.md5(buf_buf).hexdigest()
                        if md5 == buf["md5"]:
                            msg.debug("md5 sum matches. not getting buffer %s" % buf["path"])
                            if buf["encoding"] == "utf8":
                                buf_buf = buf_buf.decode("utf-8")
                            buf["buf"] = buf_buf
                        elif self.get_bufs:
                            Listener.get_buf(buf_id)
                    except Exception as e:
                        msg.debug("Error calculating md5:", e)
                        Listener.get_buf(buf_id)

            msg.log("Successfully joined workspace %s/%s" % (self.owner, self.workspace))

            temp_data = data.get("temp_data", {})
            hangout = temp_data.get("hangout", {})
            hangout_url = hangout.get("url")
            if hangout_url:
                self.prompt_join_hangout(hangout_url)

            if self.on_room_info:
                self.on_room_info()
                self.on_room_info = None
        elif name == "user_info":
            user_id = str(data["user_id"])
            user_info = data["user_info"]
            self.workspace_info["users"][user_id] = user_info
            if user_id == str(self.workspace_info["user_id"]):
                G.PERMS = user_info["perms"]
        elif name == "join":
            msg.log("%s joined the workspace" % data["username"])
            user_id = str(data["user_id"])
            self.workspace_info["users"][user_id] = data
        elif name == "part":
            msg.log("%s left the workspace" % data["username"])
            user_id = str(data["user_id"])
            try:
                del self.workspace_info["users"][user_id]
            except Exception as e:
                print("Unable to delete user %s from user list" % (data))
            region_key = "floobits-highlight-%s" % (user_id)
            for window in sublime.windows():
                for view in window.views():
                    view.erase_regions(region_key)
        elif name == "highlight":
            region_key = "floobits-highlight-%s" % (data["user_id"])
            Listener.highlight(data["id"], region_key, data["username"], data["ranges"], data.get("ping", False))
        elif name == "set_temp_data":
            hangout_data = data.get("data", {})
            hangout = hangout_data.get("hangout", {})
            hangout_url = hangout.get("url")
            if hangout_url:
                self.prompt_join_hangout(hangout_url)
        elif name == "saved":
            try:
                buf = listener.BUFS[data["id"]]
                username = self.get_username_by_id(data["user_id"])
                msg.log("%s saved buffer %s" % (username, buf["path"]))
            except Exception as e:
                msg.error(str(e))
        elif name == "request_perms":
            print(data)
            user_id = str(data.get("user_id"))
            username = self.get_username_by_id(user_id)
            if not username:
                return msg.debug("Unknown user for id %s. Not handling request_perms event." % user_id)
            perm_mapping = {"edit_room": "edit", "admin_room": "admin"}
            perms = data.get("perms")
            perms_str = "".join([perm_mapping.get(p) for p in perms])
            prompt = "User %s is requesting %s permission for this room." % (username, perms_str)
            message = data.get("message")
            if message:
                prompt += "\n\n%s says: %s" % (username, message)
            prompt += "\n\nDo you want to grant them permission?"
            confirm = bool(sublime.ok_cancel_dialog(prompt))
            if confirm:
                action = "add"
            else:
                action = "reject"
            self.put({"name": "perms", "action": action, "user_id": user_id, "perms": perms})
        elif name == "perms":
            action = data["action"]
            user_id = str(data["user_id"])
            user = self.workspace_info["users"].get(user_id)
            if user is None:
                msg.log("No user for id %s. Not handling perms event" % user_id)
                return
            perms = set(user["perms"])
            if action == "add":
                perms |= set(data["perms"])
            elif action == "remove":
                perms -= set(data["perms"])
            else:
                return
            user["perms"] = list(perms)
            if user_id == self.workspace_info["user_id"]:
                G.PERMS = perms
        elif name == "msg":
            self.on_msg(data)
        else:
            msg.debug("unknown name!", name, "data:", data)
コード例 #14
0
    def __init__(self):
        """ settings logging
        """
        """
            第一步,初始化 log 目录
        """
        day_time = time.strftime('%Y-%m-%d',
                                 time.localtime(time.time()))  # 2020-04-20
        minutes_time = time.strftime('%Y-%m-%d-%H-%M',
                                     time.localtime(time.time()))  # 2020-04-20

        path = os.path.dirname(os.path.dirname(
            os.path.abspath(__file__)))  # 项目路径

        log_folder = "{}/log".format(path)
        log_child_folder = "{}/log/{}".format(path, day_time)

        mkdir(log_folder)
        # mkdir(log_child_folder)

        # log - 2020-04-30 - 2020-04-30-14-20.log
        # log_file = "{}/{}.log".format(log_child_folder, minutes_time)
        # log_err_file = "{}/{}_error.log".format(log_child_folder, minutes_time)

        # log - 2020-04-30.log 因为按分钟写入log文件太频繁,改成按天写入log 并取消二级目录
        log_file = "{}/{}.log".format(log_folder, day_time)
        """
            第二步,创建一个handler,用于写入全部info日志文件
        """
        # a 代表继续写log,不覆盖之前log
        # w 代表重新写入,覆盖之前log
        file_handler = logging.FileHandler(log_file, mode='a+')
        file_handler.setLevel(logging.DEBUG)
        """
            第三步,创建一个handler,用于写入错误日志文件
        """
        # 由于 error log 文件只写入错误行内容,已经在log文件内覆盖,所以没必要
        # error_file_handler = logging.FileHandler(log_err_file, mode='a+')
        # error_file_handler.setLevel(logging.ERROR)
        """
            第四步,再创建一个handler,用于输出到控制台
        """
        stdout_handler = logging.StreamHandler(sys.stdout)
        stdout_handler.setLevel(logging.DEBUG)
        """
            第五步,定义handler的输出格式
        """
        formatter = logging.Formatter(
            "[%(levelname)s - %(asctime)s - %(filename)s] : %(message)s")
        file_handler.setFormatter(formatter)
        # error_file_handler.setFormatter(formatter)
        stdout_handler.setFormatter(formatter)
        """
            第六步,将logger添加到handler里面
        """
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)
        logger.handlers = []

        logger.addHandler(file_handler)
        # logger.addHandler(error_file_handler)
        logger.addHandler(stdout_handler)
コード例 #15
0
                # 2. 生成 validator 成功后,取消掉注释,即可正常运行断言器
                # validate.run_validator(validator, result)
                
    def test_case_02(self):
        pytest.skip("此为测试启动方法, 不执行")
                

if __name__ == '__main__':

    pytest.main([__file__])

"""

    # 创建模块目录
    feature_path = os.path.join(path.case_path(), feature)
    utils.mkdir(feature_path)

    # 创建用例文件
    test_path = os.path.join(feature_path,
                             "test_{}.py".format(api_name.split(".")[0]))
    utils.create_file(test_path, case)


def gen_all_case():
    """ 生成 config/all_api.yml 内所有 case
    """
    all_api = parser_yaml.load_yaml(
        os.path.join(path.config_path(), "all_api.yml"))
    for k, v in all_api.items():
        if v:  # 判断 feature 不为空
            api_items = str(v).split(" ")
コード例 #16
0
ファイル: emacs_handler.py プロジェクト: jeffsack/emacs-live
    def _on_share_dir(self, data):
        utils.reload_settings()
        G.USERNAME = data["username"]
        G.SECRET = data["secret"]
        dir_to_share = data["dir_to_share"]
        perms = data.get("perms")
        dir_to_share = os.path.expanduser(dir_to_share)
        dir_to_share = utils.unfuck_path(dir_to_share)
        workspace_name = os.path.basename(dir_to_share)
        G.PROJECT_PATH = os.path.realpath(dir_to_share)
        msg.debug("%s %s %s" % (G.USERNAME, workspace_name, G.PROJECT_PATH))

        if os.path.isfile(dir_to_share):
            return msg.error("%s is a file. Give me a directory please." % dir_to_share)

        try:
            utils.mkdir(dir_to_share)
        except Exception:
            return msg.error("The directory %s doesn't exist and I can't make it." % dir_to_share)

        floo_file = os.path.join(dir_to_share, ".floo")

        info = {}
        try:
            floo_info = open(floo_file, "rb").read().decode("utf-8")
            info = json.loads(floo_info)
        except (IOError, OSError):
            pass
        except Exception:
            msg.debug("Couldn't read the floo_info file: %s" % floo_file)

        workspace_url = info.get("url")
        if workspace_url:
            try:
                result = utils.parse_url(workspace_url)
            except Exception as e:
                msg.error(str(e))
            else:
                workspace_name = result["workspace"]
                try:
                    # TODO: blocking. beachballs sublime 2 if API is super slow
                    api.get_workspace_by_url(workspace_url)
                except HTTPError:
                    workspace_url = None
                    workspace_name = os.path.basename(dir_to_share)
                else:
                    utils.add_workspace_to_persistent_json(
                        result["owner"], result["workspace"], workspace_url, dir_to_share
                    )

        workspace_url = utils.get_workspace_by_path(dir_to_share) or workspace_url

        if workspace_url:
            try:
                api.get_workspace_by_url(workspace_url)
            except HTTPError:
                pass
            else:
                result = utils.parse_url(workspace_url)
                agent = self.remote_connect(result["owner"], result["workspace"], False)
                return agent.once("room_info", lambda: agent.upload(dir_to_share))

        def on_done(data, choices=None):
            self._on_create_workspace({}, workspace_name, dir_to_share, owner=data.get("response"), perms=perms)

        orgs = api.get_orgs_can_admin()
        orgs = json.loads(orgs.read().decode("utf-8"))
        if len(orgs) == 0:
            return on_done({"response": G.USERNAME})
        i = 0
        choices = []
        choices.append([G.USERNAME, i])
        for o in orgs:
            i += 1
            choices.append([o["name"], i])

        self.get_input("Create workspace for (%s) " % " ".join([x[0] for x in choices]), "", on_done, choices=choices)
コード例 #17
0
    def _on_share_dir(self, data):
        file_to_share = None
        utils.reload_settings()
        G.USERNAME = data['username']
        G.SECRET = data['secret']
        dir_to_share = data['dir_to_share']
        perms = data['perms']
        editor.line_endings = data['line_endings'].find(
            "unix") >= 0 and "\n" or "\r\n"
        dir_to_share = os.path.expanduser(dir_to_share)
        dir_to_share = utils.unfuck_path(dir_to_share)
        workspace_name = os.path.basename(dir_to_share)
        G.PROJECT_PATH = os.path.realpath(dir_to_share)
        msg.debug('%s %s %s' % (G.USERNAME, workspace_name, G.PROJECT_PATH))

        if os.path.isfile(dir_to_share):
            file_to_share = dir_to_share
            dir_to_share = os.path.dirname(dir_to_share)

        try:
            utils.mkdir(dir_to_share)
        except Exception:
            return msg.error(
                "The directory %s doesn't exist and I can't create it." %
                dir_to_share)

        floo_file = os.path.join(dir_to_share, '.floo')

        info = {}
        try:
            floo_info = open(floo_file, 'rb').read().decode('utf-8')
            info = json.loads(floo_info)
        except (IOError, OSError):
            pass
        except Exception as e:
            msg.warn("Couldn't read .floo file: %s: %s" % (floo_file, str(e)))

        workspace_url = info.get('url')
        if workspace_url:
            parsed_url = api.prejoin_workspace(workspace_url, dir_to_share,
                                               {'perms': perms})
            if parsed_url:
                # TODO: make sure we create_flooignore
                # utils.add_workspace_to_persistent_json(parsed_url['owner'], parsed_url['workspace'], workspace_url, dir_to_share)
                agent = self.remote_connect(parsed_url['owner'],
                                            parsed_url['workspace'], False)
                return agent.once(
                    "room_info",
                    lambda: agent.upload(file_to_share or dir_to_share))

        parsed_url = utils.get_workspace_by_path(
            dir_to_share, lambda workspace_url: api.prejoin_workspace(
                workspace_url, dir_to_share, {'perms': perms}))
        if parsed_url:
            agent = self.remote_connect(parsed_url['owner'],
                                        parsed_url['workspace'], False)
            return agent.once(
                "room_info",
                lambda: agent.upload(file_to_share or dir_to_share))

        def on_done(data, choices=None):
            self.get_input('Workspace name:',
                           workspace_name,
                           self._on_create_workspace,
                           workspace_name,
                           dir_to_share,
                           owner=data.get('response'),
                           perms=perms)

        try:
            r = api.get_orgs_can_admin()
        except IOError as e:
            return editor.error_message('Error getting org list: %s' % str(e))
        if r.code >= 400 or len(r.body) == 0:
            return on_done({'response': G.USERNAME})
        i = 0
        choices = []
        choices.append([G.USERNAME, i])
        for org in r.body:
            i += 1
            choices.append([org['name'], i])

        self.get_input('Create workspace owned by (%s) ' %
                       " ".join([x[0] for x in choices]),
                       '',
                       on_done,
                       choices=choices)
コード例 #18
0
    def on_room_info(self, data):
        # Success! Reset counter
        self.workspace_info = data
        self.perms = data['perms']

        if 'patch' not in data['perms']:
            msg.log('We don\'t have patch permission. Setting buffers to read-only')

        utils.mkdir(G.PROJECT_PATH)
        msg.debug('mkdir -p for project path %s' % G.PROJECT_PATH)

        floo_json = {
            'url': utils.to_workspace_url({
                'host': self.agent.host,
                'owner': self.agent.owner,
                'port': self.agent.port,
                'workspace': self.agent.workspace,
                'secure': self.agent.secure,
            })
        }
        utils.update_floo_file(os.path.join(G.PROJECT_PATH, '.floo'), floo_json)

        G.JOINED_WORKSPACE = True

        bufs_to_get = []
        new_dirs = set()
        for buf_id, buf in data['bufs'].items():
            buf_id = int(buf_id)  # json keys must be strings
            buf_path = utils.get_full_path(buf['path'])
            new_dir = os.path.dirname(buf_path)
            if new_dir not in new_dirs:
                utils.mkdir(new_dir)
                new_dirs.add(new_dir)
            self.FLOO_BUFS[buf_id] = buf
            self.FLOO_PATHS_TO_BUFS[buf['path']] = buf_id

            try:
                buf_fd = open(buf_path, 'rb')
            except Exception as e:
                msg.debug("Couldn't read %s: %s" % (buf_path, e))
                bufs_to_get.append(buf_id)
                continue
            else:
                buf_buf = buf_fd.read()
            md5 = hashlib.md5(buf_buf).hexdigest()
            if md5 == buf['md5']:
                msg.debug('md5 sums match- not fetching buffer %s' % buf_path)
                if buf['encoding'] == 'utf8':
                    buf['buf'] = buf_buf.decode('utf-8')
                continue
            bufs_to_get.append(buf_id)

        def finish_room_info():
            success_msg = 'Successfully joined workspace %s/%s' % (self.agent.owner, self.agent.workspace)
            msg.log(success_msg)
            self.agent.on_auth()

        if bufs_to_get and self.agent.get_bufs:
            if len(bufs_to_get) > 4:
                prompt = '%s local files are different from the workspace. Overwrite your local files?' % len(bufs_to_get)
            else:
                prompt = 'Overwrite the following local files: %s ? ' % " ".join([self.FLOO_BUFS[buf_id]['path']
                         for buf_id in bufs_to_get])

            def stomp_local(data):
                d = data['response']
                for buf_id in bufs_to_get:
                    if d:
                        self.agent.send_get_buf(buf_id)
                    else:
                        buf = self.FLOO_BUFS[buf_id]
                        # TODO: this is inefficient. we just read the file 20 lines ago
                        self.create_buf(utils.get_full_path(buf['path']))
                finish_room_info()
            self.agent.conn.get_input(prompt, '', stomp_local, y_or_n=True)
        else:
            finish_room_info()
コード例 #19
0
ファイル: emacs_handler.py プロジェクト: nabacg/emacs-live
    def _on_share_dir(self, data):
        utils.reload_settings()
        G.USERNAME = data['username']
        G.SECRET = data['secret']
        dir_to_share = data['dir_to_share']
        perms = data.get('perms')
        dir_to_share = os.path.expanduser(dir_to_share)
        dir_to_share = utils.unfuck_path(dir_to_share)
        workspace_name = os.path.basename(dir_to_share)
        G.PROJECT_PATH = os.path.realpath(dir_to_share)
        msg.debug('%s %s %s' % (G.USERNAME, workspace_name, G.PROJECT_PATH))

        if os.path.isfile(dir_to_share):
            return msg.error('%s is a file. Give me a directory please.' %
                             dir_to_share)

        try:
            utils.mkdir(dir_to_share)
        except Exception:
            return msg.error(
                "The directory %s doesn't exist and I can't make it." %
                dir_to_share)

        floo_file = os.path.join(dir_to_share, '.floo')

        info = {}
        try:
            floo_info = open(floo_file, 'rb').read().decode('utf-8')
            info = json.loads(floo_info)
        except (IOError, OSError):
            pass
        except Exception:
            msg.debug("Couldn't read the floo_info file: %s" % floo_file)

        workspace_url = info.get('url')
        if workspace_url:
            try:
                result = utils.parse_url(workspace_url)
            except Exception as e:
                msg.error(str(e))
            else:
                workspace_name = result['workspace']
                try:
                    # TODO: blocking. beachballs sublime 2 if API is super slow
                    api.get_workspace_by_url(workspace_url)
                except HTTPError:
                    workspace_url = None
                    workspace_name = os.path.basename(dir_to_share)
                else:
                    utils.add_workspace_to_persistent_json(
                        result['owner'], result['workspace'], workspace_url,
                        dir_to_share)

        workspace_url = utils.get_workspace_by_path(
            dir_to_share) or workspace_url

        if workspace_url:
            try:
                api.get_workspace_by_url(workspace_url)
            except HTTPError:
                pass
            else:
                result = utils.parse_url(workspace_url)
                agent = self.remote_connect(result['owner'],
                                            result['workspace'], False)
                return agent.once("room_info",
                                  lambda: agent.upload(dir_to_share))

        def on_done(data, choices=None):
            self._on_create_workspace({},
                                      workspace_name,
                                      dir_to_share,
                                      owner=data.get('response'),
                                      perms=perms)

        orgs = api.get_orgs_can_admin()
        orgs = json.loads(orgs.read().decode('utf-8'))
        if len(orgs) == 0:
            return on_done({'response': G.USERNAME})
        i = 0
        choices = []
        choices.append([G.USERNAME, i])
        for o in orgs:
            i += 1
            choices.append([o['name'], i])

        self.get_input('Create workspace for (%s) ' %
                       " ".join([x[0] for x in choices]),
                       '',
                       on_done,
                       choices=choices)
コード例 #20
0
ファイル: emacs_handler.py プロジェクト: mattsnyder/emacsd
    def _on_share_dir(self, data):
        file_to_share = None
        utils.reload_settings()
        G.USERNAME = data['username']
        G.SECRET = data['secret']
        dir_to_share = data['dir_to_share']
        perms = data['perms']
        editor.line_endings = data['line_endings'].find("unix") >= 0 and "\n" or "\r\n"
        dir_to_share = os.path.expanduser(dir_to_share)
        dir_to_share = utils.unfuck_path(dir_to_share)
        workspace_name = os.path.basename(dir_to_share)
        G.PROJECT_PATH = os.path.realpath(dir_to_share)
        msg.debug('%s %s %s' % (G.USERNAME, workspace_name, G.PROJECT_PATH))

        if os.path.isfile(dir_to_share):
            file_to_share = dir_to_share
            dir_to_share = os.path.dirname(dir_to_share)

        try:
            utils.mkdir(dir_to_share)
        except Exception:
            return msg.error("The directory %s doesn't exist and I can't create it." % dir_to_share)

        floo_file = os.path.join(dir_to_share, '.floo')

        info = {}
        try:
            floo_info = open(floo_file, 'rb').read().decode('utf-8')
            info = json.loads(floo_info)
        except (IOError, OSError):
            pass
        except Exception as e:
            msg.warn("Couldn't read .floo file: %s: %s" % (floo_file, str(e)))

        workspace_url = info.get('url')
        if workspace_url:
            parsed_url = api.prejoin_workspace(workspace_url, dir_to_share, {'perms': perms})
            if parsed_url:
                # TODO: make sure we create_flooignore
                # utils.add_workspace_to_persistent_json(parsed_url['owner'], parsed_url['workspace'], workspace_url, dir_to_share)
                agent = self.remote_connect(parsed_url['owner'], parsed_url['workspace'], False)
                return agent.once("room_info", lambda: agent.upload(file_to_share or dir_to_share))

        parsed_url = utils.get_workspace_by_path(dir_to_share,
                                                 lambda workspace_url: api.prejoin_workspace(workspace_url, dir_to_share, {'perms': perms}))
        if parsed_url:
            agent = self.remote_connect(parsed_url['owner'], parsed_url['workspace'], False)
            return agent.once("room_info", lambda: agent.upload(file_to_share or dir_to_share))

        def on_done(data, choices=None):
            self.get_input('Workspace name:',
                           workspace_name,
                           self._on_create_workspace,
                           workspace_name,
                           dir_to_share,
                           owner=data.get('response'),
                           perms=perms)

        try:
            r = api.get_orgs_can_admin()
        except IOError as e:
            return editor.error_message('Error getting org list: %s' % str(e))
        if r.code >= 400 or len(r.body) == 0:
            return on_done({'response': G.USERNAME})
        i = 0
        choices = []
        choices.append([G.USERNAME, i])
        for org in r.body:
            i += 1
            choices.append([org['name'], i])

        self.get_input('Create workspace owned by (%s) ' % " ".join([x[0] for x in choices]), '', on_done, choices=choices)
コード例 #21
0
    def handler(self, name, data):
        if name == 'patch':
            Listener.apply_patch(data)
        elif name == 'get_buf':
            buf_id = data['id']
            buf = listener.BUFS.get(buf_id)
            if not buf:
                return msg.warn(
                    'no buf found: %s.  Hopefully you didn\'t need that' %
                    data)
            timeout_id = buf.get('timeout_id')
            if timeout_id:
                utils.cancel_timeout(timeout_id)

            if data['encoding'] == 'base64':
                data['buf'] = base64.b64decode(data['buf'])
            # forced_patch doesn't exist in data, so this is equivalent to buf['forced_patch'] = False
            listener.BUFS[buf_id] = data
            view = listener.get_view(buf_id)
            if view:
                Listener.update_view(data, view)
            else:
                listener.save_buf(data)
        elif name == 'create_buf':
            if data['encoding'] == 'base64':
                data['buf'] = base64.b64decode(data['buf'])
            listener.BUFS[data['id']] = data
            listener.PATHS_TO_IDS[data['path']] = data['id']
            listener.save_buf(data)
            cb = listener.CREATE_BUF_CBS.get(data['path'])
            if cb:
                del listener.CREATE_BUF_CBS[data['path']]
                try:
                    cb(data['id'])
                except Exception as e:
                    print(e)
        elif name == 'rename_buf':
            del listener.PATHS_TO_IDS[data['old_path']]
            listener.PATHS_TO_IDS[data['path']] = data['id']
            new = utils.get_full_path(data['path'])
            old = utils.get_full_path(data['old_path'])
            new_dir = os.path.split(new)[0]
            if new_dir:
                utils.mkdir(new_dir)
            os.rename(old, new)
            view = listener.get_view(data['id'])
            if view:
                view.retarget(new)
            listener.BUFS[data['id']]['path'] = data['path']
        elif name == 'delete_buf':
            path = utils.get_full_path(data['path'])
            listener.delete_buf(data['id'])
            try:
                utils.rm(path)
            except Exception:
                pass
            user_id = data.get('user_id')
            username = self.get_username_by_id(user_id)
            msg.log('%s deleted %s' % (username, path))
        elif name == 'room_info':
            Listener.reset()
            G.JOINED_WORKSPACE = True
            # Success! Reset counter
            self.reconnect_delay = self.INITIAL_RECONNECT_DELAY
            self.retries = self.MAX_RETRIES

            self.workspace_info = data
            G.PERMS = data['perms']

            if 'patch' not in data['perms']:
                msg.log('No patch permission. Setting buffers to read-only')
                if sublime.ok_cancel_dialog(
                        'You don\'t have permission to edit this workspace. All files will be read-only.\n\nDo you want to request edit permission?'
                ):
                    self.put({'name': 'request_perms', 'perms': ['edit_room']})

            project_json = {'folders': [{'path': G.PROJECT_PATH}]}

            utils.mkdir(G.PROJECT_PATH)
            with open(os.path.join(G.PROJECT_PATH, '.sublime-project'),
                      'wb') as project_fd:
                project_fd.write(
                    json.dumps(project_json, indent=4,
                               sort_keys=True).encode('utf-8'))

            floo_json = {
                'url':
                utils.to_workspace_url({
                    'host': self.host,
                    'owner': self.owner,
                    'port': self.port,
                    'workspace': self.workspace,
                    'secure': self.secure,
                })
            }
            with open(os.path.join(G.PROJECT_PATH, '.floo'), 'w') as floo_fd:
                floo_fd.write(json.dumps(floo_json, indent=4, sort_keys=True))

            for buf_id, buf in data['bufs'].items():
                buf_id = int(buf_id)  # json keys must be strings
                buf_path = utils.get_full_path(buf['path'])
                new_dir = os.path.dirname(buf_path)
                utils.mkdir(new_dir)
                listener.BUFS[buf_id] = buf
                listener.PATHS_TO_IDS[buf['path']] = buf_id
                # TODO: stupidly inefficient
                view = listener.get_view(buf_id)
                if view and not view.is_loading(
                ) and buf['encoding'] == 'utf8':
                    view_text = listener.get_text(view)
                    view_md5 = hashlib.md5(
                        view_text.encode('utf-8')).hexdigest()
                    if view_md5 == buf['md5']:
                        msg.debug(
                            'md5 sum matches view. not getting buffer %s' %
                            buf['path'])
                        buf['buf'] = view_text
                        G.VIEW_TO_HASH[view.buffer_id()] = view_md5
                    elif self.get_bufs:
                        Listener.get_buf(buf_id)
                    #TODO: maybe send patch here?
                else:
                    try:
                        buf_fd = open(buf_path, 'rb')
                        buf_buf = buf_fd.read()
                        md5 = hashlib.md5(buf_buf).hexdigest()
                        if md5 == buf['md5']:
                            msg.debug(
                                'md5 sum matches. not getting buffer %s' %
                                buf['path'])
                            if buf['encoding'] == 'utf8':
                                buf_buf = buf_buf.decode('utf-8')
                            buf['buf'] = buf_buf
                        elif self.get_bufs:
                            Listener.get_buf(buf_id)
                    except Exception as e:
                        msg.debug('Error calculating md5:', e)
                        Listener.get_buf(buf_id)

            msg.log('Successfully joined workspace %s/%s' %
                    (self.owner, self.workspace))

            temp_data = data.get('temp_data', {})
            hangout = temp_data.get('hangout', {})
            hangout_url = hangout.get('url')
            if hangout_url:
                self.prompt_join_hangout(hangout_url)

            if self.on_room_info:
                self.on_room_info()
                self.on_room_info = None
        elif name == 'user_info':
            user_id = str(data['user_id'])
            user_info = data['user_info']
            self.workspace_info['users'][user_id] = user_info
            if user_id == str(self.workspace_info['user_id']):
                G.PERMS = user_info['perms']
        elif name == 'join':
            msg.log('%s joined the workspace' % data['username'])
            user_id = str(data['user_id'])
            self.workspace_info['users'][user_id] = data
        elif name == 'part':
            msg.log('%s left the workspace' % data['username'])
            user_id = str(data['user_id'])
            try:
                del self.workspace_info['users'][user_id]
            except Exception as e:
                print('Unable to delete user %s from user list' % (data))
            region_key = 'floobits-highlight-%s' % (user_id)
            for window in sublime.windows():
                for view in window.views():
                    view.erase_regions(region_key)
        elif name == 'highlight':
            region_key = 'floobits-highlight-%s' % (data['user_id'])
            Listener.highlight(data['id'], region_key, data['username'],
                               data['ranges'], data.get('ping', False))
        elif name == 'set_temp_data':
            hangout_data = data.get('data', {})
            hangout = hangout_data.get('hangout', {})
            hangout_url = hangout.get('url')
            if hangout_url:
                self.prompt_join_hangout(hangout_url)
        elif name == 'saved':
            try:
                buf = listener.BUFS[data['id']]
                username = self.get_username_by_id(data['user_id'])
                msg.log('%s saved buffer %s' % (username, buf['path']))
            except Exception as e:
                msg.error(str(e))
        elif name == 'request_perms':
            print(data)
            user_id = str(data.get('user_id'))
            username = self.get_username_by_id(user_id)
            if not username:
                return msg.debug(
                    'Unknown user for id %s. Not handling request_perms event.'
                    % user_id)
            perm_mapping = {
                'edit_room': 'edit',
                'admin_room': 'admin',
            }
            perms = data.get('perms')
            perms_str = ''.join([perm_mapping.get(p) for p in perms])
            prompt = 'User %s is requesting %s permission for this room.' % (
                username, perms_str)
            message = data.get('message')
            if message:
                prompt += '\n\n%s says: %s' % (username, message)
            prompt += '\n\nDo you want to grant them permission?'
            confirm = bool(sublime.ok_cancel_dialog(prompt))
            if confirm:
                action = 'add'
            else:
                action = 'reject'
            self.put({
                'name': 'perms',
                'action': action,
                'user_id': user_id,
                'perms': perms
            })
        elif name == 'perms':
            action = data['action']
            user_id = str(data['user_id'])
            user = self.workspace_info['users'].get(user_id)
            if user is None:
                msg.log('No user for id %s. Not handling perms event' %
                        user_id)
                return
            perms = set(user['perms'])
            if action == 'add':
                perms |= set(data['perms'])
            elif action == 'remove':
                perms -= set(data['perms'])
            else:
                return
            user['perms'] = list(perms)
            if user_id == self.workspace_info['user_id']:
                G.PERMS = perms
        elif name == 'msg':
            self.on_msg(data)
        else:
            msg.debug('unknown name!', name, 'data:', data)
コード例 #22
0
ファイル: vim_protocol.py プロジェクト: silky/floobits-vim
 def save_buf(self, buf):
     path = utils.get_full_path(buf['path'])
     utils.mkdir(os.path.split(path)[0])
     with open(path, 'wb') as fd:
         fd.write(buf['buf'].encode('utf-8'))
     return path
コード例 #23
0
    def handler(self, name, data):
        if name == 'patch':
            Listener.apply_patch(data)
        elif name == 'get_buf':
            buf_id = data['id']
            buf = listener.BUFS.get(buf_id)
            if not buf:
                return msg.warn('no buf found: %s.  Hopefully you didn\'t need that' % data)
            timeout_id = buf.get('timeout_id')
            if timeout_id:
                utils.cancel_timeout(timeout_id)

            if data['encoding'] == 'base64':
                data['buf'] = base64.b64decode(data['buf'])
            # forced_patch doesn't exist in data, so this is equivalent to buf['forced_patch'] = False
            listener.BUFS[buf_id] = data
            view = listener.get_view(buf_id)
            if view:
                Listener.update_view(data, view)
            else:
                listener.save_buf(data)
        elif name == 'create_buf':
            if data['encoding'] == 'base64':
                data['buf'] = base64.b64decode(data['buf'])
            listener.BUFS[data['id']] = data
            listener.PATHS_TO_IDS[data['path']] = data['id']
            listener.save_buf(data)
            cb = listener.CREATE_BUF_CBS.get(data['path'])
            if cb:
                del listener.CREATE_BUF_CBS[data['path']]
                try:
                    cb(data['id'])
                except Exception as e:
                    print(e)
        elif name == 'rename_buf':
            del listener.PATHS_TO_IDS[data['old_path']]
            listener.PATHS_TO_IDS[data['path']] = data['id']
            new = utils.get_full_path(data['path'])
            old = utils.get_full_path(data['old_path'])
            new_dir = os.path.split(new)[0]
            if new_dir:
                utils.mkdir(new_dir)
            os.rename(old, new)
            view = listener.get_view(data['id'])
            if view:
                view.retarget(new)
            listener.BUFS[data['id']]['path'] = data['path']
        elif name == 'delete_buf':
            path = utils.get_full_path(data['path'])
            listener.delete_buf(data['id'])
            try:
                utils.rm(path)
            except Exception:
                pass
            user_id = data.get('user_id')
            username = self.get_username_by_id(user_id)
            msg.log('%s deleted %s' % (username, path))
        elif name == 'room_info':
            Listener.reset()
            G.JOINED_WORKSPACE = True
            # Success! Reset counter
            self.reconnect_delay = self.INITIAL_RECONNECT_DELAY
            self.retries = self.MAX_RETRIES

            self.workspace_info = data
            G.PERMS = data['perms']

            if 'patch' not in data['perms']:
                msg.log('No patch permission. Setting buffers to read-only')
                if sublime.ok_cancel_dialog('You don\'t have permission to edit this workspace. All files will be read-only.\n\nDo you want to request edit permission?'):
                    self.put({'name': 'request_perms', 'perms': ['edit_room']})

            project_json = {
                'folders': [
                    {'path': G.PROJECT_PATH}
                ]
            }

            utils.mkdir(G.PROJECT_PATH)
            with open(os.path.join(G.PROJECT_PATH, '.sublime-project'), 'wb') as project_fd:
                project_fd.write(json.dumps(project_json, indent=4, sort_keys=True).encode('utf-8'))

            floo_json = {
                'url': utils.to_workspace_url({
                    'host': self.host,
                    'owner': self.owner,
                    'port': self.port,
                    'workspace': self.workspace,
                    'secure': self.secure,
                })
            }
            with open(os.path.join(G.PROJECT_PATH, '.floo'), 'w') as floo_fd:
                floo_fd.write(json.dumps(floo_json, indent=4, sort_keys=True))

            for buf_id, buf in data['bufs'].items():
                buf_id = int(buf_id)  # json keys must be strings
                buf_path = utils.get_full_path(buf['path'])
                new_dir = os.path.dirname(buf_path)
                utils.mkdir(new_dir)
                listener.BUFS[buf_id] = buf
                listener.PATHS_TO_IDS[buf['path']] = buf_id
                # TODO: stupidly inefficient
                view = listener.get_view(buf_id)
                if view and not view.is_loading() and buf['encoding'] == 'utf8':
                    view_text = listener.get_text(view)
                    view_md5 = hashlib.md5(view_text.encode('utf-8')).hexdigest()
                    if view_md5 == buf['md5']:
                        msg.debug('md5 sum matches view. not getting buffer %s' % buf['path'])
                        buf['buf'] = view_text
                        G.VIEW_TO_HASH[view.buffer_id()] = view_md5
                    elif self.get_bufs:
                        Listener.get_buf(buf_id)
                    #TODO: maybe send patch here?
                else:
                    try:
                        buf_fd = open(buf_path, 'rb')
                        buf_buf = buf_fd.read()
                        md5 = hashlib.md5(buf_buf).hexdigest()
                        if md5 == buf['md5']:
                            msg.debug('md5 sum matches. not getting buffer %s' % buf['path'])
                            if buf['encoding'] == 'utf8':
                                buf_buf = buf_buf.decode('utf-8')
                            buf['buf'] = buf_buf
                        elif self.get_bufs:
                            Listener.get_buf(buf_id)
                    except Exception as e:
                        msg.debug('Error calculating md5:', e)
                        Listener.get_buf(buf_id)

            msg.log('Successfully joined workspace %s/%s' % (self.owner, self.workspace))

            temp_data = data.get('temp_data', {})
            hangout = temp_data.get('hangout', {})
            hangout_url = hangout.get('url')
            if hangout_url:
                self.prompt_join_hangout(hangout_url)

            if self.on_room_info:
                self.on_room_info()
                self.on_room_info = None
        elif name == 'user_info':
            user_id = str(data['user_id'])
            user_info = data['user_info']
            self.workspace_info['users'][user_id] = user_info
            if user_id == str(self.workspace_info['user_id']):
                G.PERMS = user_info['perms']
        elif name == 'join':
            msg.log('%s joined the workspace' % data['username'])
            user_id = str(data['user_id'])
            self.workspace_info['users'][user_id] = data
        elif name == 'part':
            msg.log('%s left the workspace' % data['username'])
            user_id = str(data['user_id'])
            try:
                del self.workspace_info['users'][user_id]
            except Exception as e:
                print('Unable to delete user %s from user list' % (data))
            region_key = 'floobits-highlight-%s' % (user_id)
            for window in sublime.windows():
                for view in window.views():
                    view.erase_regions(region_key)
        elif name == 'highlight':
            region_key = 'floobits-highlight-%s' % (data['user_id'])
            Listener.highlight(data['id'], region_key, data['username'], data['ranges'], data.get('ping', False))
        elif name == 'set_temp_data':
            hangout_data = data.get('data', {})
            hangout = hangout_data.get('hangout', {})
            hangout_url = hangout.get('url')
            if hangout_url:
                self.prompt_join_hangout(hangout_url)
        elif name == 'saved':
            try:
                buf = listener.BUFS[data['id']]
                username = self.get_username_by_id(data['user_id'])
                msg.log('%s saved buffer %s' % (username, buf['path']))
            except Exception as e:
                msg.error(str(e))
        elif name == 'request_perms':
            print(data)
            user_id = str(data.get('user_id'))
            username = self.get_username_by_id(user_id)
            if not username:
                return msg.debug('Unknown user for id %s. Not handling request_perms event.' % user_id)
            perm_mapping = {
                'edit_room': 'edit',
                'admin_room': 'admin',
            }
            perms = data.get('perms')
            perms_str = ''.join([perm_mapping.get(p) for p in perms])
            prompt = 'User %s is requesting %s permission for this room.' % (username, perms_str)
            message = data.get('message')
            if message:
                prompt += '\n\n%s says: %s' % (username, message)
            prompt += '\n\nDo you want to grant them permission?'
            confirm = bool(sublime.ok_cancel_dialog(prompt))
            if confirm:
                action = 'add'
            else:
                action = 'reject'
            self.put({
                'name': 'perms',
                'action': action,
                'user_id': user_id,
                'perms': perms
            })
        elif name == 'perms':
            action = data['action']
            user_id = str(data['user_id'])
            user = self.workspace_info['users'].get(user_id)
            if user is None:
                msg.log('No user for id %s. Not handling perms event' % user_id)
                return
            perms = set(user['perms'])
            if action == 'add':
                perms |= set(data['perms'])
            elif action == 'remove':
                perms -= set(data['perms'])
            else:
                return
            user['perms'] = list(perms)
            if user_id == self.workspace_info['user_id']:
                G.PERMS = perms
        elif name == 'msg':
            self.on_msg(data)
        else:
            msg.debug('unknown name!', name, 'data:', data)
コード例 #24
0
ファイル: vim_protocol.py プロジェクト: silky/floobits-vim
 def save_buf(self, buf):
     path = utils.get_full_path(buf['path'])
     utils.mkdir(os.path.split(path)[0])
     with open(path, 'wb') as fd:
         fd.write(buf['buf'].encode('utf-8'))
     return path