मैं एक स्क्रिप्ट बना दिया है यह संभव कोड फ़ॉर्मेटर रूप Pydev में autopep8 उपयोग करने के लिए बनाने के लिए, और यह कर सकते हैं: PythonTidy (बाहरी उपकरण) पर एक नज़र डालें ... यह संभव इसका इस्तेमाल करने के रूप में परिभाषित किया गया है में अपनी टीम में कोडिंग मानक को संतुष्ट करने के लिए अनुकूलित किया जाना चाहिए।
यदि आप इसका उपयोग करना चाहते हैं, तो इस कोड को कहीं भी pyedit_autopep8.py (pyedit_XXXX.py आवश्यक) के रूप में सहेजें। आपको पाइथन पैकेज पेप 8 और ऑटोपैप 8 भी इंस्टॉल करना होगा। आदेश autopep8 आप बस Ctrl + Shift + F दबा सकते हैं आह्वान करने के लिए में,
अब: स्क्रिप्ट स्थान को निर्दिष्ट करने:
इसके बाद, (विंडो> Preferences> Pydev> पटकथा Pydev पर) Pydev प्राथमिकताएं पृष्ठ ग्रहण करने के लिए जाना ग्रहण में पायथन कोड संपादित करते समय। चयनित पाठ प्रारूप भी समर्थित है!
"""
By Per A. Brodtkorb
based on pyedit_pythontidy.py by Bear Huang (http://bear330.wordpress.com/).
This code is public domain.
"""
import tempfile
import os
if False:
from org.python.pydev.editor import PyEdit # @UnresolvedImport
cmd = 'command string'
editor = PyEdit
assert cmd is not None
assert editor is not None
if cmd == 'onCreateActions':
from org.python.pydev.editor.actions import PyAction
from org.python.pydev.core.docutils import PySelection
from java.lang import Runnable
from org.eclipse.swt.widgets import Display
from java.io import FileWriter
import java.lang.Exception
FORMAT_ACTION_DEFINITION_ID = "org.python.pydev.editor.actions.pyFormatStd"
FORMAT_ACTION_ID = "org.python.pydev.editor.actions.navigation.pyFormatStd"
class Autopep8Action(PyAction):
def _autopep8(self, text):
tmp_full_file_name = tempfile.mktemp()
f1 = FileWriter(tmp_full_file_name)
f1.write(text)
f1.close()
os.system('autopep8-script.py -i "%s"' % (tmp_full_file_name))
f2 = open(tmp_full_file_name, "r")
tidy_text = f2.read()
f2.close()
os.remove(tmp_full_file_name)
return tidy_text
def _get_text(self, selection):
text = selection.getSelectedText()
format_all = len(text) == 0
if format_all:
print "Autopep8: format all."
text = selection.getDoc().get()
text_offset = 0
else:
print "Autopep8: Format selected."
text_offset = selection.getAbsoluteCursorOffset()
return text, text_offset
def run(self):
try:
selection = PySelection(editor)
text, text_offset = self._get_text(selection)
tidy_text = self._autopep8(text)
if len(text)==len(tidy_text):
print "Autopep8: Nothing todo!"
else:
doc = selection.getDoc()
doc.replace(text_offset, len(text), tidy_text)
except java.lang.Exception, e:
self.beep(e)
def bindInInterface():
act = Autopep8Action()
act.setActionDefinitionId(FORMAT_ACTION_DEFINITION_ID)
act.setId(FORMAT_ACTION_ID)
try:
editor.setAction(FORMAT_ACTION_ID, act)
except:
pass
class RunInUi(Runnable):
'''Helper class that implements a Runnable (just so that we
can pass it to the Java side). It simply calls some callable.
'''
def __init__(self, c):
self.callable = c
def run(self):
self.callable()
def runInUi(callable):
'''
@param callable: the callable that will be run in the UI
'''
Display.getDefault().asyncExec(RunInUi(callable))
runInUi(bindInInterface)
नोट के रूप में, वर्तमान में autopep8.py को पीडीडीव में डिफ़ॉल्ट रूप से एकीकृत किया जाता है (इसे केवल वरीयताओं में सक्षम होना आवश्यक है)। –