コード例 #1
0
ファイル: floobits.py プロジェクト: kishkaru/.config
def share_dir(dir_to_share):
    dir_to_share = os.path.expanduser(dir_to_share)
    dir_to_share = utils.unfuck_path(dir_to_share)
    dir_to_share = os.path.abspath(dir_to_share)

    room_name = os.path.basename(dir_to_share)
    floo_room_dir = os.path.join(G.COLAB_DIR, G.USERNAME, room_name)

    if os.path.isfile(dir_to_share):
        return msg.error('give me a directory please')

    if not os.path.isdir(dir_to_share):
        return msg.error('The directory %s doesn\'t appear to exist' % dir_to_share)

    floo_file = os.path.join(dir_to_share, '.floo')
    # look for the .floo file for hints about previous behavior
    info = {}
    try:
        floo_info = open(floo_file, 'rb').read().decode('utf-8')
        info = json.loads(floo_info)
    except (IOError, OSError):
        pass
    except Exception:
        msg.warn("couldn't read the floo_info file: %s" % floo_file)

    room_url = info.get('url')
    if room_url:
        try:
            result = utils.parse_url(room_url)
        except Exception as e:
            msg.error(str(e))
        else:
            room_name = result['room']
            floo_room_dir = os.path.join(G.COLAB_DIR, result['owner'], result['room'])
            # they have previously joined the room
            if os.path.realpath(floo_room_dir) == os.path.realpath(dir_to_share):
                # it could have been deleted, try to recreate it if possible
                # TODO: org or something here?
                if result['owner'] == G.USERNAME:
                    try:
                        api.create_room(room_name)
                        msg.debug('Created room %s' % room_url)
                    except Exception as e:
                        msg.debug('Tried to create room' + str(e))
                # they wanted to share teh dir, so always share it
                return join_room(room_url, lambda x: agent.protocol.create_buf(dir_to_share))

    # link to what they want to share
    try:
        utils.mkdir(os.path.dirname(floo_room_dir))
        os.symlink(dir_to_share, floo_room_dir)
    except OSError as e:
        if e.errno != 17:
            raise
    except Exception as e:
        return msg.error("Couldn't create symlink from %s to %s: %s" % (dir_to_share, floo_room_dir, str(e)))

    # make & join room
    create_room(room_name, floo_room_dir, dir_to_share)
コード例 #2
0
ファイル: floobits.py プロジェクト: awesome/floobits-vim
def share_dir(dir_to_share):
    dir_to_share = os.path.expanduser(dir_to_share)
    dir_to_share = utils.unfuck_path(dir_to_share)
    dir_to_share = os.path.abspath(dir_to_share)

    room_name = os.path.basename(dir_to_share)
    floo_room_dir = os.path.join(G.COLAB_DIR, G.USERNAME, room_name)

    if os.path.isfile(dir_to_share):
        return msg.error('give me a directory please')

    if not os.path.isdir(dir_to_share):
        return msg.error('The directory %s doesn\'t appear to exist' % dir_to_share)

    floo_file = os.path.join(dir_to_share, '.floo')
    # look for the .floo file for hints about previous behavior
    info = {}
    try:
        floo_info = open(floo_file, 'rb').read().decode('utf-8')
        info = json.loads(floo_info)
    except (IOError, OSError):
        pass
    except Exception:
        msg.warn("couldn't read the floo_info file: %s" % floo_file)

    room_url = info.get('url')
    if room_url:
        try:
            result = utils.parse_url(room_url)
        except Exception as e:
            msg.error(str(e))
        else:
            room_name = result['room']
            floo_room_dir = os.path.join(G.COLAB_DIR, result['owner'], result['room'])
            # they have previously joined the room
            if os.path.realpath(floo_room_dir) == os.path.realpath(dir_to_share):
                # it could have been deleted, try to recreate it if possible
                # TODO: org or something here?
                if result['owner'] == G.USERNAME:
                    try:
                        api.create_room(room_name)
                        msg.debug('Created room %s' % room_url)
                    except Exception as e:
                        msg.debug('Tried to create room' + str(e))
                # they wanted to share teh dir, so always share it
                return join_room(room_url, lambda x: agent.protocol.create_buf(dir_to_share))

    # link to what they want to share
    try:
        utils.mkdir(os.path.dirname(floo_room_dir))
        os.symlink(dir_to_share, floo_room_dir)
    except OSError as e:
        if e.errno != 17:
            raise
    except Exception as e:
        return msg.error("Couldn't create symlink from %s to %s: %s" % (dir_to_share, floo_room_dir, str(e)))

    # make & join room
    create_room(room_name, floo_room_dir, dir_to_share)
コード例 #3
0
ファイル: floobits.py プロジェクト: kishkaru/.config
def join_room(room_url, on_auth=None):
    global agent
    msg.debug("room url is %s" % room_url)

    try:
        result = utils.parse_url(room_url)
    except Exception as e:
        return msg.error(str(e))

    G.PROJECT_PATH = os.path.realpath(os.path.join(G.COLAB_DIR, result['owner'], result['room']))
    utils.mkdir(os.path.dirname(G.PROJECT_PATH))

    d = ''
    # TODO: really bad prompt here
    prompt = "Give me a directory to sync data to (or just press enter): "
    if not os.path.isdir(G.PROJECT_PATH):
        while True:
            d = vim_input(prompt, d, "dir")
            if d == '':
                utils.mkdir(G.PROJECT_PATH)
                break
            d = os.path.realpath(os.path.expanduser(d))
            if os.path.isfile(d):
                prompt = '%s is not a directory. Enter an existing path (or press enter): ' % d
                continue
            if not os.path.isdir(d):
                try:
                    utils.mkdir(d)
                except Exception as e:
                    prompt = "Couldn't make dir: %s because %s " % (d, str(e))
                    continue
            try:
                os.symlink(d, G.PROJECT_PATH)
                break
            except Exception as e:
                return msg.error("Couldn't create symlink from %s to %s: %s" % (d, G.PROJECT_PATH, str(e)))

    G.PROJECT_PATH = os.path.realpath(G.PROJECT_PATH + os.sep)
    vim.command('cd %s' % G.PROJECT_PATH)
    msg.debug("joining room %s" % room_url)

    stop_everything()
    try:
        start_event_loop()
        agent = AgentConnection(on_auth=on_auth, Protocol=Protocol, **result)
        # owner and room name are slugfields so this should be safe
        agent.connect()
    except Exception as e:
        msg.error(str(e))
        tb = traceback.format_exc()
        msg.debug(tb)
        stop_everything()
コード例 #4
0
def join_room(room_url, on_auth=None):
    global agent
    msg.debug("room url is %s" % room_url)

    try:
        result = utils.parse_url(room_url)
    except Exception as e:
        return msg.error(str(e))

    G.PROJECT_PATH = os.path.realpath(
        os.path.join(G.COLAB_DIR, result['owner'], result['room']))
    utils.mkdir(os.path.dirname(G.PROJECT_PATH))

    d = ''
    # TODO: really bad prompt here
    prompt = "Give me a directory to destructively dump data into (or just press enter): "
    if not os.path.isdir(G.PROJECT_PATH):
        while True:
            d = vim_input(prompt, d)
            if d == '':
                utils.mkdir(G.PROJECT_PATH)
                break
            d = os.path.realpath(os.path.expanduser(d))
            if not os.path.isdir(d):
                prompt = '%s is not a directory. Enter an existing path (or press enter): ' % d
                continue
            try:
                os.symlink(d, G.PROJECT_PATH)
                break
            except Exception as e:
                return msg.error("Couldn't create symlink from %s to %s: %s" %
                                 (d, G.PROJECT_PATH, str(e)))

    vim.command('cd %s' % G.PROJECT_PATH)
    msg.debug("joining room %s" % room_url)

    stop_everything()
    try:
        start_event_loop()
        agent = AgentConnection(on_auth=on_auth, Protocol=Protocol, **result)
        # owner and room name are slugfields so this should be safe
        agent.connect()
    except Exception as e:
        msg.error(str(e))
        tb = traceback.format_exc()
        msg.debug(tb)
        stop_everything()
