Esempio n. 1
0
        def on_gist_num(num):
            """Handles gist when user selected it from the list"""
            off_orgs = len(self.orgs)
            off_users = off_orgs + len(self.users)

            if num < 0:
                pass
            elif num < off_orgs:
                self.gists = []

                members = [member.get("login") for member in
                           api_request(settings.get('ORG_MEMBERS_URL') % self.orgs[num])]
                for member in members:
                    self.gists += api_request(settings.get('USER_GISTS_URL') % member)

                filtered = gists_filter(self.gists)
                self.gists = filtered[0]
                gist_names = filtered[1]

                self.orgs = self.users = []
                self.get_window().show_quick_panel(gist_names, on_gist_num)
            elif num < off_users:
                filtered = gists_filter(
                    api_request(settings.get('USER_GISTS_URL') % self.users[num - off_orgs]))
                self.gists = filtered[0]
                gist_names = filtered[1]

                self.orgs = self.users = []
                self.get_window().show_quick_panel(gist_names, on_gist_num)
            else:
                self.handle_gist(self.gists[num - off_users])
Esempio n. 2
0
 def run(self, edit):
     gist_url = self.gist_url()
     api_request(gist_url, method='DELETE')
     for window in sublime.windows():
         for view in window.views():
             if view.settings().get("gist_url") == gist_url:
                 ungistify_view(view)
     sublime.status_message("Gist deleted")
Esempio n. 3
0
 def run(self, edit):
     gist_url = self.gist_url()
     api_request(gist_url, method='DELETE')
     for window in sublime.windows():
         for view in window.views():
             if view.settings().get("gist_url") == gist_url:
                 ungistify_view(view)
     sublime.status_message("Gist deleted")
Esempio n. 4
0
    def run(self, *args):  # TextCommand sends sublime.Edit object and WindowCommand is not
        filtered_gists, filtered_gist_names = gists_filter(api_request(settings.get('STARRED_GISTS_URL')), '★ ')

        if not settings.get('use_starred'):
            # add not starred gists
            filtered_not_stared = gists_filter(api_request(settings.get('GISTS_URL')))
            filtered_gists = filtered_not_stared[0] + filtered_gists
            filtered_gist_names = filtered_not_stared[1] + filtered_gist_names

        self.gists = filtered_gists
        gist_names = filtered_gist_names

        if settings.get('include_users'):
            self.users = list(settings.get('include_users'))
            gist_names = [["> " + user] for user in self.users] + gist_names

        if settings.get('include_orgs'):
            if settings.get('include_orgs') is True:
                self.orgs = [org.get("login") for org in api_request(settings.get('ORGS_URL'))]
            else:
                self.orgs = settings.get('include_orgs')

            gist_names = [["> " + org] for org in self.orgs] + gist_names

        def on_gist_num(num):
            """Handles gist when user selected it from the list"""
            off_orgs = len(self.orgs)
            off_users = off_orgs + len(self.users)

            if num < 0:
                pass
            elif num < off_orgs:
                self.gists = []

                members = [member.get("login") for member in
                           api_request(settings.get('ORG_MEMBERS_URL') % self.orgs[num])]
                for member in members:
                    self.gists += api_request(settings.get('USER_GISTS_URL') % member)

                filtered = gists_filter(self.gists)
                self.gists = filtered[0]
                gist_names = filtered[1]

                self.orgs = self.users = []
                self.get_window().show_quick_panel(gist_names, on_gist_num)
            elif num < off_users:
                filtered = gists_filter(
                    api_request(settings.get('USER_GISTS_URL') % self.users[num - off_orgs]))
                self.gists = filtered[0]
                gist_names = filtered[1]

                self.orgs = self.users = []
                self.get_window().show_quick_panel(gist_names, on_gist_num)
            else:
                self.handle_gist(self.gists[num - off_users])

        self.get_window().show_quick_panel(gist_names, on_gist_num)
Esempio n. 5
0
def open_gist(gist_url):
    gist = api_request(gist_url)
    files = sorted(gist['files'].keys())

    for gist_filename in files:
        allowed_types = ['text', 'application']
        media_type = gist['files'][gist_filename]['type'].split('/')[0]
        if media_type not in allowed_types:
            continue

        view = sublime.active_window().new_file()

        gistify_view(view, gist, gist_filename)

        view.run_command('append', {
            'characters': gist['files'][gist_filename]['content'],
        })

        if settings.get('supress_save_dialog'):
            view.set_scratch(True)

        if settings.get('update_on_save'):
            view.retarget(os.path.join(tempfile.gettempdir(), gist_filename))
            # Save over it (to stop us reloading from that file in case it exists)
            # But don't actually do a gist update
            view.settings().set('do-update', False)
            view.run_command('save')

        set_syntax(view, gist['files'][gist_filename])
