This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
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() |