コード例 #5
0
    def run(self, room_url):
        def open_room_window(cb):
            if sublime.platform() == 'linux':
                subl = open('/proc/self/cmdline').read().split(chr(0))[0]
            elif sublime.platform() == 'osx':
                # TODO: totally explodes if you install ST2 somewhere else
                subl = settings.get('sublime_executable', '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl')
            elif sublime.platform() == 'windows':
                subl = sys.executable
            else:
                raise Exception('WHAT PLATFORM ARE WE ON?!?!?')

            command = [subl]
            if utils.get_room_window() is None:
                command.append('--new-window')
            command.append('--add')
            command.append(G.PROJECT_PATH)
            print('command:', command)
            p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            poll_result = p.poll()
            print('poll:', poll_result)

            def create_chat_view():
                with open(os.path.join(G.COLAB_DIR, 'msgs.floobits.log'), 'w') as msgs_fd:
                    msgs_fd.write('')
                msg.get_or_create_chat(cb)
            utils.set_room_window(create_chat_view)

        def run_agent(owner, room, host, port, secure):
            global agent
            if agent:
                agent.stop()
                agent = None
            try:
                agent = AgentConnection(owner, room, host=host, port=port, secure=secure, on_connect=ON_CONNECT)
                # owner and room name are slugfields so this should be safe
                Listener.set_agent(agent)
                agent.connect()
            except Exception as e:
                print(e)
                tb = traceback.format_exc()
                print(tb)
            else:
                joined_room = {'url': room_url}
                update_recent_rooms(joined_room)

        try:
            result = utils.parse_url(room_url)
        except Exception as e:
            return sublime.error_message(str(e))

        def run_thread(*args):
            thread = threading.Thread(target=run_agent, kwargs=result)
            thread.start()

        def link_dir(d):
            if d == '':
                try:
                    utils.mkdir(G.PROJECT_PATH)
                except Exception as e:
                    return sublime.error_message("Couldn't create directory %s: %s" % (G.PROJECT_PATH, str(e)))
                return open_room_window(run_thread)

            try:
                utils.mkdir(os.path.dirname(G.PROJECT_PATH))
            except Exception as e:
                return sublime.error_message("Couldn't create directory %s: %s" % (os.path.dirname(G.PROJECT_PATH), str(e)))

            d = os.path.realpath(os.path.expanduser(d))
            if not os.path.isdir(d):
                make_dir = sublime.ok_cancel_dialog('%s is not a directory. Create it?' % d)
                if not make_dir:
                    return self.window.show_input_panel('%s is not a directory. Enter an existing path:' % d, d, link_dir, None, None)
                try:
                    utils.mkdir(d)
                except Exception as e:
                    return sublime.error_message("Could not create directory %s: %s" % (d, str(e)))
            try:
                os.symlink(d, G.PROJECT_PATH)
            except Exception as e:
                return sublime.error_message("Couldn't create symlink from %s to %s: %s" % (d, G.PROJECT_PATH, str(e)))

            open_room_window(run_thread)

        G.PROJECT_PATH = os.path.realpath(os.path.join(G.COLAB_DIR, result['owner'], result['room']))
        if not os.path.isdir(G.PROJECT_PATH):
            # TODO: really bad prompt here
            return self.window.show_input_panel('Give me a directory to destructively dump data into (or just press enter):', '', link_dir, None, None)

        open_room_window(run_thread)
