Conditions | 16 |
Total Lines | 150 |
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 neditor.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 |
||
35 | def message_window(self, event=None): |
||
36 | """ツリービューを右クリックしたときの処理. |
||
37 | |||
38 | ・子アイテムならば削除ダイアログを表示する。 |
||
39 | 親アイテムならば追加を行う。 |
||
40 | |||
41 | Args: |
||
42 | event (instance): tkinter.Event のインスタンス |
||
43 | """ |
||
44 | # 選択アイテムの認識番号取得 |
||
45 | curItem = self.APP.tree.focus() |
||
46 | # 親アイテムの認識番号取得 |
||
47 | parentItem = self.APP.tree.parent(curItem) |
||
48 | # 親アイテムをクリックしたとき |
||
49 | if self.APP.tree.item(curItem)["text"] == self.tree_folder[4][1]: |
||
50 | # imageタグを選択したとき |
||
51 | fTyp = [(u'小説エディタ', '*.gif')] |
||
52 | iDir = os.path.abspath(os.path.dirname(__file__)) |
||
53 | filepath = filedialog.askopenfilename( |
||
54 | filetypes=fTyp, |
||
55 | initialdir=iDir |
||
56 | ) |
||
57 | # ファイル名があるとき |
||
58 | if not filepath == "": |
||
59 | file_name = os.path.splitext(os.path.basename(filepath))[0] |
||
60 | path = "./{0}/{1}.gif".format( |
||
61 | self.tree_folder[4][0], |
||
62 | file_name |
||
63 | ) |
||
64 | shutil.copy2(filepath, path) |
||
65 | self.APP.cwc.frame_image() |
||
66 | path = "./{0}/{1}.txt".format( |
||
67 | self.tree_folder[4][0], |
||
68 | file_name |
||
69 | ) |
||
70 | tree = self.APP.tree.insert( |
||
71 | self.tree_folder[4][0], |
||
72 | 'end', |
||
73 | text=file_name |
||
74 | ) |
||
75 | self.APP.tree.see(tree) |
||
76 | self.APP.tree.selection_set(tree) |
||
77 | self.APP.tree.focus(tree) |
||
78 | self.select_list_item_input(file_name) |
||
79 | fm.FileMenuClass.now_path = path |
||
80 | f = open(path, 'w', encoding='utf-8') |
||
81 | self.APP.spc.zoom = 100 |
||
82 | f.write(str(self.APP.spc.zoom)) |
||
83 | f.close() |
||
84 | self.APP.cwc.frame_image() |
||
85 | self.path_read_image( |
||
86 | self.tree_folder[4][0], |
||
87 | file_name, |
||
88 | 0 |
||
89 | ) |
||
90 | |||
91 | else: |
||
92 | if str( |
||
93 | self.APP.tree.item(curItem)["text"] |
||
94 | ) and (not str( |
||
95 | self.APP.tree.item(parentItem)["text"] |
||
96 | ) |
||
97 | ): |
||
98 | # サブダイヤログを表示する |
||
99 | title = u'{0}に挿入'.format(self.APP.tree.item(curItem)["text"]) |
||
100 | dialog = MyDialogClass(self.APP, "挿入", True, title, False) |
||
101 | self.MASTER.wait_window(dialog.sub_name_win) |
||
102 | file_name = dialog.txt |
||
103 | del dialog |
||
104 | if not file_name == "": |
||
105 | self.APP.fmc.open_file_save(fm.FileMenuClass.now_path) |
||
106 | curItem = self.APP.tree.focus() |
||
107 | text = self.APP.tree.item(curItem)["text"] |
||
108 | path = "" |
||
109 | tree = "" |
||
110 | # 選択されているフォルダを見つける |
||
111 | for val in self.tree_folder: |
||
112 | if text == val[1]: |
||
113 | if val[0] == self.tree_folder[0][0]: |
||
114 | self.APP.cwc.frame_character() |
||
115 | self.APP.txt_yobi_name.insert( |
||
116 | tk.END, |
||
117 | file_name |
||
118 | ) |
||
119 | else: |
||
120 | self.APP.cwc.frame() |
||
121 | |||
122 | path = "./{0}/{1}.txt".format(val[0], file_name) |
||
123 | tree = self.APP.tree.insert( |
||
124 | val[0], |
||
125 | 'end', |
||
126 | text=file_name |
||
127 | ) |
||
128 | fm.FileMenuClass.now_path = path |
||
129 | break |
||
130 | |||
131 | # パスが存在すれば新規作成する |
||
132 | if not path == "": |
||
133 | f = open(path, 'w', encoding='utf-8') |
||
134 | f.write("") |
||
135 | f.close() |
||
136 | # ツリービューを選択状態にする |
||
137 | self.APP.tree.see(tree) |
||
138 | self.APP.tree.selection_set(tree) |
||
139 | self.APP.tree.focus(tree) |
||
140 | self.select_list_item_input(file_name) |
||
141 | self.APP.winfo_toplevel().title( |
||
142 | u"小説エディタ\\{0}\\{1}" |
||
143 | .format(text, file_name) |
||
144 | ) |
||
145 | self.APP.text.focus() |
||
146 | # テキストを読み取り専用を解除する |
||
147 | self.APP.text.configure(state='normal') |
||
148 | self.APP.hpc.create_tags() |
||
149 | # 子アイテムを右クリックしたとき |
||
150 | else: |
||
151 | if str(self.APP.tree.item(curItem)["text"]): |
||
152 | # 項目を削除する |
||
153 | file_name = self.APP.tree.item(curItem)["text"] |
||
154 | text = self.APP.tree.item(parentItem)["text"] |
||
155 | # OK、キャンセルダイアログを表示し、OKを押したとき |
||
156 | if messagebox.askokcancel( |
||
157 | u"項目削除", |
||
158 | "{0}を削除しますか?".format(file_name) |
||
159 | ): |
||
160 | image_path = "" |
||
161 | path = "" |
||
162 | # パスを取得する |
||
163 | for val in self.tree_folder: |
||
164 | if text == val[1]: |
||
165 | path = "./{0}/{1}.txt".format( |
||
166 | val[0], |
||
167 | file_name |
||
168 | ) |
||
169 | image_path = "./{0}/{1}.gif".format( |
||
170 | val[0], |
||
171 | file_name |
||
172 | ) |
||
173 | self.APP.tree.delete(curItem) |
||
174 | fm.FileMenuClass.now_path = "" |
||
175 | break |
||
176 | # imageパスが存在したとき |
||
177 | if os.path.isfile(image_path): |
||
178 | os.remove(image_path) |
||
179 | |||
180 | # パスが存在したとき |
||
181 | if not path == "": |
||
182 | os.remove(path) |
||
183 | self.APP.cwc.frame() |
||
184 | self.APP.text.focus() |
||
185 | |||
458 |