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