| 1 | package UR::Object::Command::List::Style; | ||
| 2 | |||
| 3 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 4 | |||
| 5 | sub new { | ||
| 6 | my ($class, %args) = @_; | ||
| 7 | foreach (qw/iterator show noheaders output/){ | ||
| 8 | die "no value for $_!" unless defined $args{$_}; | ||
| 9 | } | ||
| 10 | return bless(\%args, $class); | ||
| 11 | } | ||
| 12 | |||
| 13 | sub _get_next_object_from_iterator { | ||
| 14 | my $self = shift; | ||
| 15 | |||
| 16 | my $obj; | ||
| 17 | for (1) { | ||
| 18 | $obj = eval { $self->{'iterator'}->next }; | ||
| 19 | if ($@) { | ||
| 20 | UR::Object::Command::List->warning_message($@); | ||
| 21 | redo; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | return $obj; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _object_properties_to_string { | ||
| 28 | my ($self, $o, $char) = @_; | ||
| 29 | my @v; | ||
| 30 | return join( | ||
| 31 | $char, | ||
| 32 | map { defined $_ ? $_ : '<NULL>' } | ||
| 33 | map { | ||
| 34 | $self->_object_property_to_string($o,$_) | ||
| 35 | } @{$self->{show}} | ||
| 36 | ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _object_property_to_string { | ||
| 40 | my ($self, $o, $property) = @_; | ||
| 41 | |||
| 42 | my @v; | ||
| 43 | if (substr($property,0,1) eq '(') { | ||
| 44 | @v = eval $property; | ||
| 45 | if ($@) { | ||
| 46 | @v = ('<ERROR>'); # ($@ =~ /^(.*)$/); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | @v = (); | ||
| 51 | foreach my $i ($o->__get_attr__($property)) { | ||
| 52 | if (! defined $i) { | ||
| 53 | push @v, "<NULL>"; | ||
| 54 | } | ||
| 55 | elsif (Scalar::Util::blessed($i) and $i->isa('UR::Value') and $i->can('create_view')) { | ||
| 56 | # Here we allow any UR::Values that have their own views to present themselves. | ||
| 57 | my $v = $i->create_view( perspective => 'default', toolkit => 'text' ); | ||
| 58 | push @v, $v->content(); | ||
| 59 | } | ||
| 60 | elsif (Scalar::Util::blessed($i) and $i->can('__display_name__')) { | ||
| 61 | push @v, $i->__display_name__; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | push @v, $i; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | if (@v > 1) { | ||
| 70 | no warnings; | ||
| 71 | return join(' ',@v); | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | return $v[0]; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub format_and_print{ | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | unless ( $self->{noheaders} ) { | ||
| 82 | $self->{output}->print($self->_get_header_string. "\n"); | ||
| 83 | } | ||
| 84 | |||
| 85 | my $count = 0; | ||
| 86 | while (my $object = $self->_get_next_object_from_iterator()) { | ||
| 87 | $self->{output}->print($self->_get_object_string($object), "\n"); | ||
| 88 | $count++; | ||
| 89 | } | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | package UR::Object::Command::List::Html; | ||
| 94 | use base 'UR::Object::Command::List::Style'; | ||
| 95 | |||
| 96 | sub _get_header_string{ | ||
| 97 | my $self = shift; | ||
| 98 | return "<tr><th>". join("</th><th>", map { uc } @{$self->{show}}) ."</th></tr>"; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _get_object_string{ | ||
| 102 | my ($self, $object) = @_; | ||
| 103 | |||
| 104 | my $out = "<tr>"; | ||
| 105 | for my $property ( @{$self->{show}} ){ | ||
| 106 | $out .= "<td>" . $object->$property . "</td>"; | ||
| 107 | } | ||
| 108 | |||
| 109 | return $out . "</tr>"; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub format_and_print{ | ||
| 113 | my $self = shift; | ||
| 114 | |||
| 115 | $self->{output}->print("<table>"); | ||
| 116 | |||
| 117 | #cannot use super because \n screws up javascript | ||
| 118 | unless ( $self->{noheaders} ) { | ||
| 119 | $self->{output}->print($self->_get_header_string); | ||
| 120 | } | ||
| 121 | |||
| 122 | my $count = 0; | ||
| 123 | while (my $object = $self->_get_next_object_from_iterator()) { | ||
| 124 | $self->{output}->print($self->_get_object_string($object)); | ||
| 125 | $count++; | ||
| 126 | } | ||
| 127 | |||
| 128 | $self->{output}->print("</table>"); | ||
| 129 | } | ||
| 130 | |||
| 131 | package UR::Object::Command::List::Csv; | ||
| 132 | use base 'UR::Object::Command::List::Style'; | ||
| 133 | |||
| 134 | sub _get_header_string{ | ||
| 135 | my $self = shift; | ||
| 136 | |||
| 137 | my $delimiter = $self->{'csv_delimiter'}; | ||
| 138 | return join($delimiter, map { lc } @{$self->{show}}); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _get_object_string { | ||
| 142 | my ($self, $object) = @_; | ||
| 143 | |||
| 144 | return $self->_object_properties_to_string($object, $self->{'csv_delimiter'}); | ||
| 145 | } | ||
| 146 | |||
| 147 | package UR::Object::Command::List::Tsv; | ||
| 148 | use base 'UR::Object::Command::List::Csv'; | ||
| 149 | |||
| 150 | sub _get_header_string{ | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | my $delimiter = "\t"; | ||
| 154 | return join($delimiter, map { lc } @{$self->{show}}); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _get_object_string { | ||
| 158 | my ($self, $object) = @_; | ||
| 159 | |||
| 160 | return $self->_object_properties_to_string($object, "\t"); | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | package UR::Object::Command::List::Pretty; | ||
| 165 | use base 'UR::Object::Command::List::Style'; | ||
| 166 | |||
| 167 | sub _get_header_string{ | ||
| 168 | return ''; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _get_object_string{ | ||
| 172 | my ($self, $object) = @_; | ||
| 173 | |||
| 174 | my $out; | ||
| 175 | for my $property ( @{$self->{show}} ) | ||
| 176 | { | ||
| 177 | my $value = join(', ', $self->_object_property_to_string($object,$property)); | ||
| 178 | $out .= sprintf( | ||
| 179 | "%s: %s\n", | ||
| 180 | Term::ANSIColor::colored($property, 'red'), | ||
| 181 | Term::ANSIColor::colored($value, 'cyan'), | ||
| 182 | ); | ||
| 183 | } | ||
| 184 | |||
| 185 | return $out; | ||
| 186 | } | ||
| 187 | |||
| 188 | package UR::Object::Command::List::Xml; | ||
| 189 | use base 'UR::Object::Command::List::Style'; | ||
| 190 | |||
| 191 | sub format_and_print{ | ||
| 192 | my $self = shift; | ||
| 193 | my $out; | ||
| 194 | |||
| 195 | eval "use XML::LibXML"; | ||
| 196 | if ($@) { | ||
| 197 | die "Please install XML::LibXML (run sudo cpanm XML::LibXML) to use this tool!"; | ||
| 198 | } | ||
| 199 | |||
| 200 | my $doc = XML::LibXML->createDocument(); | ||
| 201 | my $results_node = $doc->createElement("results"); | ||
| 202 | $results_node->addChild( $doc->createAttribute("generated-at",$UR::Context::current->now()) ); | ||
| 203 | |||
| 204 | $doc->setDocumentElement($results_node); | ||
| 205 | |||
| 206 | my $count = 0; | ||
| 207 | while (my $object = $self->_get_next_object_from_iterator()) { | ||
| 208 | my $object_node = $results_node->addChild( $doc->createElement("object") ); | ||
| 209 | |||
| 210 | my $object_reftype = ref $object; | ||
| 211 | $object_node->addChild( $doc->createAttribute("type",$object_reftype) ); | ||
| 212 | $object_node->addChild( $doc->createAttribute("id",$object->id) ); | ||
| 213 | |||
| 214 | for my $property ( @{$self->{show}} ) { | ||
| 215 | |||
| 216 | my $property_node = $object_node->addChild ($doc->createElement($property)); | ||
| 217 | |||
| 218 | my @items = $self->_object_property_to_string($object, $property); | ||
| 219 | |||
| 220 | my $reftype = ref $items[0]; | ||
| 221 | |||
| 222 | if ($reftype && $reftype ne 'ARRAY' && $reftype ne 'HASH') { | ||
| 223 | foreach (@items) { | ||
| 224 | my $subobject_node = $property_node->addChild( $doc->createElement("object") ); | ||
| 225 | $subobject_node->addChild( $doc->createAttribute("type",$reftype) ); | ||
| 226 | $subobject_node->addChild( $doc->createAttribute("id",$_->id) ); | ||
| 227 | #$subobject_node->addChild( $doc->createTextNode($_->id) ); | ||
| 228 | #xIF | ||
| 229 | } | ||
| 230 | } else { | ||
| 231 | foreach (@items) { | ||
| 232 | $property_node->addChild( $doc->createTextNode($_) ); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | } | ||
| 237 | $count++; | ||
| 238 | } | ||
| 239 | $self->{output}->print($doc->toString(1)); | ||
| 240 | } | ||
| 241 | |||
| 242 | package UR::Object::Command::List::Text; | ||
| 243 | use base 'UR::Object::Command::List::Style'; | ||
| 244 | |||
| 245 | sub _get_header_string{ | ||
| 246 | my $self = shift; | ||
| 247 | return join ( | ||
| 248 | "\n", | ||
| 249 | join("\t", map { uc } @{$self->{show}}), | ||
| 250 | join("\t", map { '-' x length } @{$self->{show}}), | ||
| 251 | ); | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _get_object_string{ | ||
| 255 | my ($self, $object) = @_; | ||
| 256 | $self->_object_properties_to_string($object, "\t"); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub format_and_print{ | ||
| 260 | my $self = shift; | ||
| 261 | my $tab_delimited; | ||
| 262 | unless ($self->{noheaders}){ | ||
| 263 | $tab_delimited .= $self->_get_header_string."\n"; | ||
| 264 | } | ||
| 265 | |||
| 266 | my $count = 0; | ||
| 267 | while (my $object = $self->_get_next_object_from_iterator()) { | ||
| 268 | $tab_delimited .= $self->_get_object_string($object)."\n"; | ||
| 269 | $count++; | ||
| 270 | } | ||
| 271 | |||
| 272 | $self->{output}->print($self->tab2col($tab_delimited)); | ||
| 273 | } | ||
| 274 | |||
| 275 | sub tab2col{ | ||
| 276 | my ($self, $data) = @_; | ||
| 277 | |||
| 278 | #turn string into 2d array of arrayrefs ($array[$rownum][$colnum]) | ||
| 279 | my @rows = split("\n", $data); | ||
| 280 | @rows = map { [split("\t", $_)] } @rows; | ||
| 281 | |||
| 282 | my $output; | ||
| 283 | my @width; | ||
| 284 | |||
| 285 | #generate array of max widths per column | ||
| 286 | foreach my $row_ref (@rows) { | ||
| 287 | my @cols = @$row_ref; | ||
| 288 | my $index = $#cols; | ||
| 289 | for (my $i = 0; $i <= $index; $i++) { | ||
| 290 | my $l = (length $cols[$i]) + 3; #TODO test if we need this buffer space | ||
| 291 | $width[$i] = $l if ! defined $width[$i] or $l > $width[$i]; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | #create a array of blanks to use as a templatel | ||
| 296 | my @column_template = map { ' ' x $_ } @width; | ||
| 297 | |||
| 298 | #iterate through rows and cols, substituting in the row entry in your template | ||
| 299 | foreach my $row_ref (@rows) { | ||
| 300 | my @cols = @$row_ref; | ||
| 301 | my $index = $#cols; | ||
| 302 | #only apply template for all but the last entry in a row | ||
| 303 | for (my $i = 0; $i < $index; $i++) { | ||
| 304 | my $entry = $cols[$i]; | ||
| 305 | my $template = $column_template[$i]; | ||
| 306 | substr($template, 0, length $entry, $entry); | ||
| 307 | $output.=$template; | ||
| 308 | } | ||
| 309 | $output.=$cols[$index]."\n"; #Don't need traling spaces on the last entry | ||
| 310 | } | ||
| 311 | return $output; | ||
| 312 | } | ||
| 313 | |||
| 314 | package UR::Object::Command::List::Newtext; | ||
| 315 | use base 'UR::Object::Command::List::Text'; | ||
| 316 | |||
| 317 | sub format_and_print{ | ||
| 318 | my $self = shift; | ||
| 319 | my $tab_delimited; | ||
| 320 | |||
| 321 | unless ($self->{noheaders}){ | ||
| 322 | $tab_delimited .= $self->_get_header_string."\n"; | ||
| 323 | } | ||
| 324 | |||
| 325 | my $view = UR::Object::View->create( | ||
| 326 | subject_class_name => 'UR::Object', | ||
| 327 | perspective => 'lister', | ||
| 328 | toolkit => 'text', | ||
| 329 | aspects => [ @{$self->{'show'}} ], | ||
| 330 | ); | ||
| 331 | |||
| 332 | my $count = 0; | ||
| 333 | while (my $object = $self->_get_next_object_from_iterator()) { | ||
| 334 | $view->subject($object); | ||
| 335 | $tab_delimited .= $view->content() . "\n"; | ||
| 336 | $count++; | ||
| 337 | } | ||
| 338 | |||
| 339 | $self->{output}->print($self->tab2col($tab_delimited)); | ||
| 340 | } | ||
| 341 | |||
| 342 | 1; | ||
| 343 | =pod | ||
| 344 | |||
| 345 | =head1 NAME | ||
| 346 | |||
| 347 | UR::Object::Command::List - Fetches and lists objects in different styles. | ||
| 348 | |||
| 349 | =head1 SYNOPSIS | ||
| 350 | |||
| 351 | package MyLister; | ||
| 352 | |||
| 353 | use strict; | ||
| 354 | use warnings; | ||
| 355 | |||
| 356 | use above "UR"; | ||
| 357 | |||
| 358 | class MyLister { | ||
| 359 | is => 'UR::Object::Command::List', | ||
| 360 | has => [ | ||
| 361 | # add/modify properties | ||
| 362 | ], | ||
| 363 | }; | ||
| 364 | |||
| 365 | 1; | ||
| 366 | |||
| 367 | =head1 Provided by the Developer | ||
| 368 | |||
| 369 | =head2 subject_class_name (optional) | ||
| 370 | |||
| 371 | The subject_class_name is the class for which the objects will be fetched. It can be specified one of two main ways: | ||
| 372 | |||
| 373 | =over | ||
| 374 | |||
| 375 | =item I<by_the_end_user_on_the_command_line> | ||
| 376 | |||
| 377 | For this do nothing, the end user will have to provide it when the command is run. | ||
| 378 | |||
| 379 | =item I<by_the_developer_in the_class_declartion> | ||
| 380 | |||
| 381 | For this, in the class declaration, add a has key w/ arrayref of hashrefs. One of the hashrefs needs to be subject_class_name. Give it this declaration: | ||
| 382 | |||
| 383 | class MyFetchAndDo { | ||
| 384 | is => 'UR::Object::Command::FetchAndDo', | ||
| 385 | has => [ | ||
| 386 | subject_class_name => { | ||
| 387 | value => <CLASS NAME>, | ||
| 388 | is_constant => 1, | ||
| 389 | }, | ||
| 390 | ], | ||
| 391 | }; | ||
| 392 | |||
| 393 | =back | ||
| 394 | |||
| 395 | =head2 show (optional) | ||
| 396 | |||
| 397 | Add defaults to the show property: | ||
| 398 | |||
| 399 | class MyFetchAndDo { | ||
| 400 | is => 'UR::Object::Command::FetchAndDo', | ||
| 401 | has => [ | ||
| 402 | show => { | ||
| 403 | default_value => 'name,age', | ||
| 404 | }, | ||
| 405 | ], | ||
| 406 | }; | ||
| 407 | |||
| 408 | =head2 helps (optional) | ||
| 409 | |||
| 410 | Overwrite the help_brief, help_synopsis and help_detail methods to provide specific help. If overwiting the help_detail method, use call '_filter_doc' to get the filter documentation and usage to combine with your specific help. | ||
| 411 | |||
| 412 | =head1 List Styles | ||
| 413 | |||
| 414 | text, csv, html, xml, pretty (inprogress) | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | |||
| 419 | #$HeadURL: svn+ssh://svn/srv/svn/gscpan/perl_modules/trunk/UR/Object/Command/List.pm $ | ||
| 420 | #$Id: List.pm 50329 2009-08-25 20:10:00Z abrummet $ |