-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #24. Add Python (PEP 8) support.
- Add reusable cli-beautify for external, non-Node beautifiers.
- Loading branch information
Showing
7 changed files
with
152 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import math, sys; | ||
|
||
def example1(): | ||
####This is a long comment. This should be wrapped to fit within 72 characters. | ||
some_tuple=( 1,2, 3,'a' ); | ||
some_variable={'long':'Long code lines should be wrapped within 79 characters.', | ||
'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'], | ||
'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1, | ||
20,300,40000,500000000,60000000000000000]}} | ||
return (some_tuple, some_variable) | ||
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key('')); | ||
class Example3( object ): | ||
def __init__ ( self, bar ): | ||
#Comments should have a space after the hash. | ||
if bar : bar+=1; bar=bar* bar ; return bar | ||
else: | ||
some_string = """ | ||
Indentation in multiline strings should not be touched. | ||
Only actual code should be reindented. | ||
""" | ||
return (sys.path, some_string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
Requires http://pear.php.net/package/PHP_Beautifier | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var temp = require('temp').track(); | ||
var exec = require('child_process').exec; | ||
|
||
module.exports = function (getCmd, isStdout) { | ||
|
||
return function (text, options, callback) { | ||
// Create temp input file | ||
temp.open('input', function (err, info) { | ||
if (!err) { | ||
// Save current text to input file | ||
fs.write(info.fd, text || ''); | ||
fs.close(info.fd, function (err) { | ||
if (!err) { | ||
// Create temp output file | ||
var outputPath = temp.path(); | ||
var deleteOutputFile = function () { | ||
// Delete the output path | ||
fs.unlink(outputPath, function () {}); | ||
}; | ||
// Get the command | ||
var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line | ||
if (cmd) { | ||
|
||
var config = { | ||
env: process.env | ||
}; | ||
|
||
var isWin = /^win/.test(process.platform); | ||
if (!isWin) { | ||
// We need the $PATH to be correct when executing the command. | ||
// This should normalize the $PATH | ||
// by calling the external files that would usually | ||
// change the $PATH variable on user login. | ||
var $path = '[ -f ~/.bash_profile ] && source ~/.bash_profile;'; | ||
$path += '[ -f ~/.bash_rc ] && source ~/.bash_rc;'; | ||
// See http://stackoverflow.com/a/638980/2578205 | ||
// for checking if file exists in Bash | ||
cmd = $path + cmd; | ||
} | ||
|
||
// Execute and beautify! | ||
exec(cmd, config, function (err, stdout) { | ||
if (!err) { | ||
// Beautification has completed | ||
if (isStdout) { | ||
// Execute callback with resulting output text | ||
callback(stdout); | ||
deleteOutputFile(); | ||
} else { | ||
// Read contents of output file | ||
fs.readFile(outputPath, 'utf8', function (err, newText) { | ||
// Execute callback with resulting output text | ||
callback(newText); | ||
deleteOutputFile(); | ||
}); | ||
} | ||
} else { | ||
deleteOutputFile(); | ||
} | ||
}); | ||
} else { | ||
console.log('Beautify CLI command not valid.'); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
Requires https://github.com/hhatto/autopep8 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var cliBeautify = require('./cli-beautify'); | ||
|
||
function getCmd(inputPath, outputPath, options) { | ||
var path = options.autopep8_path; // jshint ignore: line | ||
if (path) { | ||
// Use absolute path | ||
return 'python "' + path + '" "' + inputPath + '"'; | ||
} else { | ||
// Use command available in $PATH | ||
return 'autopep8 "' + inputPath + '"'; | ||
} | ||
} | ||
var isStdin = true; | ||
module.exports = cliBeautify(getCmd, isStdin); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,8 @@ | |
"scss", | ||
"less", | ||
"sql", | ||
"php" | ||
"php", | ||
"python" | ||
], | ||
"engines": { | ||
"atom": ">0.50.0" | ||
|