Skip to content

Commit

Permalink
Fixed suppport for csstidy.exe
Browse files Browse the repository at this point in the history
added universal_newlines=True and shell=True flags to Popen, removed tmp
file support, since STDOUT works with those options set.
  • Loading branch information
fitnr committed Aug 6, 2012
1 parent 7828ffc commit 5d4e48c
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions csstidy.py
Expand Up @@ -33,15 +33,19 @@
### FUNCTIONS ###


def tidy_string(input_css, script, args):
def tidy_string(input_css, script, args, shell):
command = [script] + args
print "CSSTidy: Sending command: {0}".format(" ".join(command))

p = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
stderr=subprocess.PIPE,
universal_newlines=True,
shell=shell
)

tidied, err = p.communicate(input_css)
return tidied, err, p.returncode

Expand All @@ -59,7 +63,7 @@ def find_tidier():

if sublime.platform() == 'windows':
try:
subprocess.call([csstidypath, "-v"])
subprocess.call([csstidypath, "-v"], shell=True)
print "CSSTidy: using csstidy.exe"
return csstidypath, False
except OSError:
Expand Down Expand Up @@ -97,9 +101,14 @@ def run(self, edit, **args):
# Fetch arguments from prefs files.
csstidy_args = self.get_args(args, using_php)

if sublime.platform() == 'windows':
shell = True
else:
shell = False

# Tidy each selection.
for sel in self.view.sel():
tidied, err, retval = tidy_string(self.view.substr(sel), csstidy, csstidy_args)
tidied, err, retval = tidy_string(self.view.substr(sel), csstidy, csstidy_args, shell)
#print 'CSSTIdy: Got these tidied styles back:\n' + tidied

if err or retval != 0:
Expand All @@ -114,12 +123,6 @@ def run(self, edit, **args):
nv.set_name('CSSTidy Errors')

else:
if not using_php:
with open(self.out_file, "r") as fh:
tidied = fh.read().rstrip()
#remove(out_file)

# For PHP, use stdout
if self.view.settings().get('translate_tabs_to_spaces'):
tidied.replace("\t", self.space_tab)
self.view.replace(edit, sel, tidied + "\n")
Expand All @@ -132,10 +135,10 @@ def get_args(self, passed_args, using_php):
csstidy_args = []

# Start off with a dash, the flag for using STDIN
if not using_php:
csstidy_args.append('-')
else:
if using_php:
csstidy_args.extend(['-f', normpath(scriptpath), '--'])
else:
csstidy_args.append('-')

for option in supported_options:
# If custom value isn't set, ignore that setting.
Expand All @@ -162,8 +165,6 @@ def get_args(self, passed_args, using_php):

# Set out file for csstidy.exe. PHP using stream.
if not using_php:
self.out_file = normpath(join(packagepath, 'csstidy.tmp'))
csstidy_args.append(self.out_file)
#print 'CSSTidy: setting temp file to "{0}"'.format(self.out_file)
csstidy_args.append('--silent=1')

return csstidy_args

0 comments on commit 5d4e48c

Please sign in to comment.