TomboyFetcher

Ein kleines Perl-Script, dem man ein Verzeichnis und ein Regex übergeben kann und dann automatisch neue Notizen im (laufenden) Tomboy anlegt. Ich lasse es zum Beispiel auf meinen Projektordner laufen und es legt mir je nach Regex Notizen über ToDo’s und FixMe’s an.

#!/usr/bin/perl -w

use warnings;
use strict;

use Net::DBus;
use File::Next;
use Getopt::Long;
use Sysadm::Install qw(:all);
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($DEBUG);

my $opts={};
my $bus=Net::DBus->session;
my $handle = $bus->get_service('org.gnome.Tomboy');
my $tomboy=$handle->get_object('/org/gnome/Tomboy/RemoteControl');

GetOptions($opts,'dir=s','regex=s','notebook=s','tag=s','help');

&usage() if $opts->{help} || scalar keys %{ $opts } == 0;
&usage() if (!$opts->{dir} || !$opts->{regex});

if(!$opts->{notebook}) {
    $opts->{notebook} = 'TomboyFetcher';
}

if(!$opts->{tag}) {
    $opts->{tag} = 'TomboyFetcher';
}

$tomboy->AddNotebook($opts->{notebook});

sub add_note {
    my $name = shift;
    my $content = shift;

    my $exist_note = $tomboy->FindNote($name);
    if($exist_note){
        $tomboy->DeleteNote($exist_note);
    }

    my $new_note = $tomboy->CreateNamedNote($name);
    $tomboy->SetNoteContents($new_note, $name."\n\n".$content);
    $tomboy->AddTagToNote($new_note, $opts->{tag});
    $tomboy->AddNoteToNotebook($new_note, $opts->{notebook});
    return;
}

my @files;
my $list = File::Next::files({}, $opts->{dir});
while (( my( $dir, $file, $full) = $list->() )) {
    push @files, $full;
    my ($out, $err, $ec) = tap({no_quotes => 1}, "grep -rnE -C 5", $opts->{regex}, $full);
    if($out) {
        add_note($full, $out);
    }
}

sub usage {
    (my $proc = $0) =~ s:./*::;
    print 'usage: $proc -dir|-regex|-notebook ' . "\n\n" .
        '   -help: you\'re just reading it ;-)' . "\n" .
        '   -dir: full path to grep in' . "\n" .
        '   -notebook: name for the tomboy notebook, default is TomboyFetcher' . "\n" .
        '   -tag: you can specify a tag for the new created notes' . "\n" .
        '   -regex: The regex to grep for' . "\n".
        "\n";
    exit 0;
}

Beispiel:
tomboyfetcher.pl -dir=/path/to/my/workspace/and/project -regex=\“FIXME\“

Kommentare erwünscht!!

Links:

Tomboy: http://projects.gnome.org/tomboy/
Sysadm::Install: http://search.cpan.org/dist/Sysadm-Install/lib/Sysadm/Install.pm

Leave a Reply