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