lotrointerface.com
Search Downloads

LoTROInterface SVN BusyBee1.3.9

[/] [trunk/] [BoskoPlugins/] [BusyBee/] [BzbTaskList/] [bzbCsv2Lua.pl] - Rev 10

Compare with Previous | Blame | View Log

#!/usr/bin/perl
# A perl script to convert the BusyBee tasks CSV file to a LUA file

use strict;
use warnings;
use Carp;
use Getopt::Long;
use Pod::Usage;
# if running on windows, then use Win32 ANSI colors
use if $^O eq 'MSWin32', "Win32::Console::ANSI";
use Term::ANSIColor;
#use Text::CSV; -- not available on some perl distros, so we'll just do it on our own
use Data::Dumper;
use File::Basename;
use FindBin;

my $SCRIPTNAME = basename($0);
my $SCRIPTDIR = $FindBin::Bin;

my %opt;        # Command line options

# The columns expected in the CSV
# and the fields (in order) that will be written to the LUA file
my @FIELDS = ('id','name','qty','lvl','faction','zone','place');

################################################################
sub readCSV($)
{
        # get input arguments
        my $inFile = shift
                or confess colored('Missing argument: inFile','bold red') . "\n";
        
        # Read input file
        print "Reading $inFile ...\n";
        open(FH,'<',$inFile)
                        or die colored('Failed to open for reading: $inFile','bold red') . "\n";
        # read column headers
        my $line = <FH>;
        chomp $line; # Remove any trailing newline
        my @hdr = split(',',$line);
        if( $opt{verbose} )
        {
                print("hdr: $_\n") foreach(@hdr);
        }
        # Read data lines
        my @tasks = (); # The array of hashes of BusyBee tasks
        my $rowNbr = 1;
        while( my $line = <FH> )
        {
                chomp $line; # Remove any trailing newline
                next if($line =~ /^$/); # Skip blank lines
                next if($line =~ /^\#/); # Skip comments
                print "$line\n"
                        if( $opt{verbose} );
                my @fields = split(',',$line);
                my %task = ( id => $rowNbr++ );         # An empty hash for this row
                for(my $idx=0; $idx<$#hdr; $idx++)
                {
                        $task{$hdr[$idx]} = $fields[$idx]; # Assign properties to hash
                }
                push @tasks,\%task; # Append task to end of tasks array
        }
        close FH;
        
        return \@tasks;
}

################################################################
sub writeLUA($$)
{       
        # get input arguments
        my $outFile = shift
                or confess colored('Missing argument: outFile','bold red') . "\n";
        my $data = shift
                or confess colored('Missing argument: data','bold red') . "\n";
        
        # Prompt user if overwrite is ok
        print "Writing $outFile ...\n";
        if( -f $opt{out} )
        {
                print colored('Output file already exists - ok to overwrite? [y/N]','bold yellow');
                my $ans = getc();
                $ans = "" unless(defined($ans)); # default in case of CTRL-C
                unless( $ans eq 'y')
                {
                        print "Aborting...\n";
                        exit(-1);
                }
        }
        
        # Write output file
        open(FH,'>',$outFile)
                or die colored('Failed to open for writing: $outFile','bold red') . "\n";
        printf FH ("--Generated by $SCRIPTNAME on %s\n", scalar(localtime));
        print FH "$opt{name} = {\n";
        my $rowNbr = 0;
        foreach my $task (@$data)
        {
                $rowNbr++;
                my $line = "  {";
                foreach my $field (@FIELDS)
                {
                        my $value = $task->{$field};
                        die "row $rowNbr value for field \"$field\" is undefined\n" . 
                                Data::Dumper->Dump([$task],[qw(task)])
                                unless(defined($value));
                        $value = "\"$value\""   # Add quotes to value if non-numeric
                                if($value =~ /\D/);
                        $line .= "$field = $value, ";
                }
                $line .= "},";
                printf FH "$line\n";
        }
        printf FH "};\n";
        close FH;
}

################################################################
main:
{
        # Set default arguments
        $opt{in} = "$SCRIPTDIR/Tasklist_english.csv";
        $opt{out} = "$SCRIPTDIR/../BusyBeeTasks.lua";
        $opt{name} = "BusyBeeTasks";
        
        # Process command line arguments
        GetOptions(
                "help|?" => \$opt{help},
                "in=s" => \$opt{in},
                "out=s" => \$opt{out},
                "name=s" => \$opt{name},
                "verbose" => \$opt{verbose},
        ) or pod2usage(-verbose => 2);
        pod2usage(-verbose => 2) if ($opt{help});
        die colored('Unsupported arguments: ' . join(' ',@ARGV),'bold red') . "\n"
                if($#ARGV >= 0);
        # Verify required arguments
        die colored('Missing required argument: -in','bold red') . "\n"
                unless( defined($opt{in}) );
        die colored('Missing required argument: -out','bold red') . "\n"
                unless( defined($opt{out}) );
        
        # Read CSV file into a array of hashes
        my $tasks = readCSV($opt{in});
        print Data::Dumper->Dump([$tasks],[qw(tasks)])  # Dump for debug if requested
                if($opt{verbose});
        
        # Write the data to the specified LUA file
        writeLUA($opt{out},$tasks);
        
        print colored('Success!','bold green') . "\n"
}

__END__

=head1 NAME

bzbCsv2Lua.pl - convert BusyBee CSV to LUA for use with BusyBee plugin

=head1 SYNOPSIS

bzbCsv2Lua <infile> <outfile>

        Options:
        -help           Dispay Brief Help Documentation
        -in=file        Specify CSV file to read
        -out=file       Specify LUA file to write (will clobber contents)
        -name=name  Name of array to write to
        -verbose        Enable verbose output

=head1 OPTIONS

=over 4

=item B<-help>

Print brief help documentation

=item B<-in=file.csv> [default = Tasklist_english.csv]

Specifies the CSV file of BusyBee tasks to read from

=item B<-out=file.lua> [default = BusyBeeTasks.lua]

Specifies the LUA file of BusyBee tasks to write to

**CAUTION: This will overwrite the file!**

=item B<-name=name> [default = BusyBeeTasks]

Specifies the name of the array to populate

=item B<-verbose>

Enables verbose output

=back

=head1 DESCRIPTION

bzbCsv2Lua reads the specified CSV file of BusyBee tasks, and converts the 
data to a LUA inclue file that is directly imported into the LOTRO BusyBee plugin.

=cut

Compare with Previous | Blame


All times are GMT -5. The time now is 08:52 PM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui