Python Refactoring with Rope and Traad – The rope library is a powerful tool for refactoring Python code, but to be truly useful it needs to be available to development environments. Traad is a tool which makes it simpler to integrate rope into nearly any tool by exposing a simple HTTP API. In this session we’ll look at how traad and rope work together, and we’ll see how traad integrates with at least one popular editor.
10. RESOURCES
The files and directories in the project
server=project.get_resource('traad/server.py')
print(server.name,
server.path,
server.is_folder())
c1=project.get_resource('').get_children()[0]
print(c1.name,
c1.path,
c1.is_folder())
12. MANAGE PREFERENCES
prefsis a dict-like container of settings and preferences.
forpinproject.prefs.prefs:
print(project.prefs.get(p))
prefsare stored in a file called .ropeproject/config.py
at the project's root directory.
20. UNDOING CHANGES
Undo the most recent changes
project.history.undo()
Or select more distance changes to undo
change=project.history.undo_list[3]
project.history.undo(change)
This will undo all dependent changes.
21. REDOING CHANGES
You can redo changes that you've previously undone.
change=project.history.redo_list[-2]
project.history.redo(change)
23. THE MULTI-PROJECT
REFACTORING CLASS
First create a MultiProjectRefactoringinstance.
fromrope.refactorimportmultiproject
mpr=multiproject.MultiProjectRefactoring(
Rename,#typeofrefactoringtoperform
[other_project1,other_project2,other_project3])
Then call that with your main project and the refactoring
initializer arguments
ref=mpr(main_project,
some_resource,
offset=1337)
24. CREATE AND PERFORM THE CHANGE
#Rememberthat`ref`isarenamingoperation
changes=ref.get_all_changes(
"EnterpriseProxyManagerImplementationFactoryFactory")
multiproject.perform(changes)
44. EMACS
The emacs lisp for renaming looks like this
;Startsnewtraadserveronthespecifieddirectory
(traad-open"~/projects/roid_rage")
;Renameswhateverisathepoint(i.e.underthecursor)
(traad-rename"RagingRoid")
And the HTTP message looks like this
POST/refactor/renameHTTP/1.1
User-Agent:curl/7.30.0
Host:127.0.0.1:65172
Accept:*/*
Accept-Encoding:deflate,gzip
Content-Type:application/json
Content-Length:104
{"name":"RagingRoid",
"path":"/Users/sixtynorth/projects/roid_rage/roid_rage/roid.py",
"offset":102}