Last active
February 24, 2020 18:03
-
-
Save andreberg/5664538 to your computer and use it in GitHub Desktop.
[Aptana Studio 3: Convert print to print()] Aptana Studio 3 ruble commands which convert between Python 2 and Python 3 print statement syntax using lib2to3 and lib3to2. #aptanastudio #ruble #command #conversion #syntax #userscript #python #python3
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
#!/usr/local/bin/python | |
# -*- coding: utf-8 -*- | |
# | |
# convert_print.py | |
# Aptana Ruble Commands | |
# | |
# Created by André Berg on 28.05.2013. | |
# Copyright 2013 Berg Media. All rights reserved. | |
# | |
# Used from a Python.ruble command in Eclipse | |
# Aptana Studio 3: | |
# | |
# require 'ruble' | |
# | |
# command t(:convert_print_2to3) do |cmd| | |
# cmd.output = :replace_selection | |
# cmd.input = :selection | |
# cmd.invoke do |context| | |
# text = "#{STDIN.read.gsub('\\', '\\\\').gsub('"', '\\"')}" | |
# return `"${TM_PYTHON:-python}" "${TM_BUNDLE_SUPPORT}/convert_print.py" "-3" "#{text}"`.rstrip | |
# end | |
# end | |
from __future__ import print_function | |
import io | |
import os | |
import sys | |
import tempfile | |
from contextlib import contextmanager | |
from lib2to3.main import main as to3_main | |
try: | |
from lib3to2.main import main as to2_main | |
IS_LIB3TO2_SUPPORTED = True | |
except ImportError: | |
err_msg = ("W: lib3to2 not found. -2 direction not supported. " | |
"(install '3to2' or '3to2_py3k' via pip or easy_install)") | |
print(err_msg, file=sys.stderr) | |
IS_LIB3TO2_SUPPORTED = False | |
if sys.version_info < (3, 0): | |
IS_PYTHON3 = False | |
else: | |
IS_PYTHON3 = True | |
class DummyFile(object): | |
def write(self, x): | |
pass | |
@contextmanager | |
def muted_stderr(): | |
saved_stderr = sys.stderr | |
sys.stderr = DummyFile() | |
yield | |
sys.stderr = saved_stderr | |
def convert_print(text, target=3, encoding=sys.getdefaultencoding()): | |
if not IS_PYTHON3: | |
try: | |
text = unicode(text.encode(encoding)) | |
except UnicodeDecodeError as ude: | |
# FIXME: on Windows, Pydev passes Run Config args as "cp1252" encoded. | |
# Frankly, I have no idea why Pydev is passing these arguments | |
# with the platform default encoding and not the encoding set in | |
# either Eclipse prefs, project prefs, file prefs, file header | |
# comment or even from sitecustomize.py or pydev_sitecustomize.py. | |
# I am assuming it has to do with Jython being used to determine | |
# the PYTHONPATH for the execution. | |
if sys.platform.startswith("win"): | |
text = unicode(text.decode("cp1252").encode(encoding)) | |
else: | |
raise ude | |
if target == 3: | |
_main = to3_main | |
module_name = "lib2to3.fixes" | |
elif target == 2: | |
_main = to2_main | |
module_name = "lib3to2.fixes" | |
else: | |
raise ValueError("E: target must be one of [2, 3], but is {0!r}".format(target)) | |
tmpfile_name = "convert_print_py_scratchfile.txt" | |
tmpdir = tempfile.gettempdir() | |
tmpfile_path = os.path.join(tmpdir, tmpfile_name) | |
with io.open(tmpfile_path, "wt", encoding=encoding) as temp_file: | |
temp_file.write(text) | |
err = None | |
with muted_stderr(): | |
try: | |
_main(module_name, args=["--nobackups", "--write", "--no-diffs", "--fix=print", tmpfile_path]) | |
except Exception as e: # IGNORE:W0703 | |
err = e | |
if err is None: | |
with io.open(tmpfile_path, "rt", encoding=encoding) as fixed_file: | |
result = fixed_file.read() | |
else: | |
raise err # IGNORE:E0702 | |
return result | |
if __name__ == '__main__': | |
program_name = os.path.basename(sys.argv[0]) | |
if IS_LIB3TO2_SUPPORTED: | |
flag_str = "[-2|-3]" | |
else: | |
flag_str = "[-3]" | |
if not len(sys.argv) > 2: | |
print("usage: {0} {1} '<text>'".format(program_name, flag_str)) | |
sys.exit(1) | |
direction = sys.argv[1] | |
if direction == "-2": | |
if not IS_LIB3TO2_SUPPORTED: | |
err_str = ("E: -2 direction not supported, because lib3to2 is missing. " | |
"(install '3to2' or '3to2_py3k' via pip or easy_install)") | |
print(err_str) | |
sys.exit(1) | |
target = 2 | |
elif direction == "-3": | |
target = 3 | |
else: | |
print("E: direction must be one of ['-2', '-3'], but is {0!r}".format(direction), file=sys.stderr) | |
sys.exit(1) | |
text = sys.argv[2] | |
fixed_text = convert_print(text, target=target) | |
if len(fixed_text) < 1: | |
fixed_text = text | |
print(fixed_text) |
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
# encoding: utf-8 | |
require 'ruble' | |
# XXX should probably use Shellwords.shellescape | |
command t(:convert_print_2to3) do |cmd| | |
#cmd.scope = 'source.py' | |
cmd.output = :replace_selection | |
cmd.input = :selection | |
cmd.invoke do |context| | |
text = "#{STDIN.read.gsub('\\', '\\\\').gsub('"', '\\"')}" | |
return `"${TM_PYTHON:-python}" "${TM_BUNDLE_SUPPORT}/convert_print.py" "-3" "#{text}"`.rstrip | |
end | |
end |
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
# encoding: utf-8 | |
require 'ruble' | |
command t(:convert_print_3to2) do |cmd| | |
#cmd.scope = 'source.py' | |
cmd.output = :replace_selection | |
cmd.input = :selection | |
cmd.invoke do |context| | |
text = "#{STDIN.read.gsub('\\', '\\\\').gsub('"', '\\"')}" | |
return `"${TM_PYTHON:-python}" "${TM_BUNDLE_SUPPORT}/convert_print.py" "-2" "#{text}"`.rstrip | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment