-
Notifications
You must be signed in to change notification settings - Fork 8
/
makerRenameFile.py
executable file
·203 lines (162 loc) · 6.15 KB
/
makerRenameFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import sys
import wx
import shutil
import makerController
from makerUtilities import readFile, writeFile
from makerWidgets import MakerDialog
class RenameDialog(MakerDialog):
def __init__(self, parentView):
self.createDialog(parentView)
def createDialog(self, prnt):
MakerDialog.__init__(
self,
{
"name": "",
"parent": prnt,
"pos": wx.Point(259, 72),
"size": wx.Size(400, 180),
"style": wx.DEFAULT_DIALOG_STYLE,
"title": "Rename File",
"clientSize": wx.Size(400, 120),
"centerPos": wx.BOTH,
},
)
self.warning = self.add(
"staticText",
{
"label": "",
"name": "warning",
"parent": self,
"pos": wx.Point(20, 18),
"size": wx.Size(128, 25),
"style": 0,
},
)
# make warning label RED
self.warning.SetForegroundColour(wx.RED)
self.Ok = self.add(
"buttonNoHandler",
{
"label": "Rename",
"name": "rename",
"parent": self,
"pos": wx.Point(300, 48),
"size": wx.Size(80, 25),
"style": 0,
},
)
self.newName = self.add(
"textCtrl",
{
"name": "new_name",
"parent": self,
"pos": wx.Point(20, 48),
"size": wx.Size(250, 25),
"style": 0,
"value": "newName",
},
)
class Controller(makerController.SuperController):
def bindActions(self):
self.view.Ok.Bind(wx.EVT_BUTTON, self.onOk)
self.view.newName.Bind(wx.EVT_TEXT, self.checkNewName)
self.view.Show()
def checkNewName(self, event):
if not self.view.Ok.IsEnabled():
self.view.Ok.Enable()
self.view.warning.SetLabel("")
def setNewName(self, name):
self.view.newName.SetValue(name)
def getNewName(self):
return self.view.newName.GetValue()
def onOk(self, event):
langTypes = [".content", ".dynamic"]
if self.model.fileModel.getType() in langTypes:
if not self.getNewName().endswith("_" + self.model.fileModel.getLanguage()):
self.view.newName.AppendText("_" + self.model.fileModel.getLanguage())
oldFilename = self.model.fileModel.getRealName()
newName = self.getNewName()
nName = os.path.join(
self.model.fileModel.core.getPathParts(),
newName + self.model.fileModel.getType(),
)
if os.path.isfile(nName):
self.view.warning.SetLabel("Filename exists")
self.view.Ok.Disable()
return
else:
self.model.rename()
self.model.fileController.view.tree.SetItemText(
self.model.fileController.getReferringTreeItem(), newName
)
newName = self.model.fileModel.getFileName()
page = self.model.fileController.noteBook.GetSelection()
pageText = self.model.fileController.noteBook.GetPageText(page)
if not self.model.fileModel.getSaved():
newName += "*"
self.model.fileController.noteBook.SetPageText(
self.model.fileController.noteBook.GetSelection(), newName
)
self.view.Destroy()
def destroyView(self):
self.view.DestroyChildren()
self.view.Destroy()
class MakerFileRename:
def __init__(self, fileModel, mainView):
nameView = RenameDialog(mainView)
self.fileModel = fileModel
self.fileController = self.fileModel.fileController
self.controller = Controller(self, nameView)
self.controller.setNewName(self.fileModel.getName())
def rename(self):
oldFilename = self.fileModel.getFileName()
oldRealName = self.fileModel.getRealName()
newName = self.controller.getNewName()
nName = os.path.join(
self.fileModel.core.getPathParts(), newName + self.fileModel.getType()
)
fName = os.path.join(self.fileModel.core.getPathParts(), oldFilename)
if self.fileModel.getType() == ".content":
os.rename(fName, nName)
os.rename(
os.path.join(
self.fileModel.core.getPathParts(),
self.fileModel.getName() + ".head",
),
os.path.join(self.fileModel.core.getPathParts(), newName + ".head"),
)
fileName = self.fileModel.getFullName()
if os.path.isfile(fileName):
os.remove(fileName)
elif self.fileModel.getType() == ".dynamic":
os.rename(fName, nName)
else:
os.rename(fName, nName)
self.fileModel.setName(newName)
distContent = readFile(self.fileModel.core.getDistributionTableFilename())
if self.fileModel.getType() == ".content":
newDist = distContent.replace(oldRealName, newName + ".htm")
else:
newDist = distContent.replace(
oldFilename, newName + self.fileModel.getType()
)
writeFile(self.fileModel.core.getDistributionTableFilename(), newDist)
if self.fileModel.getType() == ".content":
quotes = ["'", '"']
for q in quotes:
this = q + oldRealName + q
that = q + newName + ".htm" + q
self.fileModel.core.replaceStringInAllItems(this, that)
else:
quotes = ["'", '"']
for q in quotes:
this = q + oldFilename + q
that = q + newName + self.fileModel.getType() + q
# Brinick: why is this done twice?
# Gerald once for single , once for double quotes
self.fileModel.core.replaceStringInAllItems(this, that)
# update in ftp queue
self.fileModel.core.renameItemInFtpQueue(
oldFilename, newName + self.fileModel.getType()
)