Permalink
Browse files

Allow debug output to be enabled

  • Loading branch information...
1 parent 08441ab commit 12102ef9df4ef5423888293776f2d816d5ad9e3d @bgreenlee committed May 23, 2012
Showing with 35 additions and 20 deletions.
  1. +3 −1 GitHub.sublime-settings
  2. +25 −13 github.py
  3. +5 −4 sublime_github.py
  4. +2 −2 sublime_requests.py
View
@@ -15,5 +15,7 @@
// "gist_list_format": ["%(filename)s", "%(description)s"],
"gist_list_format": "%(filename)s: %(description)s",
// If true, display the list of gists in a monospace font
- "gist_list_monospace": false
+ "gist_list_monospace": false,
+ // output debug info to the console
+ "debug": false
}
View
@@ -1,6 +1,11 @@
import os.path
import json
import sublime_requests as requests
+import sys
+import logging
+
+logging.basicConfig(format='%(asctime)s %(message)s')
+logger = logging.getLogger()
class GitHubApi(object):
@@ -18,30 +23,36 @@ class UnknownException(Exception):
"Raised if we get a response code we don't recognize from GitHub"
pass
- # set up requests session with the github ssl cert
- rsession = requests.session(verify=os.path.join(os.path.dirname(os.path.abspath(__file__)),
- "api.github.com.crt"))
-
- def __init__(self, token):
+ def __init__(self, token=None, debug=False):
self.token = token
-
- @classmethod
- def get_token(cls, username, password):
+ self.debug = debug
+ if debug:
+ logger.setLevel(logging.DEBUG)
+
+ # set up requests session with the github ssl cert, if found
+ cert_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "api.github.com.crt")
+ if not os.path.isfile(cert_path):
+ logger.warning("GitHub SSL cert not found at %s! Not verifying requests." % cert_path)
+ cert_path = None
+ self.rsession = requests.session(verify=cert_path,
+ config={'verbose': sys.stderr if self.debug else None})
+
+ def get_token(self, username, password):
auth_data = {
"scopes": ["gist"],
"note": "Sublime GitHub",
"note_url": "https://github.com/bgreenlee/sublime-github"
}
- resp = cls.rsession.post("https://api.github.com/authorizations",
- auth=(username, password),
- data=json.dumps(auth_data))
+ resp = self.rsession.post("https://api.github.com/authorizations",
+ auth=(username, password),
+ data=json.dumps(auth_data))
if resp.status_code == 201:
data = json.loads(resp.text)
return data["token"]
elif resp.status_code == 401:
- raise cls.UnauthorizedException()
+ raise self.UnauthorizedException()
else:
- raise cls.UnknownException("%d %s" % (resp.status_code, resp.text))
+ raise self.UnknownException("%d %s" % (resp.status_code, resp.text))
def post(self, endpoint, data=None):
return self.request('post', endpoint, data=data)
@@ -68,6 +79,7 @@ def request(self, method, url, params=None, data=None):
params=params,
data=data,
allow_redirects=True)
+ logger.debug("response headers: %s" % resp.headers)
full_url = resp.url
if resp.status_code in [requests.codes.ok,
requests.codes.created,
View
@@ -33,6 +33,7 @@ def run(self, edit):
self.settings = sublime.load_settings("GitHub.sublime-settings")
self.github_user = None
self.github_token = self.settings.get('github_token')
+ self.debug = self.settings.get('debug')
def get_token(self):
sublime.error_message(self.ERR_NO_USER_TOKEN)
@@ -53,7 +54,7 @@ def on_done_username(self, value):
def on_done_password(self, value):
"Callback for the password show_input_panel"
try:
- self.github_token = GitHubApi.get_token(self.github_user, value)
+ self.github_token = GitHubApi(debug=self.debug).get_token(self.github_user, value)
self.settings.set("github_token", self.github_token)
sublime.save_settings("GitHub.sublime-settings")
if self.callback:
@@ -87,7 +88,7 @@ def run(self, edit):
self.get_token()
def get_gists(self):
- self.gistapi = GitHubApi(self.github_token)
+ self.gistapi = GitHubApi(self.github_token, debug=self.debug)
try:
self.gists = self.gistapi.list_gists(starred=self.starred)
format = self.settings.get("gist_list_format")
@@ -245,7 +246,7 @@ def on_done_filename(self, value):
else:
text = "\n".join([self.view.substr(region) for region in self.view.sel()])
- gistapi = GitHubApi(self.github_token)
+ gistapi = GitHubApi(self.github_token, debug=self.debug)
try:
gist_url = gistapi.create_gist(description=self.description,
filename=self.filename,
@@ -294,7 +295,7 @@ def run(self, edit):
def update(self):
text = self.view.substr(sublime.Region(0, self.view.size()))
- gistapi = GitHubApi(self.github_token)
+ gistapi = GitHubApi(self.github_token, debug=self.debug)
try:
gist_url = gistapi.update_gist(self.gist, text)
sublime.set_clipboard(gist_url)
View
@@ -74,8 +74,8 @@ def post(self, *args, **kwargs):
return self.request("post", *args, **kwargs)
-def session(verify=None):
+def session(verify=None, config=None):
if hasattr(httplib, "HTTPSConnection"):
- return requests.session(verify=verify)
+ return requests.session(verify=verify, config=config)
else: # try curl
return CurlSession(verify=verify)

0 comments on commit 12102ef

Please sign in to comment.