File: config-model-edit

package info (click to toggle)
libconfig-model-itself-perl 1.215-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 292 kB
  • ctags: 16
  • sloc: perl: 2,427; makefile: 4
file content (396 lines) | stat: -rw-r--r-- 9,996 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/perl

#    Copyright (c) 2007-2010 Dominique Dumont.
#
#    This file is part of Config-Model-Itself.
#
#    Config-Model is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser Public License as
#    published by the Free Software Foundation; either version 2.1 of
#    the License, or (at your option) any later version.
#
#    Config-Model is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser Public License for more details.
#
#    You should have received a copy of the GNU Lesser Public License
#    along with Config-Model; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
#    02110-1301 USA

use strict ;
use warnings ;

use Config::Model;
use Getopt::Long ;
use Pod::Usage ;
use Log::Log4perl ;
use Config::Model::Itself ;
use YAML::Any;
use Tk ;
use Config::Model::TkUI ;
use Config::Model::Itself::TkEditUI ;

# lame tracing that will be replaced by Log4perl
use vars qw/$verbose $debug/ ;

$verbose = 0;
$debug = 0;

my $system_log4perl_conf_file = '/etc/log4config-model-edit.conf' ;
my $user_log4perl_conf_file = 'log4config-model-edit.conf' ;
my $fallback_conf = << 'EOC';
log4perl.logger=WARN, Screen
log4perl.appender.Screen        = Log::Log4perl::Appender::Screen
log4perl.appender.Screen.stderr = 0
log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Screen.layout.ConversionPattern = %d %m %n
EOC

my $log4perl_conf 
    = -e $user_log4perl_conf_file   ?  $user_log4perl_conf_file 
    : -e $system_log4perl_conf_file ?  $system_log4perl_conf_file 
    :                                 \$fallback_conf ;

Log::Log4perl::init($log4perl_conf);

use lib qw/lib/ ;

my $dev_model_dir = 'lib/Config/Model/models/';
my $root_model ;
my $trace = 0 ;


=head1 NAME

config-model-edit -  Graphical model editor for Config::Model

=head1 SYNOPSIS

  config-model-edit [options] -model Sshd [ class:Sshd element:Foo ... ]

=head1 DESCRIPTION

config-model-edit will provides a Perl/Tk graphical interface to edit
configuration models that will be used by Config::Model.

Config::Model is a general purpose configuration framework based on
configuration models (See L<Config::Model> for details).

This configuration model is also expressed as structured data. This
structure data is structured and follow a set of rules which are
described for humans in L<Config::Model>.

The structure and rules documented in L<Config::Model> are also expressed
in a model in the files provided with L<Config::Model::Itself>.

Hence the possibity to verify, modify configuration data provided by
Config::Model can also be applied on configuration models using the
same user interface as L<config-edit>.

The model editor program is config-model-edit.

=head1 USAGE

By default, C<config-model-edit> will try to load a model file from
C<lib/Config/Model/models>. If no model is found, C<config-model-edit>
will try to load installed models (i.e. located in
C</usr/share/perl/...>). 

Modified models will be saved (by default) in
C<$PWD/lib/Config/Model/models/>.

You can override this behavior with option C<-dir>.

When you specify a C<-model> options, only configuration models matching
this options will be loaded. I.e.

  config-model-edit -model Xorg

will load models C<Xorg> (file C<Xorg.pl>) and all other C<Xorg::*> like
C<Xorg::Screen> (file C<Xorg/Screen.pl>).

=head1 Options

=over

=item -model

Mandatory option that specifies the configuration model to be
edited.

=item -verbose

Be (very) verbose

=item -debug

Provide debug infos.

=item -trace

Provides a full stack trace when exiting on error.

=item -force_load

Load file even if error are found in data. Bad data are discarded

=item -dot_diagram

Returns a dot file that represent the stucture of the configuration
model. C<include> are represented by solid lines. Class usage
(i.e. C<config_class_name> parameter) is represented by dashed
lines. The name of the element is attached to the dashed line.

=item -dump [ file ]

Dump configuration content on STDOUT or in the specified with
Config::Model syntax.

By default, dump only custom values, i.e. different from application
built-in values or model default values. See -dumptype option for
other types of dump

=item -dumptype [ full | preset | custom ]

Choose to dump every values (full), only preset values or only
customized values (default)

=item -load <cds_file_to_load> | -

Load model from cds file (using Config::Model serialisation format,
typically done with -dump option). This option can be used with
C<-save> to directly save a model loaded from the cds file or from
STDIN.

=item -load_yaml <yaml_file_to_load> | -

Load configuration data in model from cds file (using Config::Model
serialisation format, typically done with -dump_yaml option). This
option can be used with C<-save> to directly save a model loaded from
the YAML file or from STDIN.

=item -save

Force a save of the model even if no edition was done. This option is
useful to migrate a model when Config::Model model feature changes.

=item -model_dir

Directory where to read and write model

=back


=cut

sub load_data {
    my $load_file = shift ;

    my @data ;
    if ( $load_file eq '-' ) {
	@data = <STDIN> ;
    }
    else {
	open(LOAD,$load_file) || die "cannot open load file $load_file:$!";
	@data = <LOAD> ;
	close LOAD; 
    }

    return wantarray ? @data : join('',@data);
}

