blib/lib/Synth/Config.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Synth::Config;
2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Synthesizer settings librarian
5  
6 our $VERSION = '0.0063';
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use GraphViz2 ();
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use List::Util qw(first);
13 use Mojo::JSON qw(from_json to_json);
14 use Mojo::SQLite ();
15 use YAML qw(Load LoadFile);
16 use namespace::clean;
17  
18  
19 has model => (
20 is => 'rw',
21 );
22  
23  
24 has dbname => (
25 is => 'ro',
26 required => 1,
27 default => sub { 'synth-config.db' },
28 );
29  
30 has _sqlite => (is => 'lazy');
31  
32 sub _build__sqlite {
33 my ($self) = @_;
34 my $sqlite = Mojo::SQLite->new('sqlite:' . $self->dbname);
35 return $sqlite->db;
36 }
37  
38  
39 has verbose => (
40 is => 'ro',
41 isa => sub { croak "$_[0] is not a boolean" unless $_[0] =~ /^[01]$/ },
42 default => sub { 0 },
43 );
44  
45  
46 sub BUILD {
47 my ($self, $args) = @_;
48 return unless $args->{model};
49 # sanitize the model name
50 (my $model = $args->{model}) =~ s/\W/_/g;
51 $self->model(lc $model);
52 # create the model table unless it's already there
53 $self->_sqlite->query(
54 'create table if not exists '
55 . $self->model
56 . ' (
57 id integer primary key autoincrement,
58 settings json not null,
59 name text not null
60 )'
61 );
62 # create the model specs table unless it's already there
63 $self->_sqlite->query(
64 'create table if not exists specs'
65 . ' (
66 id integer primary key autoincrement,
67 model text not null,
68 spec json not null
69 )'
70 );
71 }
72  
73  
74 sub make_setting {
75 my ($self, %args) = @_;
76 my $id = delete $args{id};
77 my $name = delete $args{name};
78 croak 'No columns given' unless keys %args;
79 if ($id) {
80 my $result = $self->_sqlite->select(
81 $self->model,
82 ['settings'],
83 { id => $id },
84 )->expand(json => 'settings')->hash->{settings};
85 for my $arg (keys %args) {
86 $args{$arg} = '' unless defined $args{$arg};
87 }
88 my $params = { %$result, %args };
89 $self->_sqlite->update(
90 $self->model,
91 { settings => to_json($params) },
92 { id => $id },
93 );
94 }
95 elsif ($name) {
96 $id = $self->_sqlite->insert(
97 $self->model,
98 {
99 name => $name,
100 settings => to_json(\%args),
101 },
102 )->last_insert_id;
103 }
104 return $id;
105 }
106  
107  
108 sub recall_setting {
109 my ($self, %args) = @_;
110 my $id = $args{id};
111 croak 'No id given' unless $id;
112 my $result = $self->_sqlite->select(
113 $self->model,
114 ['name', 'settings'],
115 { id => $id },
116 )->expand(json => 'settings')->hash;
117 my $setting = $result->{settings};
118 $setting->{id} = $id;
119 $setting->{name} = $result->{name};
120 return $setting;
121 }
122  
123  
124 sub search_settings {
125 my ($self, %args) = @_;
126 my $name = delete $args{name};
127 my @where;
128 push @where, "name = '$name'" if $name;
129 for my $arg (keys %args) {
130 next unless $args{$arg};
131 $args{$arg} =~ s/'/''/g; # escape the single-quote
132 push @where, q/json_extract(settings, '$./ . $arg . q/') = / . "'$args{$arg}'";
133 }
134 return [] unless @where;
135 my $sql = q/select id,name,settings,json_extract(settings, '$.group') as mygroup, json_extract(settings, '$.parameter') as parameter from /
136 . $self->model
137 . ' where ' . join(' and ', @where)
138 . ' order by name,mygroup,parameter';
139 print "Search SQL: $sql\n" if $self->verbose;
140 my $results = $self->_sqlite->query($sql);
141 my @settings;
142 while (my $next = $results->hash) {
143 my $set = from_json($next->{settings});
144 $set->{id} = $next->{id};
145 $set->{name} = $next->{name};
146 push @settings, $set;
147 }
148 return \@settings;
149 }
150  
151  
152 sub recall_settings {
153 my ($self) = @_;
154 my $sql = q/select id,name,settings,json_extract(settings, '$.group') as mygroup from /
155 . $self->model
156 . ' order by name,mygroup';
157 my $results = $self->_sqlite->query($sql);
158 my @settings;
159 while (my $next = $results->hash) {
160 my $set = from_json($next->{settings});
161 $set->{id} = $next->{id};
162 $set->{name} = $next->{name};
163 push @settings, $set;
164 }
165 return \@settings;
166 }
167  
168  
169 sub recall_models {
170 my ($self) = @_;
171 my @models;
172 my $results = $self->_sqlite->query(
173 "select name from sqlite_schema where type='table' order by name"
174 );
175 while (my $next = $results->array) {
176 next if $next->[0] =~ /^sqlite/;
177 push @models, $next->[0];
178 }
179 return \@models;
180 }
181  
182  
183 sub recall_setting_names {
184 my ($self) = @_;
185 my @settings;
186 my $results = $self->_sqlite->query(
187 'select distinct name from ' . $self->model
188 );
189 while (my $next = $results->array) {
190 push @settings, $next->[0];
191 }
192 return \@settings;
193 }
194  
195  
196 sub remove_setting {
197 my ($self, %args) = @_;
198 my $id = $args{id};
199 croak 'No id given' unless $id;
200 $self->_sqlite->delete(
201 $self->model,
202 { id => $id }
203 );
204 }
205  
206  
207 sub remove_settings {
208 my ($self, %args) = @_;
209 my $name = $args{name};
210 my $where = $name ? { name => $name } : {};
211 $self->_sqlite->delete(
212 $self->model,
213 $where
214 );
215 }
216  
217  
218 sub remove_model {
219 my ($self) = @_;
220 $self->_sqlite->query(
221 'drop table ' . $self->model
222 );
223 }
224  
225  
226 sub make_spec {
227 my ($self, %args) = @_;
228 my $id = delete $args{id};
229 croak 'No columns given' unless keys %args;
230 if ($id) {
231 my $result = $self->_sqlite->select(
232 'specs',
233 ['spec'],
234 { id => $id },
235 )->expand(json => 'spec')->hash->{spec};
236 for my $arg (keys %args) {
237 $args{$arg} = '' unless defined $args{$arg};
238 }
239 my $params = { %$result, %args };
240 $self->_sqlite->update(
241 'specs',
242 { spec => to_json($params) },
243 { id => $id },
244 );
245 }
246 else {
247 $id = $self->_sqlite->insert(
248 'specs',
249 {
250 model => $self->model,
251 spec => to_json(\%args),
252 },
253 )->last_insert_id;
254 }
255 return $id;
256 }
257  
258  
259 sub recall_specs {
260 my ($self) = @_;
261 my $sql = q/select id,model,spec,json_extract(spec, '$.group') as mygroup from /
262 . 'specs'
263 . " where model = '" . $self->model . "'"
264 . ' order by model,mygroup';
265 my $results = $self->_sqlite->query($sql);
266 my $set;
267 while (my $next = $results->hash) {
268 $set = from_json($next->{spec});
269 $set->{id} = $next->{id};
270 $set->{model} = $next->{model};
271 }
272 return $set;
273 }
274  
275  
276 sub recall_spec {
277 my ($self, %args) = @_;
278 my $id = delete $args{id};
279 croak 'No id given' unless $id;
280 my $result = $self->_sqlite->select(
281 'specs',
282 ['spec'],
283 { id => $id },
284 )->expand(json => 'spec')->hash;
285 my $spec = $result->{spec};
286 $spec->{id} = $id;
287 return $spec;
288 }
289  
290  
291 sub remove_spec {
292 my ($self) = @_;
293 $self->_sqlite->delete(
294 'specs',
295 { model => $self->model }
296 );
297 }
298  
299  
300 sub import_specs {
301 my ($self, %options) = @_;
302  
303 croak 'Invalid specs file'
304 if $options{file} && !-e $options{file};
305  
306 my $config = $options{file}
307 ? LoadFile($options{file})
308 : Load($options{string});
309  
310 my $specs = $self->recall_specs;
311  
312 if ($specs && @$specs) {
313 print 'Removing spec from ', $self->model, "\n"
314 if $self->verbose;
315 $self->remove_spec;
316 }
317  
318 my @list = @{ $config->{specs} };;
319  
320 for my $name (@list) {
321 for my $spec (@{ $config->{$name} }) {
322 print "Adding $name spec to ", $self->model, "\n"
323 if $self->verbose;
324 $self->make_spec(name => $name, %$spec);
325 }
326 }
327  
328 return \@list;
329 }
330  
331  
332 sub import_patches {
333 my ($self, %options) = @_;
334  
335 croak 'Invalid settings file'
336 if $options{file} && !-e $options{file};
337  
338 my $config = $options{file}
339 ? LoadFile($options{file})
340 : Load($options{string});
341  
342 my $list = $options{patches} && @{ $options{patches} }
343 ? $options{patches}
344 : [ map { $_->{patch} } @{ $config->{patches} } ];
345  
346 for my $patch_name (@$list) {
347 my $settings = $self->search_settings(name => $patch_name);
348  
349 if ($settings && @$settings) {
350 print "Removing $patch_name setting from ", $self->model, "\n"
351 if $self->verbose;
352 $self->remove_settings(name => $patch_name);
353 }
354  
355 for my $patch (@{ $config->{patches} }) {
356 my $name = $patch->{patch};
357 next unless first { $_ eq $name } @$list;
358 for my $set (@{ $patch->{settings} }) {
359 my $group = $set->{group};
360 print "Adding $name $group setting to ", $self->model, "\n"
361 if $self->verbose;
362 $self->make_setting(name => $name, %$set);
363 }
364 }
365 }
366  
367 return $list;
368 }
369  
370  
371 sub graphviz {
372 my ($self, %options) = @_;
373  
374 croak 'No settings given' unless $options{settings};
375  
376 $options{render} ||= 0;
377 $options{path} ||= '.';
378 $options{extension} ||= 'png';
379 $options{shape} ||= 'oval';
380 $options{color} ||= 'grey';
381  
382 my $g = GraphViz2->new(
383 global => { directed => 1 },
384 node => { shape => $options{shape} },
385 edge => { color => $options{color} },
386 );
387 my (%edges, %sets, %labels, %seen, $patch_name);
388  
389 # collect settings by group
390 for my $set (@{ $options{settings} }) {
391 $patch_name = $set->{name};
392 my $from = $set->{group};
393 push @{ $sets{$from} }, $_ for @{ $set->{parameters} };
394 }
395  
396 # accumulate non-patch parameter = value labels
397 for my $from (keys %sets) {
398 my @labels;
399 for my $parameter (@{ $sets{$from} }) {
400 next if $parameter->{control} eq 'patch';
401 my $label = "$parameter->{param} = $parameter->{value}$parameter->{unit}";
402 my $key = "$from $label";
403 push @labels, $label unless $seen{$key}++;
404 }
405 $labels{$from} = join "\n", $from, @labels;
406 }
407  
408 # add patch edges
409 for my $from (keys %sets) {
410 for my $parameter (@{ $sets{$from} }) {
411 next if $parameter->{control} ne 'patch';
412 my $to = $parameter->{group_to};
413 my $param = $parameter->{param};
414 my $param_to = $parameter->{param_to};
415 my $key = "$from $param to $to $param_to";
416 my $label = "$param to $param_to";
417 my $from_label = $labels{$from};
418 my $to_label = exists $labels{$to}
419 ? $labels{$to}
420 : ucfirst lc $to;
421 $g->add_node(name => $to_label);
422 $g->add_edge(
423 from => $from_label,
424 to => $to_label,
425 label => $label,
426 ) unless $edges{$key}++;
427 }
428 }
429  
430 # save a file
431 if ($options{render}) {
432 my $model = $self->model;
433 (my $patch = $patch_name) =~ s/\W/_/g;
434 my $filename = "$options{path}/$model-$patch.$options{extension}";
435 $g->run(format => $options{extension}, output_file => $filename);
436 }
437  
438 return $g;
439 }
440  
441 1;
442  
443 __END__
↑ Summary