| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,8 @@ | ||
| import ast | ||
| import fileinput | ||
| import json | ||
| import tokenize | ||
|
|
||
| import asttokens | ||
|
|
||
|
|
||
| def export_json(atok, pretty_print=False): | ||
| dict = export_dict(atok) | ||
| dict['comments'] = [{ | ||
| 'ast_type': 'comment', | ||
| 'value': token.string, | ||
| 'start': token.startpos, | ||
| 'end': token.endpos, | ||
| } for token in atok.tokens if token.type == 57] | ||
| return json.dumps( | ||
| dict, | ||
| indent=4 if pretty_print else None, | ||
| sort_keys=True, | ||
| separators=(",", ": ") if pretty_print else (",", ":") | ||
| ) | ||
|
|
||
|
|
||
| def export_dict(atok): | ||
| return DictExportVisitor(atok).visit(atok.tree) | ||
|
|
||
|
|
||
| class DictExportVisitor: | ||
| class DictExportVisitor(object): | ||
| ast_type_field = "ast_type" | ||
|
|
||
| def __init__(self, atok): | ||
| Expand Down Expand Up | @@ -102,21 +79,17 @@ def visit_field_Num_n(self, val): | |
| } | ||
|
|
||
|
|
||
| def parse(source): | ||
| assert (isinstance(source, str)) | ||
|
|
||
| atok = asttokens.ASTTokens(source, parse=True) | ||
|
|
||
| return atok | ||
|
|
||
|
|
||
| def main(): | ||
| source = "".join(fileinput.input()) | ||
|
|
||
| tree = parse(source) | ||
| json = export_json(tree, True) | ||
| print(json) | ||
| def export(atok): | ||
| exported_ast = DictExportVisitor(atok).visit(atok.tree) | ||
|
|
||
| exported_ast['comments'] = [ | ||
| { | ||
| 'ast_type': 'comment', | ||
| 'value': token.string, | ||
| 'start': token.startpos, | ||
| 'end': token.endpos, | ||
| } | ||
| for token in atok.tokens if token.type == tokenize.COMMENT | ||
|
Comment thread
Copy link
Copy Markdown
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualityresolves https://github.com/prettier/prettier-python/pull/9/files#r158746577 and correspondingly fixes comment handling in python 2
Sorry, something went wrong.
All reactions
|
||
| ] | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| return exported_ast | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import os | ||
| import sys | ||
|
|
||
| base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
|
||
| sys.path.insert(0, os.path.join(base_dir, 'vendor')) | ||
|
Comment thread
Copy link
Copy Markdown
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI've seen this pattern used elsewhere for CLI-y things where messing with the global Python path isn't a problem. I like that this hides the vendoring details from the caller. It's also good to split out vendored things from things that are patched from upstream.
Sorry, something went wrong.
All reactions
|
||
| sys.path.insert(0, os.path.join(base_dir, 'patched')) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import json | ||
| import sys | ||
|
|
||
| import astexport | ||
| import asttokens | ||
|
|
||
|
|
||
| def main(): | ||
| source = sys.stdin.read() | ||
| atok = asttokens.ASTTokens(source, parse=True) | ||
|
|
||
| exported_ast = astexport.export(atok) | ||
| print(json.dumps( | ||
| exported_ast, | ||
| indent=4, | ||
| sort_keys=True, | ||
| separators=(',', ': '), | ||
| )) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -3,31 +3,23 @@ | |
| 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); | ||
| function parse(text) { | ||
| const executionResult = spawnSync("python", ["-m", "prettier.parser"], { | ||
|
Comment thread
Copy link
Copy Markdown
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualityyou get a bit more flexibility when you run something as a module... you can do things like relative imports, which are sorta handy
Sorry, something went wrong.
patrick91 reacted with thumbs up emoji
All reactions
|
||
| env: { | ||
| PYTHONPATH: [ | ||
| path.join(__dirname, "../python"), | ||
| process.env.PYTHONPATH | ||
| ].join(path.delimiter) | ||
| }, | ||
| input: text | ||
| }); | ||
|
|
||
| if (executionResult.status) { | ||
| throw new Error(executionResult.stderr.toString()); | ||
| } | ||
|
|
||
| return executionResult; | ||
| } | ||
|
|
||
| function parse(text, parsers, opts) { | ||
| const pythonExectuable = `python${opts.pythonVersion == "2" ? "" : "3"}`; | ||
| const executionResult = parseText(text, pythonExectuable); | ||
|
|
||
| const res = executionResult.stdout.toString(); | ||
| const ast = JSON.parse(res); | ||
| return ast; | ||
| return JSON.parse(res); | ||
| } | ||
|
|
||
| module.exports = parse; | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityTurns out pyenv is already available on Travis (even when the language is node_js), and they have a decent selection of versions already installed.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.