Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/emacs-for-python/rope-dist/rope/refactor/topackage.py
1428 views
1
import rope.refactor.importutils
2
from rope.base.change import ChangeSet, ChangeContents, MoveResource, CreateFolder
3
4
5
class ModuleToPackage(object):
6
7
def __init__(self, project, resource):
8
self.project = project
9
self.pycore = project.pycore
10
self.resource = resource
11
12
def get_changes(self):
13
changes = ChangeSet('Transform <%s> module to package' %
14
self.resource.path)
15
new_content = self._transform_relatives_to_absolute(self.resource)
16
if new_content is not None:
17
changes.add_change(ChangeContents(self.resource, new_content))
18
parent = self.resource.parent
19
name = self.resource.name[:-3]
20
changes.add_change(CreateFolder(parent, name))
21
parent_path = parent.path + '/'
22
if not parent.path:
23
parent_path = ''
24
new_path = parent_path + '%s/__init__.py' % name
25
if self.resource.project == self.project:
26
changes.add_change(MoveResource(self.resource, new_path))
27
return changes
28
29
def _transform_relatives_to_absolute(self, resource):
30
pymodule = self.pycore.resource_to_pyobject(resource)
31
import_tools = rope.refactor.importutils.ImportTools(self.pycore)
32
return import_tools.relatives_to_absolutes(pymodule)
33
34