our
$VERSION
=
"0.47"
;
sub
new {
my
(
$class
,
%args
) =
@_
;
foreach
(
qw/iterator show noheaders output/
){
die
"no value for $_!"
unless
defined
$args
{
$_
};
}
return
bless
(\
%args
,
$class
);
}
sub
_get_next_object_from_iterator {
my
$self
=
shift
;
my
$obj
;
for
(1) {
$obj
=
eval
{
$self
->{
'iterator'
}->
next
};
if
($@) {
UR::Object::Command::List->warning_message($@);
redo
;
}
}
return
$obj
;
}
sub
_object_properties_to_string {
my
(
$self
,
$o
,
$char
) =
@_
;
my
@v
;
return
join
(
$char
,
map
{
defined
$_
?
$_
:
'<NULL>'
}
map
{
$self
->_object_property_to_string(
$o
,
$_
)
} @{
$self
->{show}}
);
}
sub
_object_property_to_string {
my
(
$self
,
$o
,
$property
) =
@_
;
my
@v
;
if
(
substr
(
$property
,0,1) eq
'('
) {
@v
=
eval
$property
;
if
($@) {
@v
= (
'<ERROR>'
);
}
}
else
{
@v
= ();
foreach
my
$i
(
$o
->__get_attr__(
$property
)) {
if
(!
defined
$i
) {
push
@v
,
"<NULL>"
;
}
elsif
(Scalar::Util::blessed(
$i
) and
$i
->isa(
'UR::Value'
) and
$i
->can(
'create_view'
)) {
my
$v
=
$i
->create_view(
perspective
=>
'default'
,
toolkit
=>
'text'
);
push
@v
,
$v
->content();
}
elsif
(Scalar::Util::blessed(
$i
) and
$i
->can(
'__display_name__'
)) {
push
@v
,
$i
->__display_name__;
}
else
{
push
@v
,
$i
;
}
}
}
if
(
@v
> 1) {
no
warnings;
return
join
(
' '
,
@v
);
}
else
{
return
$v
[0];
}
}
sub
format_and_print{
my
$self
=
shift
;
unless
(
$self
->{noheaders} ) {
$self
->{output}->
print
(
$self
->_get_header_string.
"\n"
);
}
my
$count
= 0;
while
(
my
$object
=
$self
->_get_next_object_from_iterator()) {
$self
->{output}->
print
(
$self
->_get_object_string(
$object
),
"\n"
);
$count
++;
}
}
sub
_get_header_string{
my
$self
=
shift
;
return
"<tr><th>"
.
join
(
"</th><th>"
,
map
{
uc
} @{
$self
->{show}}) .
"</th></tr>"
;
}
sub
_get_object_string{
my
(
$self
,
$object
) =
@_
;
my
$out
=
"<tr>"
;
for
my
$property
( @{
$self
->{show}} ){
$out
.=
"<td>"
.
$object
->
$property
.
"</td>"
;
}
return
$out
.
"</tr>"
;
}
sub
format_and_print{
my
$self
=
shift
;
$self
->{output}->
print
(
"<table>"
);
unless
(
$self
->{noheaders} ) {
$self
->{output}->
print
(
$self
->_get_header_string);
}
my
$count
= 0;
while
(
my
$object
=
$self
->_get_next_object_from_iterator()) {
$self
->{output}->
print
(
$self
->_get_object_string(
$object
));
$count
++;
}
$self
->{output}->
print
(
"</table>"
);
}
sub
_get_header_string{
my
$self
=
shift
;
my
$delimiter
=
$self
->{
'csv_delimiter'
};
return
join
(
$delimiter
,
map
{
lc
} @{
$self
->{show}});
}
sub
_get_object_string {
my
(
$self
,
$object
) =
@_
;
return
$self
->_object_properties_to_string(
$object
,
$self
->{
'csv_delimiter'
});
}
sub
_get_header_string{
my
$self
=
shift
;
my
$delimiter
=
"\t"
;
return
join
(
$delimiter
,
map
{
lc
} @{
$self
->{show}});
}
sub
_get_object_string {
my
(
$self
,
$object
) =
@_
;
return
$self
->_object_properties_to_string(
$object
,
"\t"
);
}
sub
_get_header_string{
return
''
;
}
sub
_get_object_string{
my
(
$self
,
$object
) =
@_
;
my
$out
;
for
my
$property
( @{
$self
->{show}} )
{
my
$value
=
join
(
', '
,
$self
->_object_property_to_string(
$object
,
$property
));
$out
.=
sprintf
(
"%s: %s\n"
,
Term::ANSIColor::colored(
$property
,
'red'
),
Term::ANSIColor::colored(
$value
,
'cyan'
),
);
}
return
$out
;
}
sub
format_and_print{
my
$self
=
shift
;
my
$out
;
eval
"use XML::LibXML"
;
if
($@) {
die
"Please install XML::LibXML (run sudo cpanm XML::LibXML) to use this tool!"
;
}
my
$doc
= XML::LibXML->createDocument();
my
$results_node
=
$doc
->createElement(
"results"
);
$results_node
->addChild(
$doc
->createAttribute(
"generated-at"
,
$UR::Context::current
->now()) );
$doc
->setDocumentElement(
$results_node
);
my
$count
= 0;
while
(
my
$object
=
$self
->_get_next_object_from_iterator()) {
my
$object_node
=
$results_node
->addChild(
$doc
->createElement(
"object"
) );
my
$object_reftype
=
ref
$object
;
$object_node
->addChild(
$doc
->createAttribute(
"type"
,
$object_reftype
) );
$object_node
->addChild(
$doc
->createAttribute(
"id"
,
$object
->id) );
for
my
$property
( @{
$self
->{show}} ) {
my
$property_node
=
$object_node
->addChild (
$doc
->createElement(
$property
));
my
@items
=
$self
->_object_property_to_string(
$object
,
$property
);
my
$reftype
=
ref
$items
[0];
if
(
$reftype
&&
$reftype
ne
'ARRAY'
&&
$reftype
ne
'HASH'
) {
foreach
(
@items
) {
my
$subobject_node
=
$property_node
->addChild(
$doc
->createElement(
"object"
) );
$subobject_node
->addChild(
$doc
->createAttribute(
"type"
,
$reftype
) );
$subobject_node
->addChild(
$doc
->createAttribute(
"id"
,
$_
->id) );
}
}
else
{
foreach
(
@items
) {
$property_node
->addChild(
$doc
->createTextNode(
$_
) );
}
}
}
$count
++;
}
$self
->{output}->
print
(
$doc
->toString(1));
}
sub
_get_header_string{
my
$self
=
shift
;
return
join
(
"\n"
,
join
(
"\t"
,
map
{
uc
} @{
$self
->{show}}),
join
(
"\t"
,
map
{
'-'
x
length
} @{
$self
->{show}}),
);
}
sub
_get_object_string{
my
(
$self
,
$object
) =
@_
;
$self
->_object_properties_to_string(
$object
,
"\t"
);
}
sub
format_and_print{
my
$self
=
shift
;
my
$tab_delimited
;
unless
(
$self
->{noheaders}){
$tab_delimited
.=
$self
->_get_header_string.
"\n"
;
}
my
$count
= 0;
while
(
my
$object
=
$self
->_get_next_object_from_iterator()) {
$tab_delimited
.=
$self
->_get_object_string(
$object
).
"\n"
;
$count
++;
}
$self
->{output}->
print
(
$self
->tab2col(
$tab_delimited
));
}
sub
tab2col{
my
(
$self
,
$data
) =
@_
;
my
@rows
=
split
(
"\n"
,
$data
);
@rows
=
map
{ [
split
(
"\t"
,
$_
)] }
@rows
;
my
$output
;
my
@width
;
foreach
my
$row_ref
(
@rows
) {
my
@cols
=
@$row_ref
;
my
$index
=
$#cols
;
for
(
my
$i
= 0;
$i
<=
$index
;
$i
++) {
my
$l
= (
length
$cols
[
$i
]) + 3;
$width
[
$i
] =
$l
if
!
defined
$width
[
$i
] or
$l
>
$width
[
$i
];
}
}
my
@column_template
=
map
{
' '
x
$_
}
@width
;
foreach
my
$row_ref
(
@rows
) {
my
@cols
=
@$row_ref
;
my
$index
=
$#cols
;
for
(
my
$i
= 0;
$i
<
$index
;
$i
++) {
my
$entry
=
$cols
[
$i
];
my
$template
=
$column_template
[
$i
];
substr
(
$template
, 0,
length
$entry
,
$entry
);
$output
.=
$template
;
}
$output
.=
$cols
[
$index
].
"\n"
;
}
return
$output
;
}
sub
format_and_print{
my
$self
=
shift
;
my
$tab_delimited
;
unless
(
$self
->{noheaders}){
$tab_delimited
.=
$self
->_get_header_string.
"\n"
;
}
my
$view
= UR::Object::View->create(
subject_class_name
=>
'UR::Object'
,
perspective
=>
'lister'
,
toolkit
=>
'text'
,
aspects
=> [ @{
$self
->{
'show'
}} ],
);
my
$count
= 0;
while
(
my
$object
=
$self
->_get_next_object_from_iterator()) {
$view
->subject(
$object
);
$tab_delimited
.=
$view
->content() .
"\n"
;
$count
++;
}
$self
->{output}->
print
(
$self
->tab2col(
$tab_delimited
));
}
Hide Show 75 lines of Pod
1;