From db72db7fd0c10131dfc3c4a49e3e2c77aae96b25 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 9 Nov 2018 14:41:29 +0000 Subject: move stuff into modules --- bayes | 65 ++++++++++---------------------------- lib/MaildirIndexer/Parser.pm6 | 71 ++++++++++++++++++++++++++++++++++++++++++ lib/MaildirIndexer/ScanDir.pm6 | 34 ++++++++++++++++++++ 3 files changed, 121 insertions(+), 49 deletions(-) create mode 100644 lib/MaildirIndexer/Parser.pm6 create mode 100644 lib/MaildirIndexer/ScanDir.pm6 diff --git a/bayes b/bayes index 37352ce..c7c81bf 100644 --- a/bayes +++ b/bayes @@ -1,55 +1,22 @@ #!/usr/bin/env perl6 use v6.d.PREVIEW; +use lib 'lib'; +use MaildirIndexer::ScanDir; +use MaildirIndexer::Parser; -grammar Message { - regex TOP { - - - +sub MAIN($maildir) { + my $file-supply = scan-dir($maildir); + my $file-channel = $file-supply.Channel; + for ^10 { + start react { + whenever $file-channel -> $file { + if $file.e && $file.f { + my $email = parse-email($file,:headers-only); + say "{$file} - {$email}"; + } + } + } } - token newline { [\x0d\x0a] | [\x0a\x0d] | \x0a | \x0d } - token separator { - [\x0a\x0d\x0a\x0d] | [\x0d\x0a\x0d\x0a] | \x0a ** 2 | \x0d ** 2 - } - token body { .* } - regex headers { -
+ % - } - regex header { - \: \h* - || - } - token name { - <-[:\s]>+ - } - regex value { - + % [ \h+] - } - token line { \N* } - token junk { \N+ } -} - -class Message-actions { - method TOP($/) { - make %( headers => $/.made, body => $/.Str ); - } - method headers($/) { - make %( |$/
».made ); - } - method header($/) { - make $/ ?? () !! ( $/.Str => $/.made ); - } - method value($/) { - make $/.join(' ') - } -} - -sub MAIN(*@files) { - say "Starting";my $start = now; - my @messages = @files.race(:degree(10) :batch(100)).map({ - my $email = $_.IO.slurp(:enc); - my $match = Message.parse($email,:actions(Message-actions.new)); - }); - say "Took { now - $start } for { +@messages } messages"; + react whenever signal(SIGINT) { exit } } diff --git a/lib/MaildirIndexer/Parser.pm6 b/lib/MaildirIndexer/Parser.pm6 new file mode 100644 index 0000000..90b3678 --- /dev/null +++ b/lib/MaildirIndexer/Parser.pm6 @@ -0,0 +1,71 @@ +use v6.d.PREVIEW; +unit module MaildirIndexer::Parser; + +my @separators = ( + "\x0a\x0d\x0a\x0d", + "\x0d\x0a\x0d\x0a", + "\x0a\x0a", + "\x0d\x0d", +); + +grammar Message { + regex TOP { + + + + } + + token newline { [\x0d\x0a] | [\x0a\x0d] | \x0a | \x0d } + token separator { @separators } + + token body { .* } + regex headers { +
+ % + } + regex header { + \: \h* + || + } + token name { + <-[:\s]>+ + } + regex value { + + % [ \h+] + } + token line { \N* } + token junk { \N+ } +} + +class Message-actions { + method TOP($/) { + make %( headers => $/.made, body => $/.Str ); + } + method headers($/) { + make %( |$/
».made ); + } + method header($/) { + make $/ ?? () !! ( $/.Str => $/.made ); + } + method value($/) { + make $/.join(' ') + } +} + +multi parse-email(IO::Path $p) is export { + return parse-email($p.slurp(:enc)); +} +multi parse-email(IO::Path $p, :$headers-only!) is export { + return parse-email( + $p.lines( + :enc, + :nl-in(@separators), + :!chomp, + )[0], + ); +} +multi parse-email(Str $email-str) is export { + with Message.parse($email-str,:actions(Message-actions.new)) { + return .made; + } + return Nil; +} diff --git a/lib/MaildirIndexer/ScanDir.pm6 b/lib/MaildirIndexer/ScanDir.pm6 new file mode 100644 index 0000000..d3e5070 --- /dev/null +++ b/lib/MaildirIndexer/ScanDir.pm6 @@ -0,0 +1,34 @@ +use v6.d.PREVIEW; +unit module MaildirIndexer::ScanDir; + +sub scan-dir(IO() $path --> Supply) is export { + supply { + my %watched-dirs; + + sub add-dir(IO::Path $dir, :$initial) { + %watched-dirs{$dir} = True; + + CATCH { when X::IO::Dir { }; default { .perl.say } } + + whenever $dir.watch { + my $path-io = .path.IO; + emit $path-io; + when $path-io.e && $path-io.d { + add-dir($path-io) unless %watched-dirs{$path-io}; + } + when !$path-io.e { + %watched-dirs{$path-io}:delete + } + } + + for $dir.dir { + emit $_; + when .e && .d { + add-dir($_); + } + } + } + + add-dir($path); + } +} -- cgit v1.2.3