6

I have my project structure as following

├── app
│   ├── Country
│   │   └── views.py
│   ├── Customer
│   │   └── views.py

Where the module 'Country' folder is what I tried to rename it to 'Countries' and every occurrence it is used, and it is also imported in Customer/views.py as well.

from app.Country.views   import *
....

According to this tutorial Refactoring Python Applications for Simplicity, I tried it as below:

>>> from rope.base.project import Project
>>> 
>>> proj = Project('app')
>>> 
>>> Country = proj.get_folder('Country')
>>> 
>>> from rope.refactor.rename import Rename
>>> 
>>> change = Rename(proj, Country).get_changes('Countries')
>>> proj.do(change)

After executing the script, the module folder 'Country' was changed to 'Countries' but its instance where it is used in Customer/views.py does not change accordingly, the import statement in Customer/views.py is still

from app.Country.views   import *

I expected it should change to from app.Countries.views import * after refactoring, but it did not.

Is there anything else I should do to refactor this successfully? Thanks.

Houy Narun
  • 1,557
  • 5
  • 37
  • 86
  • Have you tried reading `Country` in as a module using `proj.find_module('Country')`? – William Miller Dec 03 '19 at 01:15
  • @WilliamMiller, I have. I follow the steps as described in question but change `Country = proj.find_module('Country')`. It still does not work. the folder changed but the import statement in Customer/views.py did not change accordingly. Thanks. – Houy Narun Dec 03 '19 at 01:53
  • @HouyNarun Can you put your project on GitHub? – aaron Dec 03 '19 at 02:18
  • @aaron, because it involves company credential source code I am sorry for that. However, I will try to simplify it sufficient example for you to test or anything I could do for this? Thanks. – Houy Narun Dec 05 '19 at 02:18

2 Answers2

9

You could use proj.get_module('app.Country').get_resource() to rename module.

from rope.base.project import Project
from rope.refactor.rename import Rename

proj = Project('app')
country = proj.get_module('app.Country').get_resource()
change = Rename(proj, country).get_changes('Countries')
print(change.get_description())
AnnieFromTaiwan
  • 3,845
  • 3
  • 22
  • 38
0

If you happen to work in a virtual environment and/or Django (as the views.py files suggest), you may need to define your PYTHONPATH variable before you start python.

>>> export PYTHONPATH=<path-to-app-folder>:<path-to-virtualen-bin>:<other-paths-used-by-your-project>
>>> python

Then (code from AnnieFromTaiwan is valid, as well as yours I guess, but did not test it):

from rope.base.project import Project
from rope.refactor.rename import Rename

proj = Project('app')
country = proj.get_module('app.Country').get_resource()
change = Rename(proj, country).get_changes('Countries')
proj.do(change)
Bob Morane
  • 183
  • 1
  • 7