Cookbook/Recipe04/Main.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Cookbook/Recipe04/FBP/MainFB.pm ↑ Summary Cookbook/Recipe01/Main.pm »
1 package Padre::Plugin::Cookbook::Recipe04::Main;
2  
3 2 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶no if $] > 5.017010, warnings => 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # Avoids regex performance penalty
8 2 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use Padre::Logger;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Padre::Wx ();
12 use Padre::Wx::Role::Main ();
13  
14 our $VERSION = '0.24';
15  
16 use Moose;
17 use namespace::autoclean;
18  
19 # use MooseX::NonMoose;
20 extends qw( Padre::Plugin::Cookbook::Recipe04::FBP::MainFB );
21  
22 use Data::Printer { caller_info => 1, colored => 1, };
23 use Carp;
24  
25 sub BUILD {
26 my $class = shift;
27 my $main = shift;
28  
29 # Create the dialog
30 my $self = $class->SUPER::new();
31  
32 return $self;
33 }
34  
35 my $item = Wx::ListItem->new;
36 my @tuples;
37 my $card_limit = 127;
38  
39 has [qw/ relation_name config_db sql_select /] => (
40 isa => 'Str',
41 is => 'rw',
42 default => undef,
43 lazy => 1,
44 );
45  
46 has [qw/ cardinality degree previous_column /] => (
47 isa => 'Int',
48 is => 'rw',
49 default => 0,
50 lazy => 1,
51 );
52  
53 has [qw/ attributes /] => (
54 isa => 'ArrayRef',
55 is => 'rw',
56 );
57  
58 has [qw/ dialog_width /] => (
59 isa => 'Bool',
60 is => 'rw',
61 default => '0',
62 );
63  
64 #######
65 # Method set_up
66 #######
67 sub set_up {
68 my $self = shift;
69  
70 # add package name to main dialog #fails as min size naff
71 my @pkg_name = split /::/smx, __PACKAGE__,;
72 $self->package_name->SetLabel( $pkg_name[3] );
73 $self->relations->SetStringSelection('Plugin');
74 help_menu_clicked($self);
75 update_clicked($self);
76  
77 return;
78 }
79  
80 #######
81 # Event Handler Button Update Clicked
82 #######
83 sub update_clicked {
84 my $self = shift;
85 my $main = $self->main;
86  
87 $self->clean->Disable;
88 $self->show->Enable;
89 $self->width_adjust->Enable;
90 $main->info(' ');
91  
92 # get your selected relation
93 $self->relation_name( $self->relations->GetStringSelection() );
94  
95 # set padre db relation
96 $self->config_db( 'Padre::DB::' . $self->relation_name );
97  
98 # get cardinality
99 _get_cardinality($self);
100  
101 # get degree
102 eval { $self->config_db->table_info; };
103 if ($EVAL_ERROR) {
104 TRACE("Oops failed to get table info for $self->config_db ") if DEBUG;
105 carp($EVAL_ERROR);
106 } else {
107 $self->attributes( $self->config_db->table_info );
108 $self->degree( scalar( @{ $self->attributes } ) );
109 }
110  
111 # update dialog title
112 $self->relation_title->SetLabel( $self->relation_name );
113 $self->previous_column(0);
114 $self->sql_select("ORDER BY ${@{ $self->attributes}[0]}{name} ASC LIMIT $card_limit");
115  
116 _display_relation($self);
117  
118 return;
119 }
120  
121 #######
122 # Event Handler Button Show Clicked
123 #######
124 sub show_clicked {
125 my $self = shift;
126  
127 _show_relation_data($self);
128  
129 return;
130 }
131  
132 #######
133 # Event Handler Button Warning Clicked
134 #######
135 sub clean_clicked {
136 my $self = shift;
137  
138 given ( $self->relation_name ) {
139  
140 when ('DebugBreakpoints') {
141 clean_debug_breakpoints_files($self);
142 }
143 when ('History') {
144 clean_history($self);
145 }
146 when ('LastPositionInFile') {
147 clean_lastpositioninfile($self);
148 }
149 when ('Session') {
150 clean_session($self);
151 }
152 when ('SessionFile') {
153 clean_session_files($self);
154 }
155 default {
156 return;
157 }
158 }
159  
160 return;
161 }
162  
163 #######
164 # Event Handler Button Output Clicked
165 #######
166 sub width_adjust_clicked {
167 my $self = shift;
168  
169 if ( !$self->dialog_width ) {
170 $self->SetSize( 1008, -1 );
171 $self->dialog_width('1');
172 } else {
173 $self->SetSize( 560, 390 );
174 $self->dialog_width('0');
175 }
176  
177 # $self->list_ctrl->Refresh();
178 $self->list_ctrl->Update();
179  
180 return;
181 }
182  
183 #######
184 # Event Handler _on_list_item_activated (Session only)
185 #######
186 sub _on_list_item_activated {
187 my ( $self, $list_element ) = @ARG;
188  
189 if ( $self->relation_name ne 'Session' ) {
190 say 'out of scope action requested :(';
191 return;
192 }
193  
194 my $session_id = $tuples[ $list_element->GetIndex ]['0'];
195 say $tuples[ $list_element->GetIndex ]['1'];
196  
197 # redefine tuples
198 my @tuples = Padre::DB::SessionFile->select("WHERE session = $session_id");
199  
200 for ( 0 .. $#tuples ) {
201 say $tuples[$_][1];
202 }
203  
204 return;
205 }
206  
207 #######
208 # Event Handler _on_list_col_clicked
209 #######
210 sub _on_list_col_clicked {
211 my ( $self, $list_event ) = @ARG;
212  
213 my $main = $self->main;
214 my $sql_order;
215 my $col_num;
216  
217 eval { $list_event->GetColumn };
218 if ($EVAL_ERROR) {
219 say 'column info';
220 carp($EVAL_ERROR);
221 } else {
222 $col_num = $list_event->GetColumn();
223 if ( $col_num eq '0' ) {
224 say "I don't work on index";
225 return;
226 }
227 }
228  
229 if ( $col_num ne $self->previous_column ) {
230 $sql_order = 'ASC';
231 $self->previous_column($col_num);
232 } else {
233 $sql_order = 'DESC';
234  
235 # RESET previous_column
236 $self->previous_column(0);
237 }
238  
239 eval { $main->info( 'sort on: ' . ${ @{ $self->attributes }[ $col_num - 1 ] }{name} . ' ' . $sql_order ); };
240  
241 $self->sql_select("ORDER BY ${ @{ $self->attributes }[ $col_num - 1 ] }{name} $sql_order LIMIT $card_limit");
242  
243 _display_relation($self);
244  
245 return;
246 }
247 #######
248 # Composed Method,
249 # clean debug_breakpoints_files
250 #######
251 sub clean_debug_breakpoints_files {
252 my $self = shift;
253 my $main = $self->main;
254  
255 $main->info( Wx::gettext('Cleaning DebugBreakpoints Filename relation') );
256 my @debug_breakpoints_files = $self->config_db->select( $self->sql_select );
257 my @files;
258  
259 for ( 0 .. $#debug_breakpoints_files ) {
260 push @files, $debug_breakpoints_files[$_][1];
261 }
262 foreach (@files) {
263 unless ( -e $_ ) {
264 TRACE( 'Deleating missing filename from DebugBreakpoints: ' . $_ ) if DEBUG;
265 eval { $self->config_db->delete_where( "filename = ?", $_ ); };
266 if ($EVAL_ERROR) {
267 say "Oops $self->config_db is damaged";
268 carp($EVAL_ERROR);
269 }
270  
271 # get cardinality
272 _get_cardinality($self);
273 }
274 }
275  
276 $main->info( Wx::gettext('Finished Cleaning DebugBreakpoints') );
277 _display_relation($self);
278 return;
279 }
280 #######
281 # Composed Method,
282 # clean history
283 #######
284 sub clean_history {
285 my $self = shift;
286 my $main = $self->main;
287  
288 my @events = $self->config_db->select('ORDER BY name ASC');
289  
290 $main->info( Wx::gettext('Cleaning History relation') );
291 my $progressbar = _setup_progressbar($self);
292  
293 # say $self->cardinality;
294 my $count = 0;
295 for ( 0 .. ( @events - 2 ) ) {
296 if ( $events[$_][1] . $events[$_][2] eq $events[ $_ + 1 ][1] . $events[ $_ + 1 ][2] ) {
297 TRACE( $events[$_][1] . ':' . $events[$_][2] ) if DEBUG;
298 TRACE( $events[ $_ + 1 ][1] . ':' . $events[ $_ + 1 ][2] ) if DEBUG;
299 TRACE("$count: $_: found duplicate id: $events[$_][0]") if DEBUG;
300 eval { $self->config_db->delete_where( "id = ?", $events[$_][0] ); };
301 if ($EVAL_ERROR) {
302 say "Oops $self->config_db tuple $events[$_][0] is missing";
303 carp($EVAL_ERROR);
304 }
305 $count++;
306 }
307  
308 # added check for missing history files
309 if ( $events[$_][1] eq 'files' ) {
310 unless ( -e $events[$_][2] ) {
311 TRACE( "found missing file in history " . $events[$_][2] ) if DEBUG;
312 eval { $self->config_db->delete_where( "id = ?", $events[$_][0] ); };
313 if ($EVAL_ERROR) {
314 say "Oops $self->config_db tuple $events[$_][0] is missing";
315 carp($EVAL_ERROR);
316 }
317 }
318 }
319  
320 $progressbar->update( $_, "Cleaning $self->relation_name" );
321  
322 # get cardinality
323 _get_cardinality($self);
324 }
325  
326 $main->info( Wx::gettext('Finished Cleaning History') );
327 _display_relation($self);
328  
329 return;
330 }
331  
332 #######
333 # Composed Method,
334 # clean session
335 #######
336 sub clean_session {
337 my $self = shift;
338 my $main = $self->main;
339  
340 $main->info( Wx::gettext('Cleaning Session relation') );
341 for ( 0 .. $#tuples ) {
342 my @children = Padre::DB::SessionFile->select( "WHERE session = ?", $tuples[$_][0] );
343  
344 if ( @children eq 0 ) {
345 say 'id :' . $tuples[$_][1] . ' empty, deleting';
346 eval { $self->config_db->delete_where( "id = ?", $tuples[$_][0] ); };
347 if ($EVAL_ERROR) {
348 say "Oops $self->config_db is damaged";
349 carp($EVAL_ERROR);
350 }
351  
352 # get cardinality
353 _get_cardinality($self);
354 }
355 }
356 $main->info( Wx::gettext('Finished Cleaning Session relation') );
357 _display_relation($self);
358 return;
359 }
360  
361 #######
362 # Composed Method,
363 # clean session files
364 #######
365 sub clean_session_files {
366 my $self = shift;
367 my $main = $self->main;
368  
369 $main->info( Wx::gettext('Cleaning Session_Files relation') );
370 my @session_files = $self->config_db->select( $self->sql_select );
371 my @files;
372  
373 for ( 0 .. $#session_files ) {
374 push @files, $session_files[$_][1];
375 }
376 foreach (@files) {
377 unless ( -e $_ ) {
378 TRACE( 'Deleating missing file from Session_Files: ' . $_ ) if DEBUG;
379 eval { $self->config_db->delete_where( "file = ?", $_ ); };
380 if ($EVAL_ERROR) {
381 say "Oops $self->config_db is damaged";
382 carp($EVAL_ERROR);
383 }
384  
385 # get cardinality
386 _get_cardinality($self);
387 }
388 }
389  
390 $main->info( Wx::gettext('Finished Cleaning Session_Files') );
391 _display_relation($self);
392 return;
393 }
394  
395 #######
396 # Composed Method,
397 # clean session files
398 #######
399 sub clean_lastpositioninfile {
400 my $self = shift;
401 my $main = $self->main;
402  
403 $main->info( Wx::gettext('Cleaning LastPositionInFile relation') );
404 my @lastpositioninfile_files = $self->config_db->select( $self->sql_select );
405 my @files;
406  
407 for ( 0 .. $#lastpositioninfile_files ) {
408 push @files, $lastpositioninfile_files[$_][0];
409 }
410 foreach (@files) {
411 unless ( -e $_ ) {
412 TRACE( 'Deleating missing file from LastPositionInFile: ' . $_ ) if DEBUG;
413 eval { $self->config_db->delete_where( "name = ?", $_ ); };
414 if ($EVAL_ERROR) {
415 say "Oops $self->config_db is damaged";
416 carp($EVAL_ERROR);
417 }
418  
419 # get cardinality
420 _get_cardinality($self);
421 }
422 }
423  
424 $main->info( Wx::gettext('Finished Cleaning LastPositionInFile') );
425 _display_relation($self);
426 return;
427 }
428  
429 #######
430 # Composed Method,
431 # display any relation db
432 #######
433 sub _display_any_relation {
434 my $self = shift;
435  
436 _display_attribute_names($self);
437  
438 eval { $self->config_db->select; };
439 if ($EVAL_ERROR) {
440 say "Oops $self->config_db is damaged";
441 carp($EVAL_ERROR);
442 } else {
443 @tuples = $self->config_db->select( $self->sql_select );
444  
445 my $progressbar = _setup_progressbar($self);
446 my $idx = 0;
447 my $ddx = 0;
448  
449 foreach (@tuples) {
450 $item->SetId($idx);
451 if ( $idx % 2 ) {
452 $item->SetBackgroundColour( Wx::Colour->new('MEDIUM SEA GREEN') );
453 } else {
454 $item->SetBackgroundColour( Wx::Colour->new('WHITE') );
455 }
456  
457 # our display index
458 $self->list_ctrl->InsertItem($item);
459 $self->list_ctrl->SetItem( $idx, 0, $idx );
460  
461 for ( 0 .. ( $self->degree - 1 ) ) {
462 $ddx = ( $_ + 1 );
463  
464 # test for attributes with {null} values
465 if ( !defined( $tuples[$idx][$_] ) ) {
466 TRACE("Oops found a {null} in relation $self->{relation_name} ") if DEBUG;
467 } else {
468 $self->list_ctrl->SetItem( $idx, $ddx, ( $tuples[$idx][$_] ) );
469 }
470 $progressbar->update(
471 $idx,
472 "Loading $self->relation_name tuples"
473 );
474 }
475 $idx++;
476 _tidy_display($self);
477 }
478 }
479 return;
480 }
481  
482 #######
483 # Composed Method,
484 # display session data from db
485 #######
486 sub _display_session_db {
487 my $self = shift;
488  
489 _display_attribute_names($self);
490  
491 eval { $self->config_db->select; };
492 if ($EVAL_ERROR) {
493 say "Oops $self->config_db is damaged";
494 carp($EVAL_ERROR);
495 } else {
496 @tuples = $self->config_db->select( $self->sql_select );
497  
498 # TODO this is naff sortout
499 my $progressbar = _setup_progressbar($self);
500 my $idx = 0;
501  
502 foreach (@tuples) {
503  
504 # p @{$self->tuples};
505 $item->SetId($idx);
506  
507 if ( $idx % 2 ) {
508 $item->SetBackgroundColour( Wx::Colour->new('MEDIUM SEA GREEN') );
509 } else {
510 $item->SetBackgroundColour( Wx::Colour->new('WHITE') );
511 }
512 $self->list_ctrl->InsertItem($item);
513 $self->list_ctrl->SetItem( $idx, 0, $idx );
514 $self->list_ctrl->SetItem( $idx, 1, $tuples[$idx][0] );
515 $self->list_ctrl->SetItem( $idx, 2, $tuples[$idx][1] );
516 $self->list_ctrl->SetItem( $idx, 3, $tuples[$idx][2] );
517  
518 require POSIX;
519 my $update = POSIX::strftime(
520 '%Y-%m-%d %H:%M:%S',
521 localtime $tuples[$idx][3],
522 );
523  
524 # p $tuples[$idx];
525 # p( ${ @{ $self->attributes }[3] }{name} );
526  
527 $self->list_ctrl->SetItem( $idx, 4, $update );
528 $progressbar->update(
529 $idx,
530 "Loading $self->relation_name tuples"
531 );
532 $idx++;
533 _tidy_display($self);
534 }
535 }
536 return;
537 }
538  
539 #######
540 # Composed Method,
541 # _display_attribute_names
542 #######
543 sub _display_attribute_names {
544 my $self = shift;
545  
546 my $idx = 0;
547  
548 # clear ListCtrl
549 $self->list_ctrl->ClearAll;
550  
551 # List the columns in the underlying table
552 $self->list_ctrl->InsertColumn( $idx, Wx::gettext('index') );
553  
554 $idx++;
555  
556 foreach my $attribute ( @{ $self->attributes } ) {
557 my $column_title;
558 if ( $attribute->{pk} ) {
559 $column_title = "$attribute->{name} $attribute->{type} *";
560 } else {
561 $column_title = "$attribute->{name} $attribute->{type}";
562 }
563 $self->list_ctrl->InsertColumn( $idx, Wx::gettext($column_title) );
564 $self->list_ctrl->SetColumnWidth(
565 $idx,
566 Wx::wxLIST_AUTOSIZE_USEHEADER
567 );
568 $idx++;
569 }
570 return;
571 }
572  
573 #######
574 # Composed Method
575 # _display_relation
576 #######
577 sub _display_relation {
578 my $self = shift;
579  
580 given ( $self->relation_name ) {
581 when ('DebugBreakpoints') {
582 $self->clean->Enable;
583 _display_any_relation( $self, $_ );
584 }
585 when ('History') {
586 $self->clean->Enable;
587 _display_any_relation( $self, $_ );
588 }
589 when ('LastPositionInFile') {
590 $self->clean->Enable;
591 _display_any_relation( $self, $_ );
592 }
593 when ('Session') {
594 $self->clean->Enable;
595 _display_session_db( $self, $_ );
596 }
597 when ('SessionFile') {
598 $self->clean->Enable;
599 _display_any_relation( $self, $_ );
600 }
601 default {
602 _display_any_relation( $self, $_ );
603 }
604 }
605 return;
606 }
607  
608 #######
609 # Composed Method
610 # _get_cardinality
611 #######
612 sub _get_cardinality {
613  
614 my $self = shift;
615 eval { $self->config_db->count; };
616 if ($EVAL_ERROR) {
617 say "Oops failed to get cardinality for $self->config_db ";
618 carp($EVAL_ERROR);
619 } else {
620 $self->cardinality( $self->config_db->count );
621 $self->display_cardinality->SetLabel( "Cardinality: " . $self->cardinality );
622 }
623  
624 return;
625 }
626  
627 #######
628 # Composed Method,
629 # _show_relation_data
630 #######
631 sub _show_relation_data {
632 my $self = shift;
633  
634 my $info;
635  
636 eval { $self->config_db->table_info; };
637 if ($EVAL_ERROR) {
638 say "Oops no info for $self->config_db ";
639 carp($EVAL_ERROR);
640 } else {
641 $info = $self->config_db->table_info;
642 p( @{$info}, caller_message => 'Displaying ' . $self->relation_name . ' Structure' );
643 }
644  
645 eval { $self->config_db->select; };
646 if ($EVAL_ERROR) {
647 say "Oops $self->config_db is damaged";
648 carp($EVAL_ERROR);
649 } else {
650 $info = $self->config_db->select;
651 p( @{$info}, caller_message => 'Displaying ' . $self->relation_name . ' Contents' );
652 }
653  
654 return;
655 }
656  
657 #######
658 # Composed Method,
659 # _tidy_display
660 #######
661 sub _tidy_display {
662 my $self = shift;
663  
664 for ( 1 .. $self->degree ) {
665 $self->list_ctrl->SetColumnWidth( $_, Wx::wxLIST_AUTOSIZE_USEHEADER );
666 my $col_head_size = $self->list_ctrl->GetColumnWidth($_);
667 $self->list_ctrl->SetColumnWidth( $_, Wx::wxLIST_AUTOSIZE );
668 my $col_data_size = $self->list_ctrl->GetColumnWidth($_);
669 $self->list_ctrl->SetColumnWidth( $_, ( $col_head_size >= $col_data_size ) ? $col_head_size : $col_data_size );
670 }
671  
672 return;
673 }
674  
675 #######
676 # Composed Method,
677 # _setup_progressbar
678 #######
679 sub _setup_progressbar {
680 my $self = shift;
681  
682 # Set modal to true to lock other application windows while the progress
683 # box is displayed. Default is 0 (non-modal).
684 #
685 # Set lazy to true to show the progress dialog only if the whole process
686 # takes long enough that the progress box makes sense. Default if 1 (lazy-mode).
687 require Padre::Wx::Progress;
688 my $progress = Padre::Wx::Progress->new(
689 $self,
690 $self->relation_name,
691 $self->cardinality,
692 modal => 0,
693 lazy => 1,
694 );
695 return $progress;
696 }
697  
698 #######
699 # Event Handler Button About Clicked
700 #######
701 sub about_clicked {
702 my $self = shift;
703  
704 load_dialog_about($self);
705 return;
706 }
707  
708 #######
709 # Menu Help Clicked
710 #######
711 sub help_menu_clicked {
712 my $self = shift;
713 my $main = $self->main;
714  
715 $main->show_output(1);
716 my $output = $main->output;
717 $output->clear;
718  
719 my $licence = <<'MENU_HELP';
720 Info for Recipe-04 viewer.
721  
722 1, Pick a relation and click UPDATE to view it's contents, only the first
723 127 tuples will be displayed, then you can use some of the other features.
724 2, MEDIUM SEA GREEN for an old school look.
725 3, Use SHOW to peek inside after first Updating; tip start with Syntax
726 Highlight, all of Shows output goes to terminal, using Data::Printer tip set
727 your terminal to white on black
728 4, CLEAN works with History, Session, Session Files & LastPositionInFile
729 5, Clicking on Session tuple displays it's children in terminal.
730 6, You can sort on any attribute heading excluding index.
731 7, Adjust Width is a toggle: have fun
732  
733 ps outputs go-to terminal, Padre-Sataus_Bar and Padre-Output.
734  
735 MENU_HELP
736  
737 $output->AppendText($licence);
738  
739 return;
740 }
741  
742 #######
743 # Menu About Clicked
744 #######
745 sub about_menu_clicked {
746 my $self = shift;
747  
748 load_dialog_about($self);
749 return;
750 }
751  
752 #######
753 # Clean up our Classes, Padre::Plugin, POD out of date as of v0.84
754 #######
755 sub plugin_disable {
756 my $self = shift;
757  
758 # Close the dialog if it is hanging around
759 $self->clean_dialog;
760  
761 # Unload all our child classes
762 $self->unload(
763 qw{
764 Padre::Plugin::Cookbook::Recipe04::About
765 Padre::Plugin::Cookbook::Recipe04::FBP::AboutFB
766 }
767 );
768  
769 $self->SUPER::plugin_disable(@_);
770  
771 return 1;
772 }
773  
774 #######
775 # Composed Method,
776 # Load About Dialog, only once
777 #######
778 sub load_dialog_about {
779 my $self = shift;
780 my $main = $self->main;
781  
782 # Close the dialog if it is hanging around
783 $self->clean_dialog;
784  
785 # Create the new about
786 require Padre::Plugin::Cookbook::Recipe04::About;
787 $self->{dialog} = Padre::Plugin::Cookbook::Recipe04::About->new($main);
788 $self->{dialog}->ShowModal;
789  
790 return;
791 }
792  
793 ########
794 # Composed Method clean_dialog
795 ########
796 sub clean_dialog {
797 my $self = shift;
798  
799 # Close the main dialog if it is hanging around
800 if ( $self->{dialog} ) {
801 $self->{dialog}->Hide;
802 $self->{dialog}->Destroy;
803 delete $self->{dialog};
804 }
805  
806 return 1;
807 }
808  
809 no Moose;
810  
811 # no need to fiddle with inline_constructor here
812 # __PACKAGE__->meta->make_immutable;
813  
814 1;
815  
816 __END__
« Cookbook/Recipe04/FBP/MainFB.pm ↑ Summary Cookbook/Recipe01/Main.pm »