Skip to content

Commit

Permalink
Fixed empty gist language bug and some preparations for Python 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
condemil committed Feb 3, 2013
1 parent e6df4f0 commit 28389a3
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions gist.py
@@ -1,10 +1,10 @@
from __future__ import print_function
import sublime
import sublime_plugin
import os
import sys
import json
import base64
import urllib2
import subprocess
import functools
import webbrowser
Expand All @@ -14,6 +14,11 @@
import shutil
import re

try:
import urllib2 as urllib
except ImportError: # Python 3
import urllib.request as urllib

DEFAULT_CREATE_PUBLIC_VALUE = 'false'
DEFAULT_USE_PROXY_VALUE = 'false'
settings = sublime.load_settings('Gist.sublime-settings')
Expand Down Expand Up @@ -246,6 +251,8 @@ def open_gist(gist_url):

language = gist['files'][gist_filename]['language']

if language is None: continue

if language == 'C':
new_syntax = os.path.join('C++',"{0}.tmLanguage".format(language))
else:
Expand Down Expand Up @@ -323,7 +330,7 @@ def gists_filter(all_gists):
return [gists, gists_names]

def api_request_native(url, data=None, method=None):
request = urllib2.Request(url)
request = urllib.Request(url)
if method:
request.get_method = lambda: method
try:
Expand All @@ -337,18 +344,18 @@ def api_request_native(url, data=None, method=None):
request.add_data(data)

if settings.get('https_proxy'):
opener = urllib2.build_opener(urllib2.HTTPHandler(), urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'https': settings.get('https_proxy')}))
opener = urllib.build_opener(urllib.HTTPHandler(), urllib.HTTPSHandler(),
urllib.ProxyHandler({'https': settings.get('https_proxy')}))

urllib2.install_opener(opener)
urllib.install_opener(opener)

try:
with contextlib.closing(urllib2.urlopen(request)) as response:
with contextlib.closing(urllib.urlopen(request)) as response:
if response.code == 204: # No Content
return None
else:
return json.loads(response.read())
except urllib2.HTTPError as err:
except urllib.HTTPError as err:
with contextlib.closing(err):
raise SimpleHTTPError(err.code, err.read())

Expand Down Expand Up @@ -408,7 +415,7 @@ def api_request_curl(url, data=None, method=None):
else:
raise SimpleHTTPError(responsecode, response)

api_request = api_request_curl if ('ssl' not in sys.modules and os.name != 'nt') else api_request_native
api_request = api_request_native

class GistCommand(sublime_plugin.TextCommand):
public = True
Expand Down Expand Up @@ -577,7 +584,7 @@ def run(self, *args):

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

print gist_names
print(gist_names)

def on_gist_num(num):
offOrgs = len(self.orgs)
Expand All @@ -595,15 +602,15 @@ def on_gist_num(num):
filtered = gists_filter(self.gists)
self.gists = filtered[0]
gist_names = filtered[1]
print gist_names
print(gist_names)

self.orgs = self.users = []
self.get_window().show_quick_panel(gist_names, on_gist_num)
elif num < offUsers:
filtered = gists_filter(get_user_gists(self.users[num - offOrgs]))
self.gists = filtered[0]
gist_names = filtered[1]
print gist_names
print(gist_names)

self.orgs = self.users = []
self.get_window().show_quick_panel(gist_names, on_gist_num)
Expand Down

0 comments on commit 28389a3

Please sign in to comment.