Esempio n. 6
0
def open_gist(gist_url):
    gist = api_request(gist_url)
    files = sorted(gist['files'].keys())

    for gist_filename in files:
        allowed_types = ['text', 'application']
        media_type = gist['files'][gist_filename]['type'].split('/')[0]
        if media_type not in allowed_types:
            continue

        view = sublime.active_window().new_file()

        gistify_view(view, gist, gist_filename)

        view.run_command('append', {
            'characters': gist['files'][gist_filename]['content'],
        })

        if settings.get('supress_save_dialog'):
            view.set_scratch(True)

        if settings.get('update_on_save'):
            view.retarget(os.path.join(tempfile.gettempdir(), gist_filename))
            # Save over it (to stop us reloading from that file in case it exists)
            # But don't actually do a gist update
            view.settings().set('do-update', False)
            view.run_command('save')

        set_syntax(view, gist['files'][gist_filename])
Esempio n. 7
0
def update_gist(gist_url, file_changes, auth_token=None, https_proxy=None, new_description=None):
    request = {'files': file_changes}
    if new_description is not None:
        request['description'] = new_description
    data = json.dumps(request)
    result = api_request(gist_url, data, token=auth_token, https_proxy=https_proxy, method="PATCH")

    sublime.status_message("Gist updated")

    return result
Esempio n. 8
0
def create_gist(public, description, files):
    for _, text in list(files.items()):
        if not text:
            sublime.error_message("Gist: Unable to create a Gist with empty content")
            return

    file_data = dict((filename, {'content': text}) for filename, text in list(files.items()))
    data = json.dumps({'description': description, 'public': public, 'files': file_data})
    gist = api_request(settings.get('GISTS_URL'), data)
    return gist
Esempio n. 9
0
def insert_gist_embed(gist_url):
    gist = api_request(gist_url)
    files = sorted(gist['files'].keys())

    for gist_filename in files:
        view = sublime.active_window().active_view()

        template = '<script src="{0}"></script>'.format(gist['files'][gist_filename]['raw_url'])

        view.run_command('insert', {
            'characters': template,
        })
Esempio n. 10
0
def insert_gist_embed(gist_url):
    gist = api_request(gist_url)
    files = sorted(gist['files'].keys())

    for gist_filename in files:
        view = sublime.active_window().active_view()

        template = '<script src="{0}"></script>'.format(
            gist['files'][gist_filename]['raw_url'])

        view.run_command('insert', {
            'characters': template,
        })
Esempio n. 11
0
        def on_gist_num(num):
            """Handles gist when user selected it from the list"""
            off_orgs = len(self.orgs)
            off_users = off_orgs + len(self.users)

            if num < 0:
                pass
            elif num < off_orgs:
                self.gists = []

                members = [
                    member.get("login") for member in api_request(
                        settings.get('ORG_MEMBERS_URL') % self.orgs[num])
                ]
                for member in members:
                    self.gists += api_request(
                        settings.get('USER_GISTS_URL') % member)

                filtered = gists_filter(self.gists)
                self.gists = filtered[0]
                gist_names = filtered[1]

                self.orgs = self.users = []
                self.get_window().show_quick_panel(gist_names, on_gist_num)
            elif num < off_users:
                filtered = gists_filter(
                    api_request(
                        settings.get('USER_GISTS_URL') %
                        self.users[num - off_orgs]))
                self.gists = filtered[0]
                gist_names = filtered[1]

                self.orgs = self.users = []
                self.get_window().show_quick_panel(gist_names, on_gist_num)
            else:
                self.handle_gist(self.gists[num - off_users])
Esempio n. 12
0
def create_gist(public, description, files):
    for _, text in list(files.items()):
        if not text:
            sublime.error_message(
                "Gist: Unable to create a Gist with empty content")
            return

    file_data = dict((filename, {
        'content': text
    }) for filename, text in list(files.items()))
    data = json.dumps({
        'description': description,
        'public': public,
        'files': file_data
    })
    gist = api_request(settings.get('GISTS_URL'), data)
    return gist
Esempio n. 13
0
def update_gist(gist_url,
                file_changes,
                auth_token=None,
                https_proxy=None,
                new_description=None):
    request = {'files': file_changes}
    if new_description is not None:
        request['description'] = new_description
    data = json.dumps(request)
    result = api_request(gist_url,
                         data,
                         token=auth_token,
                         https_proxy=https_proxy,
                         method="PATCH")

    sublime.status_message("Gist updated")

    return result
Esempio n. 14
0
def insert_gist(gist_url):
    gist = api_request(gist_url)
    files = sorted(gist['files'].keys())

    for gist_filename in files:
        view = sublime.active_window().active_view()

        is_auto_indent = view.settings().get('auto_indent')

        if is_auto_indent:
            view.settings().set('auto_indent', False)
            view.run_command('insert', {
                'characters': gist['files'][gist_filename]['content'],
            })
            view.settings().set('auto_indent', True)
        else:
            view.run_command('insert', {
                'characters': gist['files'][gist_filename]['content'],
            })
