Conditions | 16 |
Total Lines | 151 |
Code Lines | 106 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like LM.ListMenuClass.message_window() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python3 |
||
105 | def message_window(self, event=None): |
||
106 | """ツリービューを右クリックしたときの処理. |
||
107 | |||
108 | ・子アイテムならば削除ダイアログを表示する。 |
||
109 | 親アイテムならば追加を行う。 |
||
110 | |||
111 | Args: |
||
112 | event (instance): tkinter.Event のインスタンス |
||
113 | |||
114 | """ |
||
115 | # 選択アイテムの認識番号取得 |
||
116 | curItem = self.APP.tree.focus() |
||
117 | # 親アイテムの認識番号取得 |
||
118 | parentItem = self.APP.tree.parent(curItem) |
||
119 | # 親アイテムをクリックしたとき |
||
120 | if self.APP.tree.item(curItem)["text"] == self.tree_folder[4][1]: |
||
121 | # imageタグを選択したとき |
||
122 | fTyp = [(u'小説エディタ', '*.gif')] |
||
123 | iDir = os.path.abspath(os.path.dirname(__file__)) |
||
124 | filepath = filedialog.askopenfilename( |
||
125 | filetypes=fTyp, |
||
126 | initialdir=iDir |
||
127 | ) |
||
128 | # ファイル名があるとき |
||
129 | if not filepath == "": |
||
130 | file_name = os.path.splitext(os.path.basename(filepath))[0] |
||
131 | path = "./{0}/{1}.gif".format( |
||
132 | self.tree_folder[4][0], |
||
133 | file_name |
||
134 | ) |
||
135 | shutil.copy2(filepath, path) |
||
136 | self.APP.cwc.frame_image() |
||
137 | path = "./{0}/{1}.txt".format( |
||
138 | self.tree_folder[4][0], |
||
139 | file_name |
||
140 | ) |
||
141 | tree = self.APP.tree.insert( |
||
142 | self.tree_folder[4][0], |
||
143 | 'end', |
||
144 | text=file_name |
||
145 | ) |
||
146 | self.APP.tree.see(tree) |
||
147 | self.APP.tree.selection_set(tree) |
||
148 | self.APP.tree.focus(tree) |
||
149 | self.select_list_item = file_name |
||
150 | self.APP.fmc.now_path = path |
||
151 | f = open(path, 'w', encoding='utf-8') |
||
152 | self.APP.sfc.zoom = 100 |
||
153 | f.write(str(self.APP.sfc.zoom)) |
||
154 | f.close() |
||
155 | self.APP.cwc.frame_image() |
||
156 | self.path_read_image( |
||
157 | self.tree_folder[4][0], |
||
158 | file_name, |
||
159 | 0 |
||
160 | ) |
||
161 | |||
162 | else: |
||
163 | if str( |
||
164 | self.APP.tree.item(curItem)["text"] |
||
165 | ) and (not str( |
||
166 | self.APP.tree.item(parentItem)["text"] |
||
167 | ) |
||
168 | ): |
||
169 | # サブダイヤログを表示する |
||
170 | title = u'{0}に挿入'.format(self.APP.tree.item(curItem)["text"]) |
||
171 | dialog = MyDialogClass(self.APP, "挿入", True, title, False) |
||
172 | self.MASTER.wait_window(dialog.sub_name_win) |
||
173 | file_name = dialog.txt |
||
174 | del dialog |
||
175 | if not file_name == "": |
||
176 | self.APP.fmc.open_file_save(self.APP.fmc.now_path) |
||
177 | curItem = self.APP.tree.focus() |
||
178 | text = self.APP.tree.item(curItem)["text"] |
||
179 | path = "" |
||
180 | tree = "" |
||
181 | # 選択されているフォルダを見つける |
||
182 | for val in self.tree_folder: |
||
183 | if text == val[1]: |
||
184 | if val[0] == self.tree_folder[0][0]: |
||
185 | self.APP.cwc.frame_character() |
||
186 | self.APP.txt_yobi_name.insert( |
||
187 | tk.END, |
||
188 | file_name |
||
189 | ) |
||
190 | else: |
||
191 | self.APP.cwc.frame() |
||
192 | |||
193 | path = "./{0}/{1}.txt".format(val[0], file_name) |
||
194 | tree = self.APP.tree.insert( |
||
195 | val[0], |
||
196 | 'end', |
||
197 | text=file_name |
||
198 | ) |
||
199 | self.APP.fmc.now_path = path |
||
200 | break |
||
201 | |||
202 | # パスが存在すれば新規作成する |
||
203 | if not path == "": |
||
204 | f = open(path, 'w', encoding='utf-8') |
||
205 | f.write("") |
||
206 | f.close() |
||
207 | # ツリービューを選択状態にする |
||
208 | self.APP.tree.see(tree) |
||
209 | self.APP.tree.selection_set(tree) |
||
210 | self.APP.tree.focus(tree) |
||
211 | self.select_list_item = file_name |
||
212 | self.APP.winfo_toplevel().title( |
||
213 | u"小説エディタ\\{0}\\{1}" |
||
214 | .format(text, file_name) |
||
215 | ) |
||
216 | self.APP.text.focus() |
||
217 | # テキストを読み取り専用を解除する |
||
218 | self.APP.text.configure(state='normal') |
||
219 | self.APP.hpc.create_tags() |
||
220 | # 子アイテムを右クリックしたとき |
||
221 | else: |
||
222 | if str(self.APP.tree.item(curItem)["text"]): |
||
223 | # 項目を削除する |
||
224 | file_name = self.APP.tree.item(curItem)["text"] |
||
225 | text = self.APP.tree.item(parentItem)["text"] |
||
226 | # OK、キャンセルダイアログを表示し、OKを押したとき |
||
227 | if messagebox.askokcancel( |
||
228 | u"項目削除", |
||
229 | "{0}を削除しますか?".format(file_name) |
||
230 | ): |
||
231 | image_path = "" |
||
232 | path = "" |
||
233 | # パスを取得する |
||
234 | for val in self.tree_folder: |
||
235 | if text == val[1]: |
||
236 | path = "./{0}/{1}.txt".format( |
||
237 | val[0], |
||
238 | file_name |
||
239 | ) |
||
240 | image_path = "./{0}/{1}.gif".format( |
||
241 | val[0], |
||
242 | file_name |
||
243 | ) |
||
244 | self.APP.tree.delete(curItem) |
||
245 | self.APP.fmc.now_path = "" |
||
246 | break |
||
247 | # imageパスが存在したとき |
||
248 | if os.path.isfile(image_path): |
||
249 | os.remove(image_path) |
||
250 | |||
251 | # パスが存在したとき |
||
252 | if not path == "": |
||
253 | os.remove(path) |
||
254 | self.APP.cwc.frame() |
||
255 | self.APP.text.focus() |
||
256 | |||
443 |