コード例 #6
0
    def on_input(self, dir_to_share):
        global ON_CONNECT
        dir_to_share = os.path.expanduser(dir_to_share)
        dir_to_share = utils.unfuck_path(dir_to_share)
        room_name = os.path.basename(dir_to_share)
        floo_room_dir = os.path.join(G.COLAB_DIR, G.USERNAME, room_name)
        print(G.COLAB_DIR, G.USERNAME, room_name, floo_room_dir)

        if os.path.isfile(dir_to_share):
            return sublime.error_message('give me a directory please')

        try:
            utils.mkdir(dir_to_share)
        except Exception:
            return sublime.error_message("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:
            print("couldn't read the floo_info file: %s" % floo_file)

        room_url = info.get('url')
        if room_url:
            try:
                result = utils.parse_url(room_url)
            except Exception as e:
                sublime.error_message(str(e))
            else:
                room_name = result['room']
                floo_room_dir = os.path.join(G.COLAB_DIR, result['owner'], result['room'])
                if os.path.realpath(floo_room_dir) == os.path.realpath(dir_to_share):
                    if result['owner'] == G.USERNAME:
                        try:
                            api.create_room(room_name)
                            print('Created room %s' % room_url)
                        except Exception as e:
                            print('Tried to create room' + str(e))
                    # they wanted to share teh dir, so always share it
                    return self.window.run_command('floobits_join_room', {'room_url': room_url})
        # go make sym link
        try:
            utils.mkdir(os.path.dirname(floo_room_dir))
            os.symlink(dir_to_share, floo_room_dir)
        except OSError as e:
            if e.errno != 17:
                raise
        except Exception as e:
            return sublime.error_message("Couldn't create symlink from %s to %s: %s" % (dir_to_share, floo_room_dir, str(e)))

        # make & join room
        ON_CONNECT = lambda x: Listener.create_buf(dir_to_share)
        self.window.run_command('floobits_create_room', {
            'room_name': room_name,
            'ln_path': floo_room_dir,
        })
コード例 #7
0
    def run(self, room_url):
        def open_room_window2(cb):
            if sublime.platform() == 'linux':
                subl = open('/proc/self/cmdline').read().split(chr(0))[0]
            elif sublime.platform() == 'osx':
                # TODO: totally explodes if you install ST2 somewhere else
                subl = settings.get(
                    'sublime_executable',
                    '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl'
                )
            elif sublime.platform() == 'windows':
                subl = sys.executable
            else:
                raise Exception('WHAT PLATFORM ARE WE ON?!?!?')

            command = [subl]
            if utils.get_room_window() is None:
                command.append('--new-window')
            command.append('--add')
            command.append(G.PROJECT_PATH)

            # Maybe no msg view yet :(
            print('command:', command)
            p = subprocess.Popen(command,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            poll_result = p.poll()
            print('poll:', poll_result)

            def truncate_chat_view(chat_view):
                chat_view.set_read_only(False)
                chat_view.run_command('floo_view_replace_region', {
                    'r': [0, chat_view.size()],
                    'data': ''
                })
                chat_view.set_read_only(True)
                cb()

            def create_chat_view():
                with open(os.path.join(G.COLAB_DIR, 'msgs.floobits.log'),
                          'a') as msgs_fd:
                    msgs_fd.write('')
                msg.get_or_create_chat(truncate_chat_view)

            utils.set_room_window(create_chat_view)

        def open_room_window3(cb):
            G.ROOM_WINDOW = utils.get_room_window()
            if not G.ROOM_WINDOW:
                G.ROOM_WINDOW = sublime.active_window()
            msg.debug('Setting project data. Path: %s' % G.PROJECT_PATH)
            G.ROOM_WINDOW.set_project_data(
                {'folders': [{
                    'path': G.PROJECT_PATH
                }]})

            def truncate_chat_view(chat_view):
                chat_view.set_read_only(False)
                chat_view.run_command('floo_view_replace_region', {
                    'r': [0, chat_view.size()],
                    'data': ''
                })
                chat_view.set_read_only(True)
                cb()

            with open(os.path.join(G.COLAB_DIR, 'msgs.floobits.log'),
                      'a') as msgs_fd:
                msgs_fd.write('')
            msg.get_or_create_chat(truncate_chat_view)

        def open_room_window(cb):
            if PY2:
                open_room_window2(cb)
            else:
                open_room_window3(cb)

        def run_agent(owner, room, host, port, secure):
            global agent
            if agent:
                msg.debug('Stopping agent.')
                agent.stop()
                agent = None
            try:
                agent = AgentConnection(owner,
                                        room,
                                        host=host,
                                        port=port,
                                        secure=secure,
                                        on_connect=ON_CONNECT)
                # owner and room name are slugfields so this should be safe
                Listener.set_agent(agent)
                agent.connect()
            except Exception as e:
                print(e)
                tb = traceback.format_exc()
                print(tb)
            else:
                joined_room = {'url': room_url}
                update_recent_rooms(joined_room)

        try:
            result = utils.parse_url(room_url)
        except Exception as e:
            return sublime.error_message(str(e))

        def run_thread(*args):
            thread = threading.Thread(target=run_agent, kwargs=result)
            thread.start()

        def link_dir(d):
            if d == '':
                try:
                    utils.mkdir(G.PROJECT_PATH)
                except Exception as e:
                    return sublime.error_message(
                        "Couldn't create directory %s: %s" %
                        (G.PROJECT_PATH, str(e)))
                return open_room_window(run_thread)

            try:
                utils.mkdir(os.path.dirname(G.PROJECT_PATH))
            except Exception as e:
                return sublime.error_message(
                    "Couldn't create directory %s: %s" %
                    (os.path.dirname(G.PROJECT_PATH), str(e)))

            d = os.path.realpath(os.path.expanduser(d))
            if not os.path.isdir(d):
                make_dir = sublime.ok_cancel_dialog(
                    '%s is not a directory. Create it?' % d)
                if not make_dir:
                    return self.window.show_input_panel(
                        '%s is not a directory. Enter an existing path:' % d,
                        d, link_dir, None, None)
                try:
                    utils.mkdir(d)
                except Exception as e:
                    return sublime.error_message(
                        "Could not create directory %s: %s" % (d, str(e)))
            try:
                os.symlink(d, G.PROJECT_PATH)
            except Exception as e:
                return sublime.error_message(
                    "Couldn't create symlink from %s to %s: %s" %
                    (d, G.PROJECT_PATH, str(e)))

            open_room_window(run_thread)

        reload_settings()
        G.PROJECT_PATH = os.path.realpath(
            os.path.join(G.COLAB_DIR, result['owner'], result['room']))
        print('Project path is %s' % G.PROJECT_PATH)
        if not os.path.isdir(G.PROJECT_PATH):
            # TODO: really bad prompt here
            return self.window.show_input_panel(
                'Give me a directory to destructively dump data into (or just press enter):',
                '', link_dir, None, None)

        open_room_window(run_thread)
コード例 #8
0
    def on_input(self, dir_to_share):
        global ON_CONNECT
        dir_to_share = os.path.expanduser(dir_to_share)
        dir_to_share = utils.unfuck_path(dir_to_share)
        room_name = os.path.basename(dir_to_share)
        floo_room_dir = os.path.join(G.COLAB_DIR, G.USERNAME, room_name)
        print(G.COLAB_DIR, G.USERNAME, room_name, floo_room_dir)

        if os.path.isfile(dir_to_share):
            return sublime.error_message('Give me a directory please')

        try:
            utils.mkdir(dir_to_share)
        except Exception:
            return sublime.error_message(
                "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:
            print("Couldn't read the floo_info file: %s" % floo_file)

        room_url = info.get('url')
        if room_url:
            try:
                result = utils.parse_url(room_url)
            except Exception as e:
                sublime.error_message(str(e))
            else:
                room_name = result['room']
                floo_room_dir = os.path.join(G.COLAB_DIR, result['owner'],
                                             result['room'])
                if os.path.realpath(floo_room_dir) == os.path.realpath(
                        dir_to_share):
                    if result['owner'] == G.USERNAME:
                        try:
                            api.create_room(room_name)
                            print('Created room %s' % room_url)
                        except Exception as e:
                            print('Tried to create room' + str(e))
                    # they wanted to share teh dir, so always share it
                    return self.window.run_command('floobits_join_room',
                                                   {'room_url': room_url})
        # go make sym link
        try:
            utils.mkdir(os.path.dirname(floo_room_dir))
            os.symlink(dir_to_share, floo_room_dir)
        except OSError as e:
            if e.errno != 17:
                raise
        except Exception as e:
            return sublime.error_message(
                "Couldn't create symlink from %s to %s: %s" %
                (dir_to_share, floo_room_dir, str(e)))

        # make & join room
        ON_CONNECT = lambda x: Listener.create_buf(dir_to_share)
        self.window.run_command('floobits_create_room', {
            'room_name': room_name,
            'ln_path': floo_room_dir,
        })