Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.

Commit

Permalink
Futurize (stage 1) add #33
Browse files Browse the repository at this point in the history
  • Loading branch information
zrzka committed Nov 3, 2017
1 parent dea007a commit 2eb714c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## master (unreleased)

*
* Bundle `refactoring` enhanced with `future`, `libfuturize`, `libpasteurize` modules
* `script/refactoring/futurize.py` introduced (see [Python Future](https://github.com/PythonCharmers/python-future))
* Equivalent of `futurize -1 --all-imports -n -w --add-suffix 3 $editor.get_path()` (Stage 1 only)
* When futurizer ends, editor text is replaced with content of the `.py3` and `.py3` is trashed
* You can run futurizer script with `Cmd Option F`

## 1.4.0 (2017-11-01)

Expand Down
2 changes: 2 additions & 0 deletions blackmamba/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def _register_default_key_commands():
'refactoring/organize_imports.py', 'Refactor - Organize imports'),
('E', UIKeyModifier.command | UIKeyModifier.alternate,
'refactoring/expand_star_imports.py', 'Refactor - Expand star imports'),
('F', UIKeyModifier.command | UIKeyModifier.alternate,
'refactoring/futurize.py', 'Refactor - Futurize'),

# Still keyboard only
('0', UIKeyModifier.command,
Expand Down
2 changes: 1 addition & 1 deletion blackmamba/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
_BUNDLES = {
'analyze': ['mccabe', 'flake8', 'pycodestyle', 'pyflakes', 'setuptools'],
'docutils': ['docutils'],
'refactoring': ['rope']
'refactoring': ['rope', 'future', 'libfuturize', 'libpasteurize']
}

_BUNDLED_MODULES_PATH = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib'))
Expand Down
60 changes: 60 additions & 0 deletions blackmamba/script/refactoring/futurize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!python3

import console
import os
import unittest.mock as mock

import blackmamba.ide.source as source
import blackmamba.ide.tab as tab
import editor


_SUFFIX = '3'


def _replace_file_content(path):
futurized_path = '{}{}'.format(path, _SUFFIX)

if not os.path.exists(futurized_path):
# This function is not called unless result is 0, thus if file doesn't
# exist, we can assume that there's no need to change anything -> success
return True

line_number = source.get_line_number()
content = editor.get_text()

if not line_number or content is None:
os.remove(futurized_path)
return False

new_content = open(futurized_path, 'r').read()
editor.replace_text(0, len(content) - 1, new_content)
source.scroll_to_line(line_number)
os.remove(futurized_path)
return True


# I'm lazy one, cripples Pythonista logging
@mock.patch('logging.basicConfig')
def main(mock):
import libfuturize.main

path = editor.get_path()
if not path or not path.endswith('.py'):
console.hud_alert('Not a Python file', 'error')
return

tab.save()
args = ['-1', '-n', '-w', '--all-imports', '--add-suffix', _SUFFIX, path]

result = libfuturize.main.main(args)
if result == 0 and _replace_file_content(path):
console.hud_alert('Futurized')
else:
console.hud_alert('Failed to futurize', 'error')


if __name__ == '__main__':
from blackmamba.bundle import bundle
with bundle('refactoring'):
main()

0 comments on commit 2eb714c

Please sign in to comment.