Esempio n. 15
0
def insert_gist(gist_url):
    gist = api_request(gist_url)
    files = sorted(gist['files'].keys())

    for gist_filename in files:
        view = sublime.active_window().active_view()

        is_auto_indent = view.settings().get('auto_indent')

        if is_auto_indent:
            view.settings().set('auto_indent', False)
            view.run_command(
                'insert', {
                    'characters': gist['files'][gist_filename]['content'],
                })
            view.settings().set('auto_indent', True)
        else:
            view.run_command(
                'insert', {
                    'characters': gist['files'][gist_filename]['content'],
                })
Esempio n. 16
0
    def test_api_request(self, mocked_urllib):
        url = 'https://url.test'
        data = 'some data'
        token = 'some token'
        https_proxy = 'some https proxy'
        method = 'some method'

        mocked_urllib.urlopen().read.return_value = b'{"some": "response"}'

        result = gist_request.api_request(url, data, token, https_proxy,
                                          method)

        mocked_urllib.Request.assert_called_with(url)

        self.assertEqual(mocked_urllib.Request().add_header.call_count, 3)
        self.assertEqual(
            mocked_urllib.Request().add_header.call_args_list[0][0][0],
            'Authorization')
        self.assertEqual(
            mocked_urllib.Request().add_header.call_args_list[0][0][1],
            'token some token')
        self.assertEqual(
            mocked_urllib.Request().add_header.call_args_list[1][0][0],
            'Accept')
        self.assertEqual(
            mocked_urllib.Request().add_header.call_args_list[1][0][1],
            'application/json')
        self.assertEqual(
            mocked_urllib.Request().add_header.call_args_list[2][0][0],
            'Content-Type')
        self.assertEqual(
            mocked_urllib.Request().add_header.call_args_list[2][0][1],
            'application/json')

        mocked_urllib.Request().add_data.assert_called_with(b'some data')

        mocked_urllib.build_opener.assert_called_with(
            mocked_urllib.HTTPHandler(), mocked_urllib.HTTPSHandler(),
            mocked_urllib.ProxyHandler())

        mocked_urllib.install_opener.assert_called_with(
            mocked_urllib.build_opener())

        self.assertEqual(result, {'some': 'response'})

        # no content flow, do nothing
        mocked_urllib.urlopen().code = 204

        result = gist_request.api_request(url, data, token, https_proxy,
                                          method)

        self.assertIsNone(result)

        # HTTPException flow
        mocked_urllib.HTTPError = HTTPError
        mocked_urllib.urlopen.side_effect = HTTPError(
            url, 'some code', 'some msg', 'some headers',
            StringIO('some error data'))

        self.assertRaises(SimpleHTTPError, gist_request.api_request, url, data,
                          token)
Esempio n. 17
0
    def run(
        self, *args
    ):  # TextCommand sends sublime.Edit object and WindowCommand is not
        filtered_gists, filtered_gist_names = gists_filter(
            api_request(settings.get('STARRED_GISTS_URL')), '★ ')

        if not settings.get('use_starred'):
            # add not starred gists
            filtered_not_stared = gists_filter(
                api_request(settings.get('GISTS_URL')))
            filtered_gists = filtered_not_stared[0] + filtered_gists
            filtered_gist_names = filtered_not_stared[1] + filtered_gist_names

        self.gists = filtered_gists
        gist_names = filtered_gist_names

        if settings.get('include_users'):
            self.users = list(settings.get('include_users'))
            gist_names = [["> " + user] for user in self.users] + gist_names

        if settings.get('include_orgs'):
            if settings.get('include_orgs') is True:
                self.orgs = [
                    org.get("login")
                    for org in api_request(settings.get('ORGS_URL'))
                ]
            else:
                self.orgs = settings.get('include_orgs')

            gist_names = [["> " + org] for org in self.orgs] + gist_names

        def on_gist_num(num):
            """Handles gist when user selected it from the list"""
            off_orgs = len(self.orgs)
            off_users = off_orgs + len(self.users)

            if num < 0:
                pass
            elif num < off_orgs:
                self.gists = []

                members = [
                    member.get("login") for member in api_request(
                        settings.get('ORG_MEMBERS_URL') % self.orgs[num])
                ]
                for member in members:
                    self.gists += api_request(
                        settings.get('USER_GISTS_URL') % member)

                filtered = gists_filter(self.gists)
                self.gists = filtered[0]
                gist_names = filtered[1]

                self.orgs = self.users = []
                self.get_window().show_quick_panel(gist_names, on_gist_num)
            elif num < off_users:
                filtered = gists_filter(
                    api_request(
                        settings.get('USER_GISTS_URL') %
                        self.users[num - off_orgs]))
                self.gists = filtered[0]
                gist_names = filtered[1]

                self.orgs = self.users = []
                self.get_window().show_quick_panel(gist_names, on_gist_num)
            else:
                self.handle_gist(self.gists[num - off_users])

        self.get_window().show_quick_panel(gist_names, on_gist_num)