Skip to content

Commit

Permalink
Closes #24. Add Python (PEP 8) support.
Browse files Browse the repository at this point in the history
- Add reusable cli-beautify for external, non-Node beautifiers.
  • Loading branch information
Glavin001 committed Jun 16, 2014
1 parent 5796618 commit 612c7dc
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 50 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ Atom Package: https://atom.io/packages/atom-beautify
- [x] SQL, special thanks to [pretty-data](https://github.com/vkiryukhin/pretty-data)
- [x] [PHP](https://github.com/donaldpipowitch/atom-beautify/issues/26)
- Requires [PHP_Beautifier](http://pear.php.net/package/PHP_Beautifier) to be already installed.
- [See `PHP` under `Advanced Language Setup` for setup details](https://github.com/donaldpipowitch/atom-beautify#php)
- [x] [Python](https://github.com/donaldpipowitch/atom-beautify/issues/24)
- Requires [autopep8](https://github.com/hhatto/autopep8) to be already installed.
- Beautifies to [PEP 8](http://legacy.python.org/dev/peps/pep-0008/).

### Coming Soon

- [ ] Python, see https://github.com/donaldpipowitch/atom-beautify/issues/24
- [ ] Ruby, see https://github.com/donaldpipowitch/atom-beautify/issues/25
- [ ] CoffeeScript, see https://github.com/donaldpipowitch/atom-beautify/issues/31

Expand Down Expand Up @@ -125,6 +126,15 @@ and that you set the `Php beautifier path` in the package settings.

Run `which php_beautifier` in your Terminal.

### Python

To use with Python we require [autopep8](https://github.com/hhatto/autopep8)
and that you set the `Python autopep8 path` in the package settings.

#### Retrieve the path on Mac & Linux

Run `which autopep8` in your Terminal.


## Contributing

Expand Down
21 changes: 21 additions & 0 deletions examples/simple-jsbeautifyrc/test.py
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)
10 changes: 8 additions & 2 deletions lib/atom-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var beautifyHTML = require('js-beautify').html;
var beautifyCSS = require('js-beautify').css;
var beautifySQL = require('./langs/sql-beautify');
var beautifyPHP = require('./langs/php-beautify');
var beautifyPython = require('./langs/python-beautify');

// TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = {
Expand Down Expand Up @@ -158,7 +159,7 @@ function findConfig(config, file) {

// Supported unique configuration keys
// Used for detecting nested configurations in .jsbeautifyrc
var languages = ['js', 'html', 'css', 'sql', 'php'];
var languages = ['js', 'html', 'css', 'sql', 'php', 'python'];
var defaultLanguageOptions = {
/* jshint ignore: start */
// JavaScript
Expand Down Expand Up @@ -191,7 +192,9 @@ var defaultLanguageOptions = {
sql_indent_size: 2,
sql_indent_char: ' ',
// PHP
php_beautifier_path: ''
php_beautifier_path: '',
// Python
python_autopep8_path: ''
/* jshint ignore: end */
};

Expand Down Expand Up @@ -397,6 +400,9 @@ function beautify() {
case 'PHP':
beautifyPHP(text, getOptions('php', allOptions), beautifyCompleted);
break;
case 'Python':
beautifyPython(text, getOptions('python', allOptions), beautifyCompleted);
break;
default:
return;
}
Expand Down
77 changes: 77 additions & 0 deletions lib/langs/cli-beautify.js
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.');
}
}
});
}
});
};

};
57 changes: 12 additions & 45 deletions lib/langs/php-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,16 @@ 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;
var cliBeautify = require('./cli-beautify');

module.exports = function (text, options, callback) {
// Create temp input file
temp.open('input.php', 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 () {});
};

var phpBeautifierPath = options.beautifier_path; // jshint ignore: line
if (phpBeautifierPath) {
// Beautify
var config = {
env: process.env
};
exec('php "' + phpBeautifierPath + '" "' + info.path + '" "' + outputPath + '"', config, function (err) {
if (!err) {
// Beautification has completed
// 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('PHP Beautifier Path not set in Package settings.');
}
}
});
}
});
};
function getCmd(inputPath, outputPath, options) {
var phpBeautifierPath = options.beautifier_path; // jshint ignore: line
if (phpBeautifierPath) {
// Use absolute path
return 'php "' + phpBeautifierPath + '" "' + inputPath + '" "' + outputPath + '"';
} else {
// Use command available in $PATH
return 'php_beautifier "' + inputPath + '" "' + outputPath + '"';
}
}
module.exports = cliBeautify(getCmd);
20 changes: 20 additions & 0 deletions lib/langs/python-beautify.js
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);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"scss",
"less",
"sql",
"php"
"php",
"python"
],
"engines": {
"atom": ">0.50.0"
Expand Down

0 comments on commit 612c7dc

Please sign in to comment.