Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[perl] Add facility for generating QR codes
Signed-off-by: Michael Brown <mbrown@fensystems.co.uk>
- Loading branch information
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package qPXE::Error::QRCode; | ||
|
||
=head1 NAME | ||
qPXE::Error::QRCode - qPXE screenshot exceptions | ||
=head1 SYNOPSIS | ||
use qPXE::Error::QRCode; | ||
=head1 SUBCLASSES | ||
=cut | ||
|
||
use qPXE::Moose; | ||
extends "qPXE::Error"; | ||
__PACKAGE__->meta->make_immutable ( inline_constructor => 0 ); | ||
|
||
=head2 C<qPXE::Error::QRCode::QREncodeError> | ||
C<qrencode> exited with a failure, or produced output on C<stderr>. | ||
=head3 ATTRIBUTES | ||
=over | ||
=item C<status> | ||
Exit status from C<qrencode>. | ||
=item C<errmsg> | ||
Output produced by C<qrencode> on C<stderr>. | ||
=back | ||
=cut | ||
|
||
package qPXE::Error::QRCode::QREncodeError; | ||
use qPXE::Moose; | ||
extends "qPXE::Error::QRCode"; | ||
has "+message" => ( | ||
lazy => 1, | ||
builder => "_build_message" | ||
); | ||
has "status" => ( | ||
is => "ro", | ||
isa => "Int", | ||
required => 1, | ||
); | ||
has "errmsg" => ( | ||
is => "ro", | ||
isa => "Maybe[Str]", | ||
required => 1, | ||
); | ||
method _build_message () { | ||
return ( "qrencode failed (exit ".$self->status."): ". | ||
( $self->errmsg || "(no output)" ) ); | ||
} | ||
__PACKAGE__->meta->make_immutable ( inline_constructor => 0 ); | ||
|
||
1; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package qPXE::QRCode; | ||
|
||
=head1 NAME | ||
qPXE::QRCode - a QR code to be displayed on a test machine | ||
=head1 SYNOPSIS | ||
use qPXE::QRCode; | ||
my $qrcode = qPXE::QRCode->new ( "hello world" ); | ||
print "PNG generated as ".$qrcode->png->filename; | ||
=cut | ||
|
||
use qPXE::Moose; | ||
use qPXE::Error::QRCode; | ||
use File::Temp; | ||
use Fcntl qw ( :seek ); | ||
use IPC::Run3; | ||
use strict; | ||
use warnings; | ||
|
||
=head1 ATTRIBUTES | ||
=over | ||
=item C<string> | ||
The string embedded within the QR code. | ||
=cut | ||
|
||
has "string" => ( | ||
is => "ro", | ||
isa => "Str", | ||
required => 1, | ||
); | ||
|
||
=item C<png> | ||
The C<File::Temp> object containing the generated PNG image. | ||
=cut | ||
|
||
has "png" => ( | ||
is => "ro", | ||
isa => "File::Temp", | ||
lazy => 1, | ||
builder => "_build_png", | ||
init_arg => undef, | ||
); | ||
|
||
method _build_png () { | ||
return $self->_build_tempfile ( "PNG", ".png" ); | ||
} | ||
|
||
method _build_tempfile ( Str $type, Str $suffix ) { | ||
|
||
# Create temporary file | ||
my $tempfile = File::Temp->new ( SUFFIX => $suffix ); | ||
|
||
# Run qrencode to generate code. There are more native Perl ways to | ||
# do this, but none that are generally packaged as RPMs. | ||
my $errmsg; | ||
run3 ( [ "qrencode", "-o", "-", "-t", $type ], \$self->string, | ||
$tempfile, \$errmsg ); | ||
throw qPXE::Error::QRCode::QREncodeError ( status => $?, errmsg => $errmsg ) | ||
if $? || $errmsg; | ||
|
||
# Reset to start of file | ||
$tempfile->seek ( 0, SEEK_SET ); | ||
|
||
return $tempfile; | ||
} | ||
|
||
=back | ||
=cut | ||
|
||
around BUILDARGS => sub { | ||
my $orig = shift; | ||
my $class = shift; | ||
|
||
# Allow for single-argument constructor | ||
if ( ( @_ == 1 ) && ( ! ref $_[0] ) ) { | ||
return $class->$orig ( string => $_[0] ); | ||
} else { | ||
return $class->$orig ( @_ ); | ||
} | ||
}; | ||
|
||
__PACKAGE__->meta->make_immutable(); | ||
|
||
1; |