my $man = 0;
my $help = 0;
my $force_load = 0;
my $model_dir ;
my $do_dot = 0;
my $do_dump;
my $dumptype;
my $do_yaml = 0;
my $load_yaml ;
my $save = 0;
my $load ;
my $model_modified = 0;

my $result = GetOptions (
			 "dir=s"            => \$model_dir,
			 "model=s"          => \$root_model,
			 "verbose!"         => \$verbose,
			 "debug!"           => \$debug,
			 "trace!"           => \$trace,
			 "man!"             => \$man,
			 "help!"            => \$help,
			 "force_load!"      => \$force_load,
			 "save!"            => \$save,
			 "dot_diagram!"     => \$do_dot ,
			 "dump!"            => \$do_dump ,
                         "dumptype:s"       => \$dumptype,
                         "load=s"           => \$load,
                         "load_yaml=s"      => \$load_yaml,
			 "dump_yaml!"       => \$do_yaml ,
			);

pod2usage(2) if not $result ;
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $man;

Config::Model::Exception::Any->Trace(1) if $trace ;

die "Unspecified root configuration model (option -model)\n"
  unless defined $root_model ;

my $wr_model_dir = $model_dir || $dev_model_dir ;

if (! -d $wr_model_dir) {
    mkdir $wr_model_dir, 0755 || die "can't create $wr_model_dir:$!";
}

my $meta_model = Config::Model -> new();

my $meta_inst 
  = $meta_model->instance (root_class_name => 'Itself::Model' ,
			   instance_name   => $root_model.' model' ,
			   force_load      => $force_load,
			  );

my $meta_root = $meta_inst -> config_root ;

# now load model

my $rw_obj = Config::Model::Itself -> new(model_object => $meta_root ) ;

my $read_model_dir = $model_dir || $dev_model_dir ;

if (not -e $read_model_dir.'/'.$root_model.'.pl') {
    $read_model_dir =  $INC{'Config/Model.pm'} ;
    $read_model_dir =~ s/\.pm//;
    $read_model_dir .= '/models' ;
}

$rw_obj -> read_all( model_dir  => $read_model_dir, 
		     force_load => $force_load ,
		     root_model => $root_model,
		     legacy =>'ignore',
		   ) ;

$meta_inst->push_no_value_check(qw/store type/) if $force_load ;

if (defined $load) {
    my $data = load_data($load) ;
    $meta_root->load($data);
    $model_modified = 1;
}

if (defined $load_yaml) {
    my $yaml = load_data($load_yaml) ;
    my $pdata = Load($yaml) ;
    $meta_root->load_data($pdata) ;
    $model_modified = 1;
}

if (@ARGV) {
    $meta_root->load("@ARGV") ;
    $model_modified = 1;
}

$meta_inst->pop_no_value_check() if $force_load ;


if ($do_dot) {
    print $rw_obj->get_dot_diagram ;
    exit ;
}

if (defined $do_dump) {
    my $dump_string = $meta_root->dump_tree( mode => $dumptype || 'custom' ) ;
    if ($do_dump) {
	open(DUMP,">$do_dump") or die "cannot dump in $do_dump:$!";
	print DUMP $dump_string ;
	close DUMP;
    }
    else {
	print $dump_string ;
    }
    exit ;
}

if ($do_yaml) {
    require YAML::Tiny;
    import YAML::Tiny qw/Dump/;
    print Dump($meta_root->dump_as_data(ordered_hash_as_list => 0)) ;
    exit ;
}

my $write_sub = sub { 
    my $wr_dir = shift || $wr_model_dir ;
    $rw_obj->write_all(model_dir => $wr_dir);
} ;

if ($save) {
    &$write_sub ;
    exit ;
}

my $mw = MainWindow-> new ;
$mw->withdraw ;
# Thanks to Jerome Quelin for the tip
$mw->optionAdd('*BorderWidth' => 1);

$model_modified = 1 if $wr_model_dir ne $read_model_dir ;


my $cmu = $mw->ConfigModelEditUI (-root      => $meta_root,
				  -store_sub => $write_sub,
  				  -model_dir => $wr_model_dir,
				  -model_name => $root_model,
				  -model_modified => $model_modified,
				 ) ;

&MainLoop ; # Tk's

=head1 LOGGING

All Config::Model logging is (slowly) moved from klunky debug and
verbose prints to L<Log::Log4perl>. Logging can be configured in the
following files:

=over

=item *

 ~/.log4config-model

=item * 

 /etc/log4config-model.conf

=back

Without these files, the following Log4perl config is used:

 log4perl.logger=WARN, Screen
 log4perl.appender.Screen        = Log::Log4perl::Appender::Screen
 log4perl.appender.Screen.stderr = 0
 log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
 log4perl.appender.Screen.layout.ConversionPattern = %d %m %n

Log4Perl categories are shown in L<config-edit/LOGGING>

=head1 AUTHOR

Dominique Dumont, ddumont at cpan dot org

=head1 SEE ALSO

L<Config::Model>, 
L<Config::Model::Node>, 
L<Config::Model::Instance>, 
L<Config::Model::HashId>,
L<Config::Model::ListId>,
L<Config::Model::WarpedNode>,
L<Config::Model::Value>

=cut