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

Initial Python formatting support #3349

Closed
wants to merge 80 commits into
base: master
from
Commits
Jump to file or symbol
Failed to load files and symbols.
+4,219 −9
Diff settings

Always

Just for now

View
@@ -13,3 +13,5 @@
.DS_Store
coverage
.idea
__pycache__/
*.pyc

This comment has been minimized.

@azz

azz Dec 16, 2017

Member

nit: newline here

@azz

azz Dec 16, 2017

Member

nit: newline here

View
@@ -7,13 +7,23 @@ cache:
yarn: true
directories:
- node_modules
- /home/travis/.pyenv_cache
env:
- NODE_ENV=development
- NODE_ENV=production
matrix:
fast_finish: true
install:
- NODE_ENV=development yarn install
before_install:
- export PYTHON_BUILD_CACHE_PATH="/home/travis/.pyenv_cache"
- curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
- export PATH="/home/travis/.pyenv/bin:$PATH"
- eval "$(pyenv init -)"
- eval "$(pyenv virtualenv-init -)"
- pyenv install -s 2.7.11
- pyenv install -s 3.6.3
- pyenv global 2.7.11 3.6.3
before_script:
- yarn check-deps
- if [ "${NODE_ENV}" = "production" ]; then yarn build; fi
@@ -14,7 +14,8 @@ const parsers = [
"graphql",
"postcss",
"parse5",
"markdown"
"markdown",
"python"

This comment has been minimized.

@lipis

lipis Dec 12, 2017

Member

Shall we do it alphabetically?! Scales better..

@lipis

lipis Dec 12, 2017

Member

Shall we do it alphabetically?! Scales better..

This comment has been minimized.

@azz

azz Dec 12, 2017

Member

At the rate we add parsers I really don't think it matters

@azz

azz Dec 12, 2017

Member

At the rate we add parsers I really don't think it matters

];
function pipe(string) {
View
@@ -15,7 +15,8 @@ const parsers = [
"graphql",
"postcss",
"parse5",
"markdown"
"markdown",
"python"
];
process.env.PATH += path.delimiter + path.join(rootDir, "node_modules", ".bin");
View
@@ -58,7 +58,9 @@ function massageAST(ast, parent) {
"trailingComma",
"parent",
"prev",
"position"
"position",
"lineno",
"col_offset"
].forEach(name => {
delete newObj[name];
});
View
@@ -232,7 +232,8 @@ const detailedOptions = normalizeDetailedOptions({
"scss",
"json",
"graphql",
"markdown"
"markdown",
"python"
],
description: "Which parser to use.",
getter: (value, argv) => (argv["flow-parser"] ? "flow" : value)
View
@@ -22,7 +22,8 @@ const defaults = {
requirePragma: false,
semi: true,
proseWrap: "preserve",
arrowParens: "avoid"
arrowParens: "avoid",
pythonVersion: ""
};
const exampleConfig = Object.assign({}, defaults, {
@@ -60,6 +61,10 @@ function normalize(options) {
normalized.trailingComma = "none";
}
if (normalized.parser === "python" && !normalized.tabWidth) {
normalized.tabWidth = 4;
}
/* istanbul ignore if */
if (typeof normalized.trailingComma === "boolean") {
// Support a deprecated boolean type for the trailing comma config
View
@@ -0,0 +1,36 @@
"use strict";
const spawnSync = require("child_process").spawnSync;
const path = require("path");
function parseText(text, pythonExecutable) {
const executionResult = spawnSync(
pythonExecutable,
[path.join(__dirname, "../vendor/python/astexport.py")],
{
input: text
}
);
const error = executionResult.stderr.toString();
if (error) {
throw new Error(error);
}
return executionResult;
}
function parse(text, parsers, opts) {
const pythonExectuable = `python${opts.pythonVersion}`;

This comment has been minimized.

@azz

azz Dec 16, 2017

Member

Do we want do something like:

  • try parse with "python3",
  • if error, then try parse with "python"

We actually do this with TypeScript ts and tsx.

Then we wouldn't need an option here.

@azz

azz Dec 16, 2017

Member

Do we want do something like:

  • try parse with "python3",
  • if error, then try parse with "python"

We actually do this with TypeScript ts and tsx.

Then we wouldn't need an option here.

This comment has been minimized.

@patrick91

patrick91 Dec 16, 2017

If not python version is specified it will use python directly, which means that will use the default python in your current environment, could be python2 or python3 depending on your setup (eg if you are in a virtualenv).

The option is mainly to run tests with both python2 and python3 :)

@patrick91

patrick91 Dec 16, 2017

If not python version is specified it will use python directly, which means that will use the default python in your current environment, could be python2 or python3 depending on your setup (eg if you are in a virtualenv).

The option is mainly to run tests with both python2 and python3 :)

This comment has been minimized.

@azz

azz Dec 16, 2017

Member

Ok cool. So we don't need to document it.

@azz

azz Dec 16, 2017

Member

Ok cool. So we don't need to document it.

const executionResult = parseText(text, pythonExectuable);
const ast = JSON.parse(executionResult.stdout.toString());
// TODO: add comments
ast.comments = [];
return ast;
}
module.exports = parse;
View
@@ -33,6 +33,9 @@ const parsers = {
},
get markdown() {
return eval("require")("./parser-markdown");
},
get python() {
return eval("require")("./parser-python");
}
};
Oops, something went wrong.