From: Chris W. <la...@us...> - 2005-02-28 00:59:29
|
Update of /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Config In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30824/OpenInteract2/Config Modified Files: Package.pm Log Message: OIN-137: move from package.conf to package.ini Index: Package.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Config/Package.pm,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** Package.pm 8 Feb 2005 01:27:23 -0000 1.21 --- Package.pm 28 Feb 2005 00:59:18 -0000 1.22 *************** *** 5,8 **** --- 5,9 ---- use strict; use base qw( Class::Accessor::Fast ); + use OpenInteract2::Config::Ini; use File::Basename qw( dirname ); use File::Spec::Functions qw( catfile rel2abs ); *************** *** 15,19 **** my ( $log ); ! use constant DEFAULT_FILENAME => 'package.conf'; my @REQUIRED_FIELDS = qw( name version ); --- 16,20 ---- my ( $log ); ! use constant DEFAULT_FILENAME => 'package.ini'; my @REQUIRED_FIELDS = qw( name version ); *************** *** 32,37 **** # can have multiple items defined: # ! # author Larry Wall <la...@wa...> ! # author Chris Winters E<lt>ch...@cw...E<gt> # NOTE: If you add a field here you must also add it to @SERIAL_FIELDS --- 33,38 ---- # can have multiple items defined: # ! # author = Larry Wall <la...@wa...> ! # author = Chris Winters E<lt>ch...@cw...E<gt> # NOTE: If you add a field here you must also add it to @SERIAL_FIELDS *************** *** 75,79 **** $self->package_dir( rel2abs( dirname( $filename ) ) ); } ! return $self->init( $params ); } --- 76,80 ---- $self->package_dir( rel2abs( dirname( $filename ) ) ); } ! return $self->_initialize( $params ); } *************** *** 90,94 **** # OBJECT METHODS ! sub init { my ( $self, $params ) = @_; for ( @FIELDS ) { --- 91,95 ---- # OBJECT METHODS ! sub _initialize { my ( $self, $params ) = @_; for ( @FIELDS ) { *************** *** 202,309 **** unless ( $self->filename() ) { ! oi_error "Save failed: set filename first"; } - $self->check_required_fields; - eval { open( CONF, '> ' . $self->filename ) || die $! }; - if ( $@ ) { - oi_error "Cannot open [", $self->filename, "] for writing: $@"; - } - foreach my $field ( @SERIAL_FIELDS ) { - if ( $LIST_FIELDS{ $field } ) { - my $values = $self->$field() || []; - foreach my $value ( @{ $values } ) { - print CONF $self->_line_single_value( $field, $value ); - } ! } ! elsif ( $HASH_FIELDS{ $field } ) { ! my $h = $self->$field() || {}; ! while ( my ( $key, $value ) = each %{ $h } ) { ! print CONF $self->_line_keyed_value( $field, $key, $value ); } } - elsif ( $field eq 'description' ) { - print CONF "$field\n", $self->description, "\n"; - } else { ! print CONF $self->_line_single_value( $field, $self->$field() ); } } ! close( CONF ); return $self->filename; } - - # Read in the configuration, returning a hashref of data. Fields in - # %LIST_FIELDS are read into an arrayref (even if only one exists); - # fields in %HASH_FIELDS are read into a hashref - sub _read_config { my ( $class, $filename ) = @_; unless ( -f $filename ) { ! die "Package configuration file [$filename] does not exist.\n"; ! } ! eval { open( CONF, '<', $filename ) || die $! }; ! if ( $@ ) { ! oi_error "Cannot open package configuration [$filename]: $@"; } ! my %config = (); ! while ( <CONF> ) { ! next if ( /^\s*\#/ ); ! next if ( /^\s*$/ ); ! chomp; ! s/\r//g; ! s/^\s+//; ! s/\s+$//; ! my ( $field, $value ) = split /\s+/, $_, 2; ! last if ( $field eq 'description' ); ! ! # If there are multiple values possible, make a list ! ! if ( $LIST_FIELDS{ $field } and $value ) { ! push @{ $config{ $field } }, $value; ! } ! ! # Otherwise, if it's a key -> key -> value set; add to list ! ! elsif ( $HASH_FIELDS{ $field } and $value ) { ! my ( $sub_key, $sub_value ) = split /\s+/, $value, 2; ! $config{ $field }->{ $sub_key } = $sub_value; } - - # If not all that, then simple key -> value - else { ! $config{ $field } = $value; } } ! ! # Once all that is done, read the description in all at once ! ! { ! local $/ = undef; ! $config{description} = <CONF>; ! } ! chomp $config{description}; ! close( CONF ); ! return \%config; ! } ! ! ! sub _line_single_value { ! my ( $class, $field, $value ) = @_; ! $value ||= ''; ! return sprintf "%-20s%s\n", $field, $value; } - sub _line_keyed_value { - my ( $class, $field, $key, $value ) = @_; - $value ||= ''; - return sprintf "%-20s%-15s%s\n", $field, $key, $value; - } - 1; --- 203,248 ---- unless ( $self->filename() ) { ! oi_error "Package configuration save failed: set filename first"; } $self->check_required_fields; ! my $ini = OpenInteract2::Config::Ini->new(); ! foreach my $field ( @SERIAL_FIELDS ) { ! if ( $HASH_FIELDS{ $field } ) { ! my $values = $self->$field() || {}; ! for ( keys %{ $values } ) { ! $ini->set( 'package', $field, $_, $values->{ $_ } ); } } else { ! $ini->set( 'package', $field, $self->$field() ); } } ! $ini->write_file( $self->filename() ); ! return $self->filename; } sub _read_config { my ( $class, $filename ) = @_; unless ( -f $filename ) { ! oi_error "Package configuration file '$filename' does not exist."; } ! my $ini = OpenInteract2::Config::Ini->new({ filename => $filename }); ! return {} unless ( $ini->{package} ); ! my %params = (); ! while ( my ( $key, $value ) = each %{ $ini->{package} } ) { ! if ( $LIST_FIELDS{ $key } ) { ! $params{ $key } = ( ref $value eq 'ARRAY' ) ? $value : [ $value ]; } else { ! $params{ $key } = $value; } } ! return \%params; } 1; *************** *** 317,330 **** # Sample package file ! name MyPackage ! version 1.53 ! author Steve <st...@du...> ! author Chuck <ch...@gu...> ! url http://www.oirox.com/ ! template_plugin TestPlugin OpenInteract2::Plugin::Test ! observer mywiki OpenInteract2::Filter::MyWiki ! description ! This package rocks! # Create a new package file from scratch --- 256,274 ---- # Sample package file ! ! [package] ! name = MyPackage ! version = 1.53 ! author = Steve <st...@du...> ! author = Chuck <ch...@gu...> ! url = http://www.oirox.com/ ! description = This package rocks! ! ! [package template_plugin] ! TestPlugin = OpenInteract2::Plugin::Test + [package observer] + mywiki = OpenInteract2::Filter::MyWiki + # Create a new package file from scratch *************** *** 337,392 **** $c->author( [ 'Steve <st...@du...>', 'Chuck <ch...@gu...>' ] ); $c->template_plugin({ TestPlugin => 'OpenInteract2::Plugin::Test' }); $c->description( 'This package rocks!' ); # Set the filename to save the config to and save it ! $c->filename( 'mydir/pkg/MyPackage/package.conf' ); eval { $c->save_config }; # Specify a directory for an existing config ! my $c = OpenInteract2::Config::Package->new( ! { directory => '/path/to/mypackage' } ); # Specify a filename for an existing config ! my $c = OpenInteract2::Config::Package->new( ! { filename => 'work/pkg/mypackage/package-alt.conf' } ); =head1 DESCRIPTION This class implements read/write access to a package configuration ! file. This is a very simple line-based format. There are three types ! of values: ! ! B<single value> ! ! Example: ! ! name MyPackage ! ! B<multiple value> ! ! Example: ! ! author Chris Winters E<lt>ch...@cw...E<gt> ! author Mario Lemiux <ma...@pg...> ! ! B<multiple keyed value> ! ! Example: ! ! template_plugin MyPlugin OpenInteract2::Template::Plugin::MyPlugin ! template_plugin MyPluginNew OpenInteract2::Template::Plugin::MyPluginNew ! observer mywiki OpenInteract2::Filter::MyWiki ! ! Additionally, all data below the last entry C<description> is used as ! the description. Example: ! ! description ! Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam ! nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat ! volutpat. Ut wisi enim ad minim veniam, quis nostrud exercitation ! ulliam corper suscipit lobortis nisl ut aliquip ex ea commodo =head1 METHODS --- 281,309 ---- $c->author( [ 'Steve <st...@du...>', 'Chuck <ch...@gu...>' ] ); $c->template_plugin({ TestPlugin => 'OpenInteract2::Plugin::Test' }); + $c->observer({ mywiki => 'OpenInteract2::Filter::MyWiki' }); $c->description( 'This package rocks!' ); # Set the filename to save the config to and save it ! $c->filename( 'mydir/pkg/MyPackage/package.ini' ); eval { $c->save_config }; # Specify a directory for an existing config ! my $c = OpenInteract2::Config::Package->new({ ! directory => '/path/to/mypackage' ! }); # Specify a filename for an existing config ! my $c = OpenInteract2::Config::Package->new({ ! filename => 'work/pkg/mypackage/package-alt.ini' ! }); =head1 DESCRIPTION This class implements read/write access to a package configuration ! file. As all other configurations in OI2 this uses the modified INI ! format. =head1 METHODS *************** *** 420,429 **** Create a filename for this configuration file given C<$directory>. The ! default name for the package configuration file is C<package.conf>. Examples: my $filename = OpenInteract2::Config::Package->create_filename( '/home/httpd/mysite/pkg/foo' ); ! # $filename: '/home/httpd/mysite/pkg/foo/package.conf' We do not check whether C<$directory> exists or whether the resulting --- 337,346 ---- Create a filename for this configuration file given C<$directory>. The ! default name for the package configuration file is C<package.ini>. Examples: my $filename = OpenInteract2::Config::Package->create_filename( '/home/httpd/mysite/pkg/foo' ); ! # $filename: '/home/httpd/mysite/pkg/foo/package.ini' We do not check whether C<$directory> exists or whether the resulting *************** *** 439,450 **** =head2 Object Methods - B<init( \%params )> - - Initialize the object with C<\%params>. Only fields listed in - L<PROPERTIES> will be set, and only properties with a value will be - set. - - Returns: object - B<author_names()> --- 356,359 ---- *************** *** 467,474 **** Example: ! name foo ! version 1.51 ! spops_file conf/object_one.ini ! spops_file conf/object_two.ini ... $config->package_dir( '/home/me/pkg' ) --- 376,383 ---- Example: ! name = foo ! version = 1.51 ! spops_file = conf/object_one.ini ! spops_file = conf/object_two.ini ... $config->package_dir( '/home/me/pkg' ) *************** *** 489,496 **** Example: ! name foo ! version 1.51 ! action_file conf/action_one.ini ! action_file conf/action_two.ini ... $config->package_dir( '/home/me/pkg' ) --- 398,405 ---- Example: ! name = foo ! version = 1.51 ! action_file = conf/action_one.ini ! action_file = conf/action_two.ini ... $config->package_dir( '/home/me/pkg' ) *************** *** 511,519 **** Example: ! name foo ! version 1.51 ! message_file data/foo-en.msg ! message_file data/foo-en_us.msg ! message_file data/foo-en_uk.msg ... $config->package_dir( '/home/me/pkg' ) --- 420,428 ---- Example: ! name = foo ! version = 1.51 ! message_file = data/foo-en.msg ! message_file = data/foo-en_us.msg ! message_file = data/foo-en_uk.msg ... $config->package_dir( '/home/me/pkg' ) *************** *** 574,596 **** B<spops_file> (\@) ! File with SPOPS objects defined in this package. If you do not specify ! these you must store all object configuration information in a single ! file C<conf/spops.ini>. Both perl- and INI-formatted configurations ! are acceptable. (TODO: true?) B<action_file> (\@) ! File with the actions defined in this package. If you do not specify ! these you must store all action information in a single file ! C<conf/action.ini>. Both perl- and INI-formatted configurations are ! acceptable. (TODO: true?) B<message_file> (\@) ! File with the localized messages used in your application. If you do ! not specify these you must store your message files in a subdirectory ! C<msg/> and in files ending with C<.msg>. The format of these files is ! discussed in L<OpenInteract2::I18N|OpenInteract2::I18N> and ! L<OpenInteract2::Manual::I18N|OpenInteract2::Manual::I18N>. B<module> (\@) --- 483,499 ---- B<spops_file> (\@) ! File(s) with SPOPS objects defined in this package. B<action_file> (\@) ! File(s) with the actions defined in this package. B<message_file> (\@) ! File(s) with the localized messages used in your application. If you ! do not specify these you must store your message files in a ! subdirectory C<msg/> and in files ending with C<.msg>. The format of ! these files is discussed in L<OpenInteract2::I18N|OpenInteract2::I18N> ! and L<OpenInteract2::Manual::I18N|OpenInteract2::Manual::I18N>. B<module> (\@) *************** *** 628,643 **** C<description> ($*) ! Description of this package. Instead of reading a single line we read ! every line after the 'description' key to the end of the file. Do not ! put additional configuration keys under 'description', they will not ! be read. ! ! =head1 BUGS ! ! None known. ! ! =head1 TO DO ! ! Nothing known. =head1 SEE ALSO --- 531,535 ---- C<description> ($*) ! Description of this package. =head1 SEE ALSO *************** *** 647,650 **** --- 539,544 ---- L<Class::Accessor|Class::Accessor> + L<OpenInteract2::Config::Ini> + =head1 COPYRIGHT |