Skip to content
Snippets Groups Projects
Commit d6496d0e authored by Gerhard Gonter's avatar Gerhard Gonter :speech_balloon:
Browse files

added "tom" script as a more general tool to work with Tomboy files

parent a58367da
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/perl
package Tomboy::TOC;
use strict;
use Tomboy::Directory;
sub new
{
my $class= shift;
my $self= {};
bless $self, $class;
$self->set (@_);
$self;
}
sub set
{
my $self= shift;
my %par= @_;
foreach my $par (keys %par) { $self->{$par}= $par{$par}; }
}
sub find
{
my $self= shift;
my $pattern= shift;
print "find: pattern=[$pattern]\n";
my ($mode, $toc, $rows)= $self->load_toc();
my @res;
foreach my $row (@$rows)
{
next unless ($row->{'title'} =~ m#$pattern#i);
# print "row: ", main::Dumper ($row);
push (@res, $row);
}
@res;
}
sub scan_dir
{
my $self= shift;
print main::Dumper ($self);
my ($note_dir, $toc_file)= map { $self->{$_} } qw(note_dir toc_file);
$note_dir=~ s#/+$##;
my $tb_d= new Tomboy::Directory ();
# print "tb_d: ", Dumper ($tb_d);
print "scanning [$note_dir]\n";
my ($mode, $toc, $rows)= $self->load_toc();
my $toc_data= $tb_d->scan_dir ($note_dir, $rows, $mode);
# TODO: if verbose or so print "toc_data: ", Dumper ($toc_data);
$toc->{'data'}= $toc_data;
# TODO: optionally sort the returned values
$toc->sort ('uuid');
# print "toc: ", Dumper ($toc);
$toc->save_csv_file ('filename' => $toc_file, 'separator' => "\t", 'UTF8' => 1);
}
sub load_toc
{
my $self= shift;
my $mode= 0;
my $toc;
print "load_toc\n";
return (1, $toc, $toc->{'data'}) if (defined ($toc= $self->{'_toc'}));
my ($toc_file)= map { $self->{$_} } qw(toc_file);
print "load_toc: toc_file=[$toc_file]\n";
my $toc= new Util::Simple_CSV ('UTF8' => 1, 'no_array' => 1, 'separator' => "\t", 'UTF8' => 1);
my $rows= [];
if (-f $toc_file)
{
print "loading $toc_file\n";
$toc->load_csv_file ($toc_file);
$mode= 1;
$rows= $toc->{'data'};
print "preparing quick scan, loaded [$toc_file]\n";
# print "toc: ", Dumper ($toc);
# print "rows: ", Dumper ($rows);
# exit;
}
else
{
$toc->define_columns (Tomboy::Directory::TB_attrs());
}
$self->{'_toc'}= $toc;
($mode, $toc, $rows);
}
1;
#!/usr/bin/perl
=head1 NAME
tom
=head1 DESCRIPTION
do stuff with Tomboy files
=head1 USAGE
tom <op-code> @options @parameters
=head1 OPTIONS
...
=head1 OP-CODES
=head2 show
=head2 diff
=cut
use strict;
use Data::Dumper;
$Data::Dumper::Indent= 1;
use Util::Simple_CSV;
use Tomboy::Note::Simple;
use Tomboy::Directory;
use Tomboy::TOC;
my $toc_file;
my $note_dir= $ENV{'HOME'} . '/.local/share/tomboy';
my $start_tb= 0;
my $toc;
my $op_code;
my @PAR;
while (my $arg= shift (@ARGV))
{
if ($arg =~ /^-/)
{
if ($arg eq '-d') { $note_dir= shift (@ARGV); }
elsif ($arg eq '-t') { $toc_file= shift (@ARGV); }
elsif ($arg eq '-e') { $start_tb= 1; }
else
{
usage();
}
}
else
{
push (@PAR, $arg);
}
}
my $op_code= shift (@PAR) unless (defined ($op_code));
if ($op_code eq 'help') { usage(); }
elsif ($op_code eq 'dump') { dump_note ($_) foreach (@PAR); }
elsif ($op_code eq 'toc') { tom_toc(); }
elsif ($op_code eq 'find') { find_note(@PAR); }
elsif ($op_code eq 'diff')
{
my $f1= shift (@PAR);
my $n1= parse Tomboy::Note::Simple ($f1);
foreach my $f2 (@PAR)
{
my $n2= parse Tomboy::Note::Simple ($f2);
diff_note ($n1, $n2);
}
}
else
{
usage();
}
exit (0);
sub usage
{
system ('perldoc', $0);
exit;
}
sub setup_toc
{
$toc_file= join ('/', $note_dir, 'Tomboy-TOC.csv') unless (defined ($toc_file));
$toc= new Tomboy::TOC('note_dir' => $note_dir, 'toc_file' => $toc_file);
# print "toc: ", Dumper ($toc);
}
sub tom_toc
{
setup_toc();
$toc->scan_dir();
}
sub find_note
{
setup_toc();
my %uuid;
foreach my $pattern (@_)
{
my @res= $toc->find ($pattern);
foreach my $res (@res) { $uuid{$res->{'uuid'}}= $res; } # TODO: count hits
}
foreach my $uuid (keys %uuid)
{
if ($start_tb)
{
Tomboy::start_tb ('uuid', $uuid);
}
else
{
print Dumper ($uuid{$uuid});
}
}
}
sub dump_note
{
my $note_fnm= shift;
my $n1= parse Tomboy::Note::Simple ($note_fnm);
$n1->update();
print "n1: ", Dumper ($n1);
}
sub diff_note
{
my $n1= shift;
my $n2= shift;
print "n1: ", Dumper ($n1);
print "n2: ", Dumper ($n2);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment