Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add braces to all outgoing requests so that Prefixr behaves itself... #19

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@
"file": "${packages}/User/Default (Linux).sublime-keymap",
"platform": "Linux"
}
},
{
"caption": "Prefixr",
"command": "prefixr"
}
]
56 changes: 36 additions & 20 deletions Prefixr.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import sublime
import sublime_plugin
import urllib
import urllib2
import threading
import re

try:
from urllib.request import Request, urlopen
from urllib.parse import urlencode
from urllib.error import HTTPError, URLError
except ImportError:
from urllib import urlencode
from urllib2 import Request, HTTPError, URLError, urlopen

class PrefixrCommand(sublime_plugin.TextCommand):
def run(self, edit):
Expand Down Expand Up @@ -38,21 +42,18 @@ def run(self, edit):

# We clear all selection because we are going to manually set them
self.view.sel().clear()

# This creates an edit group so we can undo all changes in one go
edit = self.view.begin_edit('prefixr')

self.handle_threads(edit, threads, braces)

def handle_threads(self, edit, threads, braces, offset=0, i=0, dir=1):
next_threads = []
finished_threads = []
for thread in threads:
if thread.is_alive():
next_threads.append(thread)
continue
if thread.result == False:
continue
offset = self.replace(edit, thread, braces, offset)
finished_threads.append(thread)
threads = next_threads

if len(threads):
Expand All @@ -71,17 +72,31 @@ def handle_threads(self, edit, threads, braces, offset=0, i=0, dir=1):
braces, offset, i, dir), 100)
return

self.view.end_edit(edit)

replacements = []
for thread in finished_threads:
replacements.append({"begin": thread.sel.begin(),
"end":thread.sel.end(),
"original": thread.original,
"result": thread.result.decode('utf8')})
args = {"braces": braces, "replacements": replacements}
self.view.run_command("prefixrreplace",args)
self.view.erase_status('prefixr')
selections = len(self.view.sel())
sublime.status_message('Prefixr successfully run on %s selection%s' %
(selections, '' if selections == 1 else 's'))

def replace(self, edit, thread, braces, offset):
sel = thread.sel
original = thread.original
result = thread.result
class PrefixrreplaceCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
offset = 0
braces = args['braces']
for replacement in args['replacements']:
offset = self.replace(edit, replacement, braces, offset)
return

def replace(self, edit, replacement, braces, offset):
sel = sublime.Region(replacement['begin'],replacement['end'])
original = replacement['original']
result = replacement['result']

# Here we adjust each selection for any text we have already inserted
if offset:
Expand Down Expand Up @@ -152,16 +167,17 @@ def __init__(self, sel, string, timeout):

def run(self):
try:
data = urllib.urlencode({'css': self.original})
request = urllib2.Request('http://prefixr.com/api/index.php', data,
data = urlencode({'css': '{%s}' % (self.original)})
data = data.encode('utf8')
request = Request('http://prefixr.com/api/index.php', data,
headers={"User-Agent": "Sublime Prefixr"})
http_file = urllib2.urlopen(request, timeout=self.timeout)
self.result = http_file.read()
http_file = urlopen(request, timeout=self.timeout)
self.result = http_file.read().replace('{', '').replace('}', '')
return

except (urllib2.HTTPError) as (e):
except (HTTPError) as e:
err = '%s: HTTP error %s contacting API' % (__name__, str(e.code))
except (urllib2.URLError) as (e):
except (URLError) as e:
err = '%s: URL error %s contacting API' % (__name__, str(e.reason))

sublime.error_message(err)
Expand Down