Passed
Pull Request — master (#53)
by Yoshihiro
02:11
created

FP   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 296
Duplicated Lines 6.76 %

Importance

Changes 0
Metric Value
eloc 157
dl 20
loc 296
rs 10
c 0
b 0
f 0
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A FindProcessingClass.replacement() 0 36 4
A FindProcessingClass.find_dialog() 0 40 1
A FindProcessingClass.search() 10 28 3
A FindProcessingClass.search_forward() 10 28 3
B FindProcessingClass.replacement_dialog() 0 54 1
A FindProcessingClass.replacement_dialog_on_closing() 0 8 1
A FindProcessingClass.push_keys() 0 11 1
B FindProcessingClass.search_next() 0 55 6
A FindProcessingClass.__init__() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import tkinter as tk
2
import tkinter.ttk as ttk
3
import tkinter.messagebox as messagebox
4
5
6
class FindProcessingClass():
7
    """検索置換のクラス
8
9
    ・検索置換するためのプログラム群
10
11
    Attributes:
12
        replacement_check (bool): 検索ダイアログが表示されているTrue
13
        next_pos (str): 次の検索位置 例.(1.0)
14
        find_text (str): 検索文字列
15
16
    """
17
    def __init__(self, app):
18
        """
19
        Args:
20
            app (instance): lineframeインスタンス
21
22
        """
23
        self.replacement_check = 0
24
        self.next_pos = ""
25
        self.find_text = ""
26
        self.APP = app
27
28
    def push_keys(self, event=None):
29
        """キーが押されたときの処理
30
31
        ・何かキーが押されたときに検索処理を中断する。
32
33
        Args:
34
            event (instance): tkinter.Event のインスタンス
35
36
        """
37
        # 検索処理を中断する
38
        self.replacement_check = 0
39
40
    def find_dialog(self, event=None):
41
        """検索ダイアログを作成
42
43
        ・検索ダイアログを作成する。
44
45
        Args:
46
            event (instance): tkinter.Event のインスタンス
47
48
        """
49
        search_win = tk.Toplevel(self.APP)
50
        self.text_var = ttk.Entry(search_win, width=40)
51
        self.text_var.grid(
52
            row=0,
53
            column=0,
54
            columnspan=2,
55
            padx=5,
56
            pady=5,
57
            sticky=tk.W+tk.E,
58
            ipady=3
59
        )
60
        button = ttk.Button(
61
            search_win,
62
            text=u'検索',
63
            width=str(u'検索'),
64
            padding=(10, 5),
65
            command=self.search
66
        )
67
        button.grid(row=1, column=0)
68
        button2 = ttk.Button(
69
            search_win,
70
            text=u'昇順検索',
71
            width=str(u'昇順検索'),
72
            padding=(10, 5),
73
            command=self.search_forward
74
        )
75
        button2.grid(row=1, column=1)
76
        # 最前面に表示し続ける
77
        search_win.attributes("-topmost", True)
78
        search_win.title(u'検索')
79
        self.text_var.focus()
80
81
    def replacement_dialog(self, event=None):
82
        """置換ダイアログを作成
83
84
        ・置換ダイアログを作成する。
85
86
        Args:
87
            event (instance): tkinter.Event のインスタンス
88
89
        """
90
        self.replacement_win = tk.Toplevel(self.APP)
91
        self.text_var = ttk.Entry(self.replacement_win, width=40)
92
        self.text_var.grid(
93
            row=0,
94
            column=0,
95
            columnspan=2,
96
            padx=5,
97
            pady=5,
98
            sticky=tk.W+tk.E,
99
            ipady=3
100
        )
101
        self.replacement_var = ttk.Entry(self.replacement_win, width=40)
102
        self.replacement_var.grid(
103
            row=1,
104
            column=0,
105
            columnspan=2,
106
            padx=5,
107
            pady=5,
108
            sticky=tk.W+tk.E,
109
            ipady=3
110
        )
111
        button = ttk.Button(
112
            self.replacement_win,
113
            text=u'検索',
114
            width=str(u'検索'),
115
            padding=(10, 5),
116
            command=self.search
117
        )
118
        button.grid(row=2, column=0)
119
        button2 = ttk.Button(
120
            self.replacement_win,
121
            text=u'置換',
122
            width=str(u'置換'),
123
            padding=(10, 5),
124
            command=self.replacement
125
        )
126
        button2.grid(row=2, column=1)
127
        # 最前面に表示し続ける
128
        self.replacement_win.attributes("-topmost", True)
129
        self.replacement_win.title(u'置換')
130
        self.text_var.focus()
131
        # ウインドウが閉じられたときの処理
132
        self.replacement_win.protocol(
133
            "WM_DELETE_WINDOW",
134
            self.replacement_dialog_on_closing
135
            )
136
137
    def replacement_dialog_on_closing(self):
138
        """検索ウインドウが閉じられたときの処理
139
140
        ・検索ダイアログが閉じられたことがわかるようにする。
141
142
        """
143
        self.replacement_check = 0
144
        self.replacement_win.destroy()
145
146
    def search(self, event=None):
147
        """検索処理
148
149
        ・検索処理をする。空欄なら処理しない、違うなら最初から、
150
        同じなら次のを検索する。
151
152
        Args:
153
            event (instance): tkinter.Event のインスタンス
154
155
        """
156
        # 現在選択中の部分を解除
157
        self.APP.text.tag_remove('sel', '1.0', 'end')
158
159
        # 現在検索ボックスに入力されてる文字
160
        now_text = self.text_var.get()
161 View Code Duplication
        if not now_text:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
162
            # 空欄だったら処理しない
163
            pass
164
        elif now_text != self.find_text:
165
            # 前回の入力と違う文字なら、検索を最初から行う
166
            index = '0.0'
167
            self.search_next(now_text, index, 0)
168
        else:
169
            # 前回の入力と同じなら、検索の続きを行う
170
            self.search_next(now_text, self.next_pos, 1)
171
172
        # 今回の入力を、「前回入力文字」にする
173
        self.find_text = now_text
174
175
    def replacement(self, event=None):
176
        """置換処理
177
178
        ・置換処理をする。空欄なら処理しない、違うなら初めから、
179
        同じなら次を検索する。
180
181
        Args:
182
            event (instance): tkinter.Event のインスタンス
183
184
        """
185
        # 現在選択中の部分を解除
186
        self.APP.text.tag_remove('sel', '1.0', 'end')
187
188
        # 現在検索ボックスに入力されてる文字
189
        now_text = self.text_var.get()
190
        replacement_text = self.replacement_var.get()
191
        if not now_text or not replacement_text:
192
            # 空欄だったら処理しない
193
            pass
194
        elif now_text != self.find_text:
195
            # 前回の入力と違う文字なら、検索を最初から行う
196
            self.replacement_check = 1
197
            index = '0.0'
198
            self.search_next(now_text, index, 0)
199
        else:
200
            # 前回の入力と同じなら、検索の続きを行う
201
            self.replacement_check = 1
202
            # 検索文字の置換を行なう
203
            start = self.next_pos
204
            end = '{0} + {1}c'.format(self.next_pos, len(now_text))
205
            self.APP.text.delete(start, end)
206
            self.APP.text.insert(start, replacement_text)
207
            self.search_next(now_text, self.next_pos, 1)
208
209
        # 今回の入力を、「前回入力文字」にする
210
        self.find_text = now_text
211
212
    def search_forward(self, event=None):
213
        """昇順検索処理
214
215
        ・昇順検索をする。空欄なら処理しない、違うなら初めから、
216
        同じなら次を検索する。
217
218
        Args:
219
            event (instance): tkinter.Event のインスタンス
220
221
        """
222
        # 現在選択中の部分を解除
223
        self.APP.text.tag_remove('sel', '1.0', 'end')
224
225
        # 現在検索ボックスに入力されてる文字
226
        now_text = self.text_var.get()
227 View Code Duplication
        if not now_text:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
228
            # 空欄だったら処理しない
229
            pass
230
        elif now_text != self.find_text:
231
            # 前回の入力と違う文字なら、検索を最初から行う
232
            index = 'end'
233
            self.search_next(now_text, index, 2)
234
        else:
235
            # 前回の入力と同じなら、検索の続きを行う
236
            self.search_next(now_text, self.next_pos, 2)
237
238
        # 今回の入力を、「前回入力文字」にする
239
        self.find_text = now_text
240
241
    def search_next(self, search, index, case):
242
        """検索のメイン処理
243
244
        ・検索できれば選択をする。できなければ、ダイアログを出して終了。
245
246
        Args:
247
            search (str): 検索文字列
248
            index (str): 検索位置 ex. (1.0)
249
            case (int): 0.初めから検索 1.次に検索 2.昇順検索
250
251
        """
252
        if case == 2:
253
            backwards = True
254
            stopindex = '0.0'
255
            index = '{0}'.format(index)
256
        elif case == 0:
257
            backwards = False
258
            stopindex = 'end'
259
            index = '{0}'.format(index)
260
        else:
261
            backwards = False
262
            stopindex = 'end'
263
            index = '{0} + 1c'.format(index)
264
265
        pos = self.APP.text.search(
266
            search, index,
267
            stopindex=stopindex,
268
            backwards=backwards
269
        )
270
        if not pos:
271
            if case == 2:
272
                index = "end"
273
            else:
274
                index = "0.0"
275
276
            pos = self.APP.text.search(
277
                search, index,
278
                stopindex=stopindex,
279
                backwards=backwards
280
            )
281
            if not pos:
282
                messagebox.showinfo(
283
                    "検索",
284
                    "最後まで検索しましたが検索文字はありませんでした。"
285
                )
286
                self.replacement_check = 0
287
                return
288
289
        self.next_pos = pos
290
        start = pos
291
        end = '{0} + {1}c'.format(pos, len(search))
292
        self.APP.text.tag_add('sel', start, end)
293
        self.APP.text.mark_set('insert', start)
294
        self.APP.text.see('insert')
295
        self.APP.text.focus()
296