From 612c7dc17c20f01c36476e0432baf20bcab94ca4 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Sun, 15 Jun 2014 23:11:01 -0300 Subject: [PATCH] Closes #24. Add Python (PEP 8) support. - Add reusable cli-beautify for external, non-Node beautifiers. --- README.md | 14 ++++- examples/simple-jsbeautifyrc/test.py | 21 ++++++++ lib/atom-beautify.js | 10 +++- lib/langs/cli-beautify.js | 77 ++++++++++++++++++++++++++++ lib/langs/php-beautify.js | 57 +++++--------------- lib/langs/python-beautify.js | 20 ++++++++ package.json | 3 +- 7 files changed, 152 insertions(+), 50 deletions(-) create mode 100644 examples/simple-jsbeautifyrc/test.py create mode 100644 lib/langs/cli-beautify.js create mode 100644 lib/langs/python-beautify.js diff --git a/README.md b/README.md index dca0be6..c8b196a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/examples/simple-jsbeautifyrc/test.py b/examples/simple-jsbeautifyrc/test.py new file mode 100644 index 0000000..b04a9a1 --- /dev/null +++ b/examples/simple-jsbeautifyrc/test.py @@ -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) diff --git a/lib/atom-beautify.js b/lib/atom-beautify.js index 455ffe7..8a3749c 100644 --- a/lib/atom-beautify.js +++ b/lib/atom-beautify.js @@ -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 = { @@ -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 @@ -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 */ }; @@ -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; } diff --git a/lib/langs/cli-beautify.js b/lib/langs/cli-beautify.js new file mode 100644 index 0000000..c890cf8 --- /dev/null +++ b/lib/langs/cli-beautify.js @@ -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.'); + } + } + }); + } + }); + }; + +}; diff --git a/lib/langs/php-beautify.js b/lib/langs/php-beautify.js index e4554a4..4ef8a14 100644 --- a/lib/langs/php-beautify.js +++ b/lib/langs/php-beautify.js @@ -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); diff --git a/lib/langs/python-beautify.js b/lib/langs/python-beautify.js new file mode 100644 index 0000000..396c944 --- /dev/null +++ b/lib/langs/python-beautify.js @@ -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); diff --git a/package.json b/package.json index a4dc068..a9a04c9 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,8 @@ "scss", "less", "sql", - "php" + "php", + "python" ], "engines": { "atom": ">0.50.0"