| Conditions | 13 |
| Total Lines | 204 |
| Lines | 27 |
| Ratio | 13.24 % |
| Tests | 0 |
| CRAP Score | 182 |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 Application.init() 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 python |
||
| 202 | def init(self, root): |
||
| 203 | """Initialize and return the main frame.""" |
||
| 204 | # Shared arguments |
||
| 205 | width_outline = 20 |
||
| 206 | width_text = 30 |
||
| 207 | width_uid = 10 |
||
| 208 | height_text = 10 |
||
| 209 | height_ext = 5 |
||
| 210 | |||
| 211 | # Shared keyword arguments |
||
| 212 | kw_f = {'padding': 5} # constructor arguments for frames |
||
| 213 | kw_gp = {'padx': 2, 'pady': 2} # grid arguments for padded widgets |
||
| 214 | kw_gs = {'sticky': tk.NSEW} # grid arguments for sticky widgets |
||
| 215 | kw_gsp = dict(chain(kw_gs.items(), kw_gp.items())) # grid arguments for sticky padded widgets |
||
| 216 | |||
| 217 | # Shared style |
||
| 218 | if sys.platform == 'darwin': |
||
| 219 | size = 14 |
||
| 220 | else: |
||
| 221 | size = 10 |
||
| 222 | normal = font.Font(family='TkDefaultFont', size=size) |
||
| 223 | fixed = font.Font(family='Courier New', size=size) |
||
| 224 | |||
| 225 | # Configure grid |
||
| 226 | frame = ttk.Frame(root, **kw_f) |
||
| 227 | frame.rowconfigure(0, weight=0) |
||
| 228 | frame.rowconfigure(1, weight=1) |
||
| 229 | frame.columnconfigure(0, weight=2) |
||
| 230 | frame.columnconfigure(1, weight=1) |
||
| 231 | frame.columnconfigure(2, weight=1) |
||
| 232 | frame.columnconfigure(3, weight=2) |
||
| 233 | |||
| 234 | # Create widgets |
||
| 235 | def frame_project(root): |
||
| 236 | """Frame for the current project.""" |
||
| 237 | View Code Duplication | # Configure grid |
|
|
|
|||
| 238 | frame = ttk.Frame(root, **kw_f) |
||
| 239 | frame.rowconfigure(0, weight=1) |
||
| 240 | frame.columnconfigure(0, weight=0) |
||
| 241 | frame.columnconfigure(1, weight=1) |
||
| 242 | |||
| 243 | # Place widgets |
||
| 244 | ttk.Label(frame, text="Project:").grid(row=0, column=0, **kw_gp) |
||
| 245 | ttk.Entry(frame, textvariable=self.stringvar_project).grid(row=0, column=1, **kw_gsp) |
||
| 246 | |||
| 247 | return frame |
||
| 248 | |||
| 249 | def frame_tree(root): |
||
| 250 | """Frame for the current document.""" |
||
| 251 | View Code Duplication | # Configure grid |
|
| 252 | frame = ttk.Frame(root, **kw_f) |
||
| 253 | frame.rowconfigure(0, weight=1) |
||
| 254 | frame.columnconfigure(0, weight=0) |
||
| 255 | frame.columnconfigure(1, weight=1) |
||
| 256 | |||
| 257 | # Place widgets |
||
| 258 | ttk.Label(frame, text="Document:").grid(row=0, column=0, **kw_gp) |
||
| 259 | self.combobox_documents = ttk.Combobox(frame, textvariable=self.stringvar_document, state='readonly') |
||
| 260 | self.combobox_documents.grid(row=0, column=1, **kw_gsp) |
||
| 261 | |||
| 262 | return frame |
||
| 263 | |||
| 264 | def frame_document(root): |
||
| 265 | """Frame for current document's outline and items.""" |
||
| 266 | # Configure grid |
||
| 267 | frame = ttk.Frame(root, **kw_f) |
||
| 268 | frame.rowconfigure(0, weight=0) |
||
| 269 | frame.rowconfigure(1, weight=5) |
||
| 270 | frame.rowconfigure(2, weight=0) |
||
| 271 | frame.rowconfigure(3, weight=0) |
||
| 272 | frame.columnconfigure(0, weight=0) |
||
| 273 | frame.columnconfigure(1, weight=0) |
||
| 274 | frame.columnconfigure(2, weight=0) |
||
| 275 | frame.columnconfigure(3, weight=0) |
||
| 276 | frame.columnconfigure(4, weight=1) |
||
| 277 | frame.columnconfigure(5, weight=1) |
||
| 278 | |||
| 279 | @_log |
||
| 280 | def listbox_outline_listboxselect(event): |
||
| 281 | """Callback for selecting an item.""" |
||
| 282 | if self.ignore: |
||
| 283 | return |
||
| 284 | widget = event.widget |
||
| 285 | curselection = widget.curselection() |
||
| 286 | if curselection: |
||
| 287 | index = int(curselection[0]) |
||
| 288 | value = widget.get(index) |
||
| 289 | self.stringvar_item.set(value) |
||
| 290 | |||
| 291 | # Place widgets |
||
| 292 | ttk.Label(frame, text="Outline:").grid(row=0, column=0, columnspan=4, sticky=tk.W, **kw_gp) |
||
| 293 | ttk.Label(frame, text="Items:").grid(row=0, column=4, columnspan=2, sticky=tk.W, **kw_gp) |
||
| 294 | self.listbox_outline = Listbox2(frame, width=width_outline, font=normal) |
||
| 295 | self.listbox_outline.bind('<<ListboxSelect>>', listbox_outline_listboxselect) |
||
| 296 | self.listbox_outline.grid(row=1, column=0, columnspan=4, **kw_gsp) |
||
| 297 | self.text_items = tk.Text(frame, width=width_text, wrap=tk.WORD, font=normal) |
||
| 298 | self.text_items.grid(row=1, column=4, columnspan=2, **kw_gsp) |
||
| 299 | ttk.Button(frame, text="<", width=0, command=self.left).grid(row=2, column=0, sticky=tk.EW, padx=(2, 0)) |
||
| 300 | ttk.Button(frame, text="v", width=0, command=self.down).grid(row=2, column=1, sticky=tk.EW) |
||
| 301 | ttk.Button(frame, text="^", width=0, command=self.up).grid(row=2, column=2, sticky=tk.EW) |
||
| 302 | ttk.Button(frame, text=">", width=0, command=self.right).grid(row=2, column=3, sticky=tk.EW, padx=(0, 2)) |
||
| 303 | ttk.Button(frame, text="Add Item", command=self.add).grid(row=2, column=4, sticky=tk.W, **kw_gp) |
||
| 304 | ttk.Button(frame, text="Remove Selected Item", command=self.remove).grid(row=2, column=5, sticky=tk.E, **kw_gp) |
||
| 305 | |||
| 306 | return frame |
||
| 307 | |||
| 308 | def frame_item(root): |
||
| 309 | """Frame for the currently selected item.""" |
||
| 310 | # Configure grid |
||
| 311 | frame = ttk.Frame(root, **kw_f) |
||
| 312 | frame.rowconfigure(0, weight=0) |
||
| 313 | frame.rowconfigure(1, weight=4) |
||
| 314 | frame.rowconfigure(2, weight=0) |
||
| 315 | frame.rowconfigure(3, weight=1) |
||
| 316 | frame.rowconfigure(4, weight=1) |
||
| 317 | frame.rowconfigure(5, weight=1) |
||
| 318 | frame.rowconfigure(6, weight=1) |
||
| 319 | frame.rowconfigure(7, weight=0) |
||
| 320 | frame.rowconfigure(8, weight=0) |
||
| 321 | frame.rowconfigure(9, weight=0) |
||
| 322 | frame.rowconfigure(10, weight=0) |
||
| 323 | frame.rowconfigure(11, weight=4) |
||
| 324 | frame.columnconfigure(0, weight=0, pad=kw_f['padding'] * 2) |
||
| 325 | frame.columnconfigure(1, weight=1) |
||
| 326 | frame.columnconfigure(2, weight=1) |
||
| 327 | |||
| 328 | @_log |
||
| 329 | def text_focusin(_): |
||
| 330 | """Callback for entering a text field.""" |
||
| 331 | self.ignore = True |
||
| 332 | |||
| 333 | @_log |
||
| 334 | def text_item_focusout(event): |
||
| 335 | """Callback for updating text.""" |
||
| 336 | self.ignore = False |
||
| 337 | widget = event.widget |
||
| 338 | value = widget.get('1.0', 'end') |
||
| 339 | self.stringvar_text.set(value) |
||
| 340 | |||
| 341 | @_log |
||
| 342 | def text_extendedvalue_focusout(event): |
||
| 343 | """Callback for updating extended attributes.""" |
||
| 344 | self.ignore = False |
||
| 345 | widget = event.widget |
||
| 346 | value = widget.get('1.0', 'end') |
||
| 347 | self.stringvar_extendedvalue.set(value) |
||
| 348 | |||
| 349 | # Place widgets |
||
| 350 | ttk.Label(frame, text="Selected Item:").grid(row=0, column=0, columnspan=3, sticky=tk.W, **kw_gp) |
||
| 351 | self.text_item = tk.Text(frame, width=width_text, height=height_text, wrap=tk.WORD, font=fixed) |
||
| 352 | self.text_item.bind('<FocusIn>', text_focusin) |
||
| 353 | self.text_item.bind('<FocusOut>', text_item_focusout) |
||
| 354 | self.text_item.grid(row=1, column=0, columnspan=3, **kw_gsp) |
||
| 355 | ttk.Label(frame, text="Properties:").grid(row=2, column=0, sticky=tk.W, **kw_gp) |
||
| 356 | ttk.Label(frame, text="Links:").grid(row=2, column=1, columnspan=2, sticky=tk.W, **kw_gp) |
||
| 357 | ttk.Checkbutton(frame, text="Active", variable=self.intvar_active).grid(row=3, column=0, sticky=tk.W, **kw_gp) |
||
| 358 | self.listbox_links = tk.Listbox(frame, width=width_uid, height=6) |
||
| 359 | self.listbox_links.grid(row=3, column=1, rowspan=4, **kw_gsp) |
||
| 360 | ttk.Entry(frame, width=width_uid, textvariable=self.stringvar_link).grid(row=3, column=2, sticky=tk.EW + tk.N, **kw_gp) |
||
| 361 | ttk.Checkbutton(frame, text="Derived", variable=self.intvar_derived).grid(row=4, column=0, sticky=tk.W, **kw_gp) |
||
| 362 | ttk.Button(frame, text="<< Link Item", command=self.link).grid(row=4, column=2, **kw_gp) |
||
| 363 | ttk.Checkbutton(frame, text="Normative", variable=self.intvar_normative).grid(row=5, column=0, sticky=tk.W, **kw_gp) |
||
| 364 | ttk.Checkbutton(frame, text="Heading", variable=self.intvar_heading).grid(row=6, column=0, sticky=tk.W, **kw_gp) |
||
| 365 | ttk.Button(frame, text=">> Unlink Item", command=self.unlink).grid(row=6, column=2, **kw_gp) |
||
| 366 | ttk.Label(frame, text="External Reference:").grid(row=7, column=0, columnspan=3, sticky=tk.W, **kw_gp) |
||
| 367 | ttk.Entry(frame, width=width_text, textvariable=self.stringvar_ref, font=fixed).grid(row=8, column=0, columnspan=3, **kw_gsp) |
||
| 368 | ttk.Label(frame, text="Extended Attributes:").grid(row=9, column=0, columnspan=3, sticky=tk.W, **kw_gp) |
||
| 369 | self.combobox_extended = ttk.Combobox(frame, textvariable=self.stringvar_extendedkey, font=fixed) |
||
| 370 | self.combobox_extended.grid(row=10, column=0, columnspan=3, **kw_gsp) |
||
| 371 | self.text_extendedvalue = tk.Text(frame, width=width_text, height=height_ext, wrap=tk.WORD, font=fixed) |
||
| 372 | self.text_extendedvalue.bind('<FocusIn>', text_focusin) |
||
| 373 | self.text_extendedvalue.bind('<FocusOut>', text_extendedvalue_focusout) |
||
| 374 | self.text_extendedvalue.grid(row=11, column=0, columnspan=3, **kw_gsp) |
||
| 375 | |||
| 376 | return frame |
||
| 377 | |||
| 378 | def frame_family(root): |
||
| 379 | """Frame for the parent and child document items.""" |
||
| 380 | # Configure grid |
||
| 381 | frame = ttk.Frame(root, **kw_f) |
||
| 382 | frame.rowconfigure(0, weight=0) |
||
| 383 | frame.rowconfigure(1, weight=1) |
||
| 384 | frame.rowconfigure(2, weight=0) |
||
| 385 | frame.rowconfigure(3, weight=1) |
||
| 386 | frame.columnconfigure(0, weight=1) |
||
| 387 | |||
| 388 | # Place widgets |
||
| 389 | ttk.Label(frame, text="Linked To:").grid(row=0, column=0, sticky=tk.W, **kw_gp) |
||
| 390 | self.text_parents = tk.Text(frame, width=width_text, wrap=tk.WORD, font=normal) |
||
| 391 | self.text_parents.grid(row=1, column=0, **kw_gsp) |
||
| 392 | ttk.Label(frame, text="Linked From:").grid(row=2, column=0, sticky=tk.W, **kw_gp) |
||
| 393 | self.text_children = tk.Text(frame, width=width_text, wrap=tk.WORD, font=normal) |
||
| 394 | self.text_children.grid(row=3, column=0, **kw_gsp) |
||
| 395 | |||
| 396 | return frame |
||
| 397 | |||
| 398 | # Place widgets |
||
| 399 | frame_project(frame).grid(row=0, column=0, columnspan=2, **kw_gs) |
||
| 400 | frame_tree(frame).grid(row=0, column=2, columnspan=2, **kw_gs) |
||
| 401 | frame_document(frame).grid(row=1, column=0, **kw_gs) |
||
| 402 | frame_item(frame).grid(row=1, column=1, columnspan=2, **kw_gs) |
||
| 403 | frame_family(frame).grid(row=1, column=3, **kw_gs) |
||
| 404 | |||
| 405 | return frame |
||
| 406 | |||
| 660 |