Created
December 15, 2022 10:40
-
-
Save jesusbagpuss/553234bf241caf4d3aab78d0c5b3066c to your computer and use it in GitHub Desktop.
Debug / analysis of getting data from an EPrint DataObj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# Save this into ~/bin/local/ | |
# or alter line 7 below! ;) | |
use FindBin; | |
use lib "$FindBin::Bin/../../perl_lib"; | |
use EPrints; | |
use Data::Dumper; | |
eval "use Term::ReadKey"; | |
eval "use Compat::Term::ReadKey" if $@; | |
use strict; | |
use vars qw( %COLORS ); | |
eval "use Term::ANSIColor qw()"; | |
unless( $@ ) | |
{ | |
%COLORS = ( | |
reset => "reset", | |
prompt => "bold magenta", | |
input => "bold yellow", | |
bad_input => "bold red", | |
output => "green", | |
); | |
} | |
my $REGEXP_NUMBER = '^[0-9]+$'; | |
my $REGEXP_VARNAME = '^[a-zA-Z][_A-Za-z0-9]*$'; | |
my $repoid = get_input( $REGEXP_VARNAME, 'Archive ID' ); | |
my $noise = get_input( $REGEXP_NUMBER, 'Session noise [0 (quiet) - 3 (includes SQL debug)]' ); | |
my $session = new EPrints::Session( 1 , $repoid , $noise ); | |
if( !defined $session ) | |
{ | |
print STDERR "Failed to load repository: $repoid\n"; | |
exit 1; | |
} | |
my $eprintid = get_input( $REGEXP_NUMBER, 'EPrint ID' ); | |
print color("output"), "\nChecking for EPrint #$eprintid...\n", color("reset"), "\n\n"; | |
my $eprint = EPrints::DataObj::EPrint->new( $session, $eprintid ); | |
if( !defined $eprint ) | |
{ | |
$session->get_repository->log( "EPrint #$eprintid not found.\n" ); | |
} | |
else | |
{ | |
my $field = get_input( $REGEXP_VARNAME, 'Field to print' ); | |
print color("output"), "Info for $field:\n"; | |
print "RAW VALUE (Dumper):\n\t", color("reset"), Dumper($eprint->value( $field )); | |
print color("output"), "RENDERED AND STRINGIFIED:\n\t", color("reset"), EPrints::Utils::tree_to_utf8( $eprint->render_value( $field ) ), "\n"; | |
print color("output"), "HTML:\n\t", color("reset"), $eprint->render_value( $field )->toString(1),"\n"; | |
} | |
$session->terminate(); | |
exit; | |
# Borrowed from EPrints::Utils - with a bit of added colour | |
sub get_input | |
{ | |
my( $regexp, $prompt, $default ) = @_; | |
$prompt = "" if( !defined $prompt); | |
$prompt .= " [$default] " if( defined $default ); | |
$prompt .= "? "; | |
for(;;) | |
{ | |
print color("prompt"); | |
#print wrap_text( $prompt, 'console' ); | |
print "\n\n$prompt"; | |
print color("input"); | |
my $in = Term::ReadKey::ReadLine(0); | |
$in =~ s/\015?\012?$//s; | |
print color("reset"); | |
if( $in eq "" && defined $default ) | |
{ | |
return $default; | |
} | |
if( $in=~m/^$regexp$/ ) | |
{ | |
return $in; | |
} | |
else | |
{ | |
print color("bad_input"); | |
print "Bad Input, try again.\n"; | |
print color("input"); | |
} | |
} | |
} | |
# Borrowed from EPrints::CLIProcessor | |
sub color | |
{ | |
my( $type ) = @_; | |
return exists $COLORS{$type} ? Term::ANSIColor::color( $COLORS{$type} ) : ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment