From: Andrew Ruthven Date: Fri, 20 Feb 2009 22:27:44 +0000 (+1300) Subject: Convert a Planner file to CSV. X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fbbad8e9bd1af9a5e32f9b0bc4a03fec7c377ebd;p=planner-tools.git Convert a Planner file to CSV. --- fbbad8e9bd1af9a5e32f9b0bc4a03fec7c377ebd diff --git a/planner2csv.pl b/planner2csv.pl new file mode 100755 index 0000000..ed3c5c3 --- /dev/null +++ b/planner2csv.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl -w + +# Parse a .planner file from Planner and generate a CSV from it. +# For use with: http://live.gnome.org/Planner +# +# Written by: Andrew Ruthven +# Git repo: http://git.etc.gen.nz/planner-tools.git +# +# Released under the GPLv3. +# +# Usage: planner2csv.pl > out.csv + +use XML::LibXML; +use Text::CSV_XS; + +my $file = shift; +if (-! -f $file) { + die "Sorry, what file do you want to convert to a CSV?\n"; +} + +my $parser = XML::LibXML->new; +my $tree = $parser->parse_file($file); + +my $root = $tree->getDocumentElement; + +my $csv = Text::CSV_XS->new(); + +print 'id,description,allocated,start,end,"percent complete"' . "\n"; + +for my $tasks ($root->findnodes('/project/tasks')) { + find_tasks($tasks); +} + +sub find_tasks { + my $node = shift; + my $level = shift || 0; + my @id = @_; + + $id[$level] = 0; + + for my $task ($node->findnodes('task')) { + $id[$level]++; + my @fields = (); + push @fields, + join('.', @id), + $task->findvalue('@name'); + push @fields, find_allocated($task); + + push @fields, + date_convert($task->findvalue('@start')), + date_convert($task->findvalue('@end')), + $task->findvalue('@percent-complete'); + + $csv->combine (@fields); + print $csv->string() . "\n"; + + push @id, '0'; + find_tasks($task, $level + 1, @id); + pop @id; + } +} + +sub find_allocated { + my $task = shift; + + my @resources = (); + + for my $allocation ($root->findnodes('/project/allocations/allocation[@task-id=' . $task->findvalue('@id') . ']')) { + for my $resource ($root->findnodes('/project/resources/resource[@id=' . $allocation->findvalue('@resource-id') . ']')) { + push @resources, $resource->findvalue('@name'); + } + } + + return join(', ', @resources); +} + +sub date_convert { + my $date = shift; + + $date =~ s/^(\d{4})(\d\d)(\d\d).*/$1-$2-$3/; + + return $date; +}