| 1 | package Exobrain; | ||
| 2 | |||
| 3 | 10 | ▶ | use v5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 10 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 10 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 10 | ▶ | use feature (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 10 | ▶ | use autodie; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 10 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 10 | ▶ | use Method::Signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 10 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 10 | ▶ | use POSIX qw(tzset); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | # ABSTRACT: Automate your life with Exobrain | ||
| 14 | |||
| 15 | our $VERSION = '1.08'; # VERSION: Generated by DZP::OurPkg:Version | ||
| 16 | |||
| 17 | |||
| 18 | # This forms the top-level Exobrain dispatch class. It's mostly | ||
| 19 | # lazy attributes, and nice sugar for doing common operations. | ||
| 20 | |||
| 21 | 0 | ▶ | use Exobrain::Bus; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 22 | use Exobrain::Config; | ||
| 23 | use Exobrain::Message; | ||
| 24 | use Exobrain::Message::Raw; | ||
| 25 | |||
| 26 | has 'config' => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => 'Exobrain::Config', | ||
| 29 | builder => '_build_config', | ||
| 30 | ); | ||
| 31 | |||
| 32 | # Pub/Sub interfaces to our bus. These don't get generated unless | ||
| 33 | # our end code actually asks for them. Many things will only require | ||
| 34 | # one, or will use higher-level functions to do their work. | ||
| 35 | |||
| 36 | has 'pub' => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Exobrain::Bus', | ||
| 39 | builder => '_build_pub', | ||
| 40 | lazy => 1, | ||
| 41 | ); | ||
| 42 | |||
| 43 | has 'sub' => ( | ||
| 44 | is => 'ro', | ||
| 45 | isa => 'Exobrain::Bus', | ||
| 46 | builder => '_build_sub', | ||
| 47 | lazy => 1, | ||
| 48 | ); | ||
| 49 | |||
| 50 | # import is called when we load the Exobrain class, and lets us | ||
| 51 | # automatically set strict, warnings, 5.10 features and Method::Signatures | ||
| 52 | # by default. | ||
| 53 | |||
| 54 | sub import { | ||
| 55 | |||
| 56 | strict->import(); | ||
| 57 | warnings->import(); | ||
| 58 | feature->import(':5.10'); | ||
| 59 | |||
| 60 | # Method::Signatures needs a little more magic. | ||
| 61 | |||
| 62 | my $caller = caller; | ||
| 63 | |||
| 64 | ## no critic (ProhibitStringyEval) | ||
| 65 | |||
| 66 | eval qq{ | ||
| 67 | package $caller; | ||
| 68 | Method::Signatures->import; | ||
| 69 | 1; | ||
| 70 | } or die $@; | ||
| 71 | |||
| 72 | ## use critic | ||
| 73 | |||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | # Right now we make sure anything using Exobrain is going to use | ||
| 78 | # the user's timezone (if set). | ||
| 79 | |||
| 80 | method BUILD(...) { | ||
| 81 | $self->_set_timezone(); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | # This sets our timezone based upon what's in our config file, | ||
| 86 | # so any use of localtime() should use the user's local timezone. | ||
| 87 | |||
| 88 | method _set_timezone($tz?) { | ||
| 89 | $tz //= $self->config->{General}{timezone}; | ||
| 90 | |||
| 91 | if ($tz) { | ||
| 92 | $ENV{TZ} = $tz; ## no critic RequireLocalizedPunctuationVars | ||
| 93 | tzset(); | ||
| 94 | } | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _build_config { return Exobrain::Config->new; }; | ||
| 99 | sub _build_pub { return Exobrain::Bus->new(type => 'PUB', exobrain => shift) } | ||
| 100 | sub _build_sub { return Exobrain::Bus->new(type => 'SUB', exobrain => shift) } | ||
| 101 | |||
| 102 | |||
| 103 | method watch_loop( | ||
| 104 | Str :$class!, | ||
| 105 | CodeRef :$filter, | ||
| 106 | CodeRef :$then!, | ||
| 107 | CodeRef :$debug?, | ||
| 108 | ) { | ||
| 109 | |||
| 110 | # Load our component, because that means we immediately get | ||
| 111 | # an error if that class doesn't exist. | ||
| 112 | |||
| 113 | $self->_load_component($class); | ||
| 114 | |||
| 115 | while (my $event = $self->sub->get) { | ||
| 116 | my $namespace = $event->namespace; | ||
| 117 | |||
| 118 | if (grep { $_ eq $class } ($namespace, @{ $event->roles })) { | ||
| 119 | |||
| 120 | # Note that we have to cast it to the namespace on the | ||
| 121 | # packet (which is a class), and not the $class argument | ||
| 122 | # (which could be a role!) | ||
| 123 | # | ||
| 124 | # Yes, this code should be simplified/improved! | ||
| 125 | |||
| 126 | $event = $event->to_class($namespace); | ||
| 127 | |||
| 128 | $debug->($event) if $debug; | ||
| 129 | |||
| 130 | if ($filter) { | ||
| 131 | |||
| 132 | # Check our filter, and skip if required | ||
| 133 | local $_ = $event; | ||
| 134 | next unless $filter->($event); | ||
| 135 | |||
| 136 | } | ||
| 137 | |||
| 138 | # Everything passes! Trigger our callback | ||
| 139 | { local $_ = $event; $then->($event); } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | use constant NOTIFY => 'Notify'; | ||
| 146 | |||
| 147 | method notify($message, @args) { | ||
| 148 | return $self->intent( NOTIFY, | ||
| 149 | message => $message, | ||
| 150 | @args, | ||
| 151 | ); | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | method message(@args) { | ||
| 156 | return Exobrain::Message::Raw->new( | ||
| 157 | exobrain => $self, | ||
| 158 | @args, | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | |||
| 162 | method message_class($class, @args) { | ||
| 163 | $class = $self->_load_component($class); | ||
| 164 | |||
| 165 | return $class->new( | ||
| 166 | exobrain => $self, | ||
| 167 | @args, | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | use constant MEASURE_PREFIX => 'Measurement::'; | ||
| 173 | |||
| 174 | method measure($type, @args) { | ||
| 175 | |||
| 176 | my $class = $self->_load_component( MEASURE_PREFIX . $type ); | ||
| 177 | |||
| 178 | return $class->new( | ||
| 179 | exobrain => $self, | ||
| 180 | @args, | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | use constant INTENT_PREFIX => 'Intent::'; | ||
| 186 | |||
| 187 | method intent($type, @args) { | ||
| 188 | my $class = $self->_load_component( INTENT_PREFIX . $type ); | ||
| 189 | |||
| 190 | return $class->new( | ||
| 191 | exobrain => $self, | ||
| 192 | @args, | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | use constant AGENT_PREFIX => 'Agent::'; | ||
| 198 | |||
| 199 | method run(Str $class) { | ||
| 200 | my $agent = $self->_load_component( AGENT_PREFIX . $class )->new; | ||
| 201 | |||
| 202 | return $agent->start; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | method can_run(Str $class) { | ||
| 207 | my $agent = $self->_load_component( AGENT_PREFIX . $class ); | ||
| 208 | |||
| 209 | if ($agent->DOES('Exobrain::Agent::Depends')) { | ||
| 210 | return $agent->check_dependencies; | ||
| 211 | } | ||
| 212 | |||
| 213 | return 1; | ||
| 214 | } | ||
| 215 | |||
| 216 | # Loads a class, automatically adding Exobrain if | ||
| 217 | # required. Returns the class loaded. | ||
| 218 | |||
| 219 | use constant CLASS_PREFIX => 'Exobrain::'; | ||
| 220 | |||
| 221 | method _load_component(Str $class) { | ||
| 222 | $class = CLASS_PREFIX . $class; | ||
| 223 | |||
| 224 | eval "require $class"; | ||
| 225 | croak $@ if $@; | ||
| 226 | |||
| 227 | return $class; | ||
| 228 | } | ||
| 229 | |||
| 230 | __PACKAGE__->meta->make_immutable; | ||
| 231 | |||
| 232 | |||
| 233 | 1; | ||
| 234 | |||
| 235 | __END__ | ||