├── .gitmodules ├── Default (Linux).sublime-keymap ├── Default (OSX).sublime-keymap ├── Default (Windows).sublime-keymap ├── README.rst └── Tidy.py /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "PythonTidy"] 2 | path = PythonTidy 3 | url = git://github.com/witsch/PythonTidy.git 4 | -------------------------------------------------------------------------------- /Default (Linux).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+shift+alt+t"], "command": "python_tidy" } 3 | ] -------------------------------------------------------------------------------- /Default (OSX).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["super+ctrl+alt+t"], "command": "python_tidy" } 3 | ] -------------------------------------------------------------------------------- /Default (Windows).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+shift+alt+t"], "command": "python_tidy" } 3 | ] -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | This integrates Chuck Rhode's `PythonTidy` script from 2 | http://pypi.python.org/pypi/PythonTidy/ with the Sublime Text 2 editor. 3 | For as long as there is no download available from PyPI, it uses my 4 | Github fork at https://github.com/witsch/PythonTidy 5 | 6 | 7 | Installation 8 | ------------ 9 | 10 | At the moment Git is required to install the plugin. You will need 11 | to clone the repository in your Sublime Text "Packages" directory:: 12 | 13 | $ git clone --recursive https://github.com/witsch/SublimePythonTidy.git 14 | 15 | The "Packages" directory is located at: 16 | 17 | * OS X: `~/Library/Application Support/Sublime Text 2/Packages/` 18 | * Linux: `~/.Sublime Text 2/Packages/` 19 | * Windows: `%APPDATA%/Sublime Text 2/Packages/` 20 | 21 | 22 | Usage 23 | ----- 24 | 25 | To reformat your Python source, simply press the keyboard shortcut. It 26 | is **Ctrl+Alt+Cmd+T** on OSX and **Ctrl+Alt+Shift+T** on 27 | Linux and Windows. 28 | 29 | The output of `PythonTidy` can be tweaked to your individual taste or 30 | company policy via a configuration file stored at `~/.pythontidy.xml`. 31 | The complete set of default settings can be dumped using:: 32 | 33 | $ cd 'Library/Application Support/Sublime Text 2/Packages' 34 | $ python SublimePythonTidy/PythonTidy/runner.py -d 35 | 36 | Example settings are available at https://gist.github.com/1240583 37 | -------------------------------------------------------------------------------- /Tidy.py: -------------------------------------------------------------------------------- 1 | from sublime_plugin import TextCommand 2 | from sublime import Region 3 | from subprocess import call 4 | from os.path import abspath, expanduser, exists, join 5 | from StringIO import StringIO 6 | from sys import path 7 | 8 | 9 | # load the git submodule 10 | extra = abspath('PythonTidy') 11 | if not exists(join(extra, '.git')): 12 | call(['git', 'submodule', 'init']) 13 | call(['git', 'submodule', 'update']) 14 | 15 | # tweak path to allow importing PythonTidy from the git submodule 16 | path.insert(0, extra) 17 | import PythonTidy 18 | import PythonTidyWrapper 19 | path.remove(extra) 20 | 21 | 22 | def setup(): 23 | xml = expanduser('~/.pythontidy.xml') 24 | if exists(xml): 25 | config = PythonTidyWrapper.Config(file=xml) 26 | config.to_pythontidy_namespace() 27 | 28 | 29 | class python_tidy(TextCommand): 30 | 31 | def run(self, edit): 32 | setup() 33 | view = self.view 34 | region = Region(0L, view.size()) 35 | encoding = view.encoding() 36 | if not encoding or encoding == u'Undefined': 37 | encoding = view.settings().get('default_encoding') 38 | source = StringIO(view.substr(region).encode(encoding)) 39 | output = StringIO() 40 | PythonTidy.tidy_up(source, output) 41 | view.replace(edit, region, output.getvalue().decode(encoding)) 42 | 43 | --------------------------------------------------------------------------------