Skip to content

Detect whether PHP is on path or not, better handle unicode, code cleaning and linting #1

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

Merged
merged 2 commits into from
Dec 18, 2012
Merged
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
39 changes: 20 additions & 19 deletions CSScomb.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import sublime, sublime_plugin
from csscomb import BaseSort
# coding: utf-8

import sublime
import sublime_plugin
from csscomb import LocalSort


def to_unicode_or_bust(obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj


class BaseSorter(sublime_plugin.TextCommand):
"""Base Sorter"""

def __init__(self, view):
self.view = view
# self.settings = sublime.load_settings("Minifier.sublime-settings")

def run(self, edit):

Expand All @@ -15,13 +25,12 @@ def run(self, edit):
threads = []
for sel in selections:
selbody = self.view.substr(sel)

thread = BaseSort(sel,selbody)
selbody = selbody.encode('utf-8')
thread = LocalSort(sel, selbody)

threads.append(thread)
thread.start()

selections.clear()
self.handle_threads(edit, threads, selections, offset=0, i=0)

def get_selections(self):
Expand All @@ -40,7 +49,7 @@ def get_selections(self):

return selections

def handle_threads(self, edit, threads, selections, offset = 0, i = 0):
def handle_threads(self, edit, threads, selections, offset=0, i=0):

next_threads = []
for thread in threads:
Expand All @@ -59,15 +68,10 @@ def handle_threads(self, edit, threads, selections, offset = 0, i = 0):
self.view.end_edit(edit)
sublime.status_message('Successfully sorted')


def handle_result(self, edit, thread, selections, offset):
sel = thread.sel
original = thread.original
# print original
result = thread.result
# print result

if thread.error is True:
if thread.error:
sublime.error_message(result)
return
elif result is None:
Expand All @@ -82,11 +86,8 @@ class CssSorter(BaseSorter):
def handle_result(self, edit, thread, selections, offset):
result = super(CssSorter, self).handle_result(edit, thread, selections, offset)

editgroup = self.view.begin_edit('csscomb')

sel = thread.sel
result = thread.result
# if offset:
# sel = sublime.Region(thread.sel.begin() + offset, thread.sel.end() + offset)
result = to_unicode_or_bust(thread.result)

self.view.replace(edit, sel, result)
if not thread.error:
self.view.replace(edit, sel, result)
3 changes: 2 additions & 1 deletion csscomb/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from basesort import BaseSort
from basesort import BaseSort
from localsort import LocalSort
17 changes: 1 addition & 16 deletions csscomb/basesort.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import sublime
import sublime_plugin
import threading
import sys,subprocess
from os import path

__file__ = path.normpath(path.abspath(__file__))
__path__ = path.dirname(__file__)
libs_path = path.join(__path__, 'libs')
csscomb_path = path.join(libs_path,"call_string.php")

class BaseSort(threading.Thread):

Expand All @@ -19,14 +11,7 @@ def __init__(self, sel, original):
threading.Thread.__init__(self)

def exec_request(self):
myprocess = subprocess.Popen(['php',csscomb_path,self.original], shell=False, stdout=subprocess.PIPE)
(sout,serr) = myprocess.communicate()
myprocess.wait()

if len(sout) > 0:
return sout
else:
return None
return

def run(self):
try:
Expand Down
43 changes: 43 additions & 0 deletions csscomb/localsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import subprocess
from os import path, name

from basesort import BaseSort

__file__ = path.normpath(path.abspath(__file__))
__path__ = path.dirname(__file__)
libs_path = path.join(__path__, 'libs')
csscomb_path = path.join(libs_path, 'call_string.php')


class LocalSort(BaseSort):
startupinfo = None
if name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE

def check_php_on_path(self):
try:
subprocess.call('php -v', shell=False, startupinfo=self.startupinfo)
except (OSError):
self.error = True
self.result = 'Unable find php.exe. Make sure it is available in your PATH.'

def exec_request(self):
if not self.error:
myprocess = subprocess.Popen(['php', csscomb_path, self.original], shell=False, stdout=subprocess.PIPE, startupinfo=self.startupinfo)
(sout, serr) = myprocess.communicate()
myprocess.wait()

if len(sout) > 0:
return sout
else:
return None

def run(self):
self.check_php_on_path()
try:
self.result = self.exec_request()
except (OSError):
self.error = True
self.result = 'Sorter Error: attempt to sort non-existent file'