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