From: Tara L Andrews Date: Fri, 14 Sep 2012 12:06:27 +0000 (+0200) Subject: load extensions statically to avoid bad object wrapping interactions X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=37bf09f44d828c64eef875cfd35371eae4e82ce9;p=scpubgit%2Fstemmatology.git load extensions statically to avoid bad object wrapping interactions --- diff --git a/analysis/lib/Text/Tradition/Analysis.pm b/analysis/lib/Text/Tradition/Analysis.pm index 633a1db..f245b94 100644 --- a/analysis/lib/Text/Tradition/Analysis.pm +++ b/analysis/lib/Text/Tradition/Analysis.pm @@ -16,7 +16,7 @@ use TryCatch; use vars qw/ @EXPORT_OK $VERSION /; @EXPORT_OK = qw/ run_analysis group_variants analyze_variant_location wit_stringify /; -$VERSION = "1.0"; +$VERSION = "1.1"; my $SOLVER_URL = 'http://byzantini.st/cgi-bin/graphcalc.cgi'; @@ -34,7 +34,6 @@ Text::Tradition::Analysis - functions for stemma analysis of a tradition 'name' => 'this is a text', 'input' => 'TEI', 'file' => '/path/to/tei_parallel_seg_file.xml' ); - $t->enable_stemmata; $t->add_stemma( 'dotfile' => $stemmafile ); my $variant_data = run_analysis( $tradition ); @@ -87,7 +86,6 @@ my $datafile = 't/data/florilegium_tei_ps.xml'; my $tradition = Text::Tradition->new( 'input' => 'TEI', 'name' => 'test0', 'file' => $datafile ); -$tradition->enable_stemmata; my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' ); is( ref( $s ), 'Text::Tradition::Stemma', "Added stemma to tradition" ); diff --git a/analysis/lib/Text/Tradition/Analysis/Result.pm b/analysis/lib/Text/Tradition/Analysis/Result.pm index 2e920ff..da1e123 100644 --- a/analysis/lib/Text/Tradition/Analysis/Result.pm +++ b/analysis/lib/Text/Tradition/Analysis/Result.pm @@ -88,7 +88,6 @@ my $datafile = 't/data/florilegium_tei_ps.xml'; my $tradition = Text::Tradition->new( 'input' => 'TEI', 'name' => 'flortest', 'file' => $datafile ); -$tradition->enable_stemmata; my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' ); my $sets = [ [ qw/ D Q / ], [ qw/ F H / ], [ qw/ A B C P S T / ] ]; diff --git a/analysis/lib/Text/Tradition/HasStemma.pm b/analysis/lib/Text/Tradition/HasStemma.pm index aa2e761..70bab13 100644 --- a/analysis/lib/Text/Tradition/HasStemma.pm +++ b/analysis/lib/Text/Tradition/HasStemma.pm @@ -66,7 +66,6 @@ my $t = Text::Tradition->new( 'input' => 'Tabular', 'file' => 't/data/simple.txt', ); -$t->enable_stemmata; is( $t->stemma_count, 0, "No stemmas added yet" ); my $s; ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" ); diff --git a/analysis/lib/Text/Tradition/Stemma.pm b/analysis/lib/Text/Tradition/Stemma.pm index 5efaf70..764aa2f 100644 --- a/analysis/lib/Text/Tradition/Stemma.pm +++ b/analysis/lib/Text/Tradition/Stemma.pm @@ -527,7 +527,7 @@ sub hypotheticals { return @wits; } -=head2 root_graph( $root_vertex ) { +=head2 root_graph( $root_vertex ) If the stemma graph is undirected, make it directed with $root_vertex at the root. If it is directed, re-root it. diff --git a/analysis/t/analysis.t b/analysis/t/analysis.t index 08ac38f..bc5661f 100755 --- a/analysis/t/analysis.t +++ b/analysis/t/analysis.t @@ -15,7 +15,6 @@ eval { no warnings; binmode $DB::OUT, ':utf8'; $DB::deep = 1000 }; my $tradition = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/besoin.xml' ); -$tradition->enable_stemmata; $tradition->add_stemma( 'dotfile' => 't/data/besoin.dot' ); # Run the analysis of the tradition diff --git a/analysis/t/stemma.t b/analysis/t/stemma.t index 094c30b..6b361cf 100644 --- a/analysis/t/stemma.t +++ b/analysis/t/stemma.t @@ -22,7 +22,6 @@ $c->add_relationship( 'n9', 'n10', { 'type' => 'spelling' } ); $c->add_relationship( 'n12', 'n13', { 'type' => 'spelling' } ); $c->calculate_ranks(); -$tradition->enable_stemmata; my $stemma = $tradition->add_stemma( dotfile => 't/data/simple.dot' ); # Test for object creation diff --git a/analysis/t/text_tradition_analysis.t b/analysis/t/text_tradition_analysis.t index 1ebf655..f568b36 100644 --- a/analysis/t/text_tradition_analysis.t +++ b/analysis/t/text_tradition_analysis.t @@ -15,7 +15,6 @@ my $datafile = 't/data/florilegium_tei_ps.xml'; my $tradition = Text::Tradition->new( 'input' => 'TEI', 'name' => 'test0', 'file' => $datafile ); -$tradition->enable_stemmata; my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' ); is( ref( $s ), 'Text::Tradition::Stemma', "Added stemma to tradition" ); diff --git a/analysis/t/text_tradition_analysis_result.t b/analysis/t/text_tradition_analysis_result.t index 2997818..e3462dc 100644 --- a/analysis/t/text_tradition_analysis_result.t +++ b/analysis/t/text_tradition_analysis_result.t @@ -20,7 +20,6 @@ my $datafile = 't/data/florilegium_tei_ps.xml'; my $tradition = Text::Tradition->new( 'input' => 'TEI', 'name' => 'flortest', 'file' => $datafile ); -$tradition->enable_stemmata; my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' ); my $sets = [ [ qw/ D Q / ], [ qw/ F H / ], [ qw/ A B C P S T / ] ]; diff --git a/analysis/t/text_tradition_hasstemma.t b/analysis/t/text_tradition_hasstemma.t index ecf253e..6f16b5e 100644 --- a/analysis/t/text_tradition_hasstemma.t +++ b/analysis/t/text_tradition_hasstemma.t @@ -15,7 +15,6 @@ my $t = Text::Tradition->new( 'input' => 'Tabular', 'file' => 't/data/simple.txt', ); -$t->enable_stemmata; is( $t->stemma_count, 0, "No stemmas added yet" ); my $s; ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" ); diff --git a/base/lib/Text/Tradition.pm b/base/lib/Text/Tradition.pm index 59bf001..fb66a03 100644 --- a/base/lib/Text/Tradition.pm +++ b/base/lib/Text/Tradition.pm @@ -11,7 +11,13 @@ use Text::Tradition::User; use TryCatch; use vars qw( $VERSION ); -$VERSION = "1.0"; +$VERSION = "1.1"; + +# Enable plugin(s) if available +eval { with 'Text::Tradition::HasStemma'; }; +if( $@ ) { + warn "Text::Tradition::Analysis not found. Disabling stemma analysis functionality"; +}; has 'collation' => ( is => 'ro', @@ -320,55 +326,10 @@ sub add_json_witnesses { } } -=head1 PLUGIN HOOKS - -=head2 enable_stemmata - -If the tradition in question does not have the HasStemma role, make it so. Throws -an error if the role (ergo, if the Analysis package) is not installed. - -=cut - -sub enable_stemmata { - my $self = shift; - my $rolename = 'Text::Tradition::HasStemma'; - return 1 if does_role( $self, $rolename ); - try { - apply_all_roles( $self, $rolename ); - } catch { - throw( "Cannot apply role to enable stemmata; is the Analysis extension installed?" ); - } - return 1; -} - -=head2 enable_morphology - -If the tradition in question has readings that do not include the Morphology -role, apply the role to them. Throws an error if the role (ergo, if the -Morphology package) is not installed. - -=cut - -sub enable_morphology { - my $self = shift; - my $rolename = 'Text::Tradition::Morphology'; - try { - load( $rolename ); - } catch { - throw( "Cannot apply role to enable morphology; is the extension installed?" ); - } - foreach my $r ( $self->collation->readings ) { - apply_all_roles( $r, $rolename ) - unless does_role( $r, $rolename ); - } - return 1; -} - =head2 lemmatize Calls the appropriate lemmatization function for the language of the -tradition. Implicitly applies the Morphology role where appropriate (and -throws an error if the package is not installed.) +tradition. Will throw an error if the Morphology package is not installed. =cut @@ -379,7 +340,6 @@ sub lemmatize { warn "Please set a language to lemmatize a tradition"; return; } - $self->enable_morphology; my $mod = "Text::Tradition::Language::" . $self->language; load( $mod ); $mod->can( 'lemmatize' )->( $self ); diff --git a/base/lib/Text/Tradition/Collation.pm b/base/lib/Text/Tradition/Collation.pm index bf9832a..216e731 100644 --- a/base/lib/Text/Tradition/Collation.pm +++ b/base/lib/Text/Tradition/Collation.pm @@ -910,14 +910,8 @@ is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" ); is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" ); # Now add a stemma, write to GraphML, and look at the output. -my $SKIP_STEMMA; -try { - $tradition->enable_stemmata; -} catch { - $SKIP_STEMMA = 1; -} SKIP: { - skip "Analysis module not present", 3 if $SKIP_STEMMA; + skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' ); my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' ); is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" ); is( $tradition->stemmata, 1, "Tradition now has the stemma" ); diff --git a/base/lib/Text/Tradition/Collation/Reading.pm b/base/lib/Text/Tradition/Collation/Reading.pm index ed82b57..06ee68a 100644 --- a/base/lib/Text/Tradition/Collation/Reading.pm +++ b/base/lib/Text/Tradition/Collation/Reading.pm @@ -14,6 +14,13 @@ subtype 'ReadingID', no Moose::Util::TypeConstraints; +# Enable plugin(s) if available +eval { with 'Text::Tradition::Morphology'; }; +# Morphology package is not on CPAN, so don't warn of its absence +# if( $@ ) { +# warn "Text::Tradition::Morphology not found: $@. Disabling lexeme functionality"; +# }; + =head1 NAME Text::Tradition::Collation::Reading - represents a reading (usually a word) @@ -199,12 +206,9 @@ around BUILDARGS => sub { sub BUILD { my( $self, $args ) = @_; if( exists $args->{'lexemes'} ) { - unless( does_role( $self, 'Text::Tradition::Morphology' ) ) { - eval { apply_all_roles( $self, 'Text::Tradition::Morphology' ) }; - if( $@ ) { - warn "No morphology package installed; DROPPING lexemes"; - return; - } + unless( $self->can( '_deserialize_lexemes' ) ) { + warn "No morphology package installed; DROPPING lexemes"; + return; } $self->_deserialize_lexemes( $args->{'lexemes'} ); } diff --git a/base/lib/Text/Tradition/Directory.pm b/base/lib/Text/Tradition/Directory.pm index f301292..d99a888 100644 --- a/base/lib/Text/Tradition/Directory.pm +++ b/base/lib/Text/Tradition/Directory.pm @@ -32,8 +32,7 @@ Text::Tradition::Directory - a KiokuDB interface for storing and retrieving trad ); my $tradition = Text::Tradition->new( @args ); - $tradition->enable_stemmata; - my $stemma = $tradition->add_stemma( dotfile => $dotfile ); + my $stemma = $tradition->add_stemma( dotfile => $dotfile ); # if Analysis module installed $d->save_tradition( $tradition ); foreach my $id ( $d->traditions ) { @@ -111,8 +110,7 @@ my $t = Text::Tradition->new( 'input' => 'Tabular', 'file' => 't/data/simple.txt', ); -my $stemma_enabled; -eval { $stemma_enabled = $t->enable_stemmata; }; +my $stemma_enabled = $t->can( 'add_stemma' ); { my $d = Text::Tradition::Directory->new( 'dsn' => $dsn, diff --git a/base/lib/Text/Tradition/Parser/Self.pm b/base/lib/Text/Tradition/Parser/Self.pm index 168ffd6..5d0858a 100644 --- a/base/lib/Text/Tradition/Parser/Self.pm +++ b/base/lib/Text/Tradition/Parser/Self.pm @@ -122,12 +122,7 @@ if( $t ) { # TODO add a relationship, add a stemma, write graphml, reparse it, check that # the new data is there $t->language('Greek'); -my $stemma_enabled; -try { - $stemma_enabled = $t->enable_stemmata; -} catch { - ok( 1, "Skipping stemma tests without Analysis module" ); -} +my $stemma_enabled = $t->can('add_stemma'); if( $stemma_enabled ) { $t->add_stemma( 'dotfile' => 't/data/florilegium.dot' ); } @@ -211,17 +206,13 @@ sub parse { $use_version = $val; } elsif( $gkey eq 'stemmata' ) { # Make sure we can handle stemmata - my $stemma_enabled; - try { - $stemma_enabled = $tradition->enable_stemmata; - } catch { - warn "Analysis module not installed; DROPPING stemmata"; - } # Parse the stemmata into objects - if( $stemma_enabled ) { + if( $tradition->can('add_stemma') ) { foreach my $dotstr ( split( /\n/, $val ) ) { $tradition->add_stemma( 'dot' => $dotstr ); } + } else { + warn "Analysis module not installed; DROPPING stemmata"; } } elsif( $gkey eq 'user' ) { # Assign the tradition to the user if we can @@ -249,7 +240,6 @@ sub parse { my %namechange = ( '#START#' => '__START__', '#END#' => '__END__' ); # print STDERR "Adding collation readings\n"; - my $need_morphology; foreach my $n ( @{$graph_data->{'nodes'}} ) { # If it is the start or end node, we already have one, so # grab the rank and go. @@ -261,21 +251,11 @@ sub parse { $collation->end->rank( $n->{'rank'} ); next; } - # HACKY but no better way yet - # If $n has a 'lexemes' property then we will need the morphology for - # the whole tradition. - $need_morphology = 1 if exists $n->{'lexemes'}; my $gnode = $collation->add_reading( $n ); if( $gnode->id ne $n->{'id'} ) { $namechange{$n->{'id'}} = $gnode->id; } } - # HACK continued - if any of the readings had morphology info, we - # must enable it for the whole tradition. Just eval it, as we will - # have already been warned if the morphology extension isn't installed. - if( $need_morphology ) { - eval { $tradition->enable_morphology }; - } # Now add the edges. # print STDERR "Adding collation path edges\n"; diff --git a/base/t/text_tradition_collation.t b/base/t/text_tradition_collation.t index f1bcba4..a83e11e 100644 --- a/base/t/text_tradition_collation.t +++ b/base/t/text_tradition_collation.t @@ -87,14 +87,8 @@ is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" ); is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" ); # Now add a stemma, write to GraphML, and look at the output. -my $SKIP_STEMMA; -try { - $tradition->enable_stemmata; -} catch { - $SKIP_STEMMA = 1; -} SKIP: { - skip "Analysis module not present", 3 if $SKIP_STEMMA; + skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' ); my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' ); is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" ); is( $tradition->stemmata, 1, "Tradition now has the stemma" ); diff --git a/base/t/text_tradition_directory.t b/base/t/text_tradition_directory.t index 1eaa598..4ec2c17 100644 --- a/base/t/text_tradition_directory.t +++ b/base/t/text_tradition_directory.t @@ -24,8 +24,7 @@ my $t = Text::Tradition->new( 'input' => 'Tabular', 'file' => 't/data/simple.txt', ); -my $stemma_enabled; -eval { $stemma_enabled = $t->enable_stemmata; }; +my $stemma_enabled = $t->can( 'add_stemma' ); { my $d = Text::Tradition::Directory->new( 'dsn' => $dsn, diff --git a/base/t/text_tradition_parser_self.t b/base/t/text_tradition_parser_self.t index 337c520..f53b16f 100644 --- a/base/t/text_tradition_parser_self.t +++ b/base/t/text_tradition_parser_self.t @@ -35,12 +35,7 @@ if( $t ) { # TODO add a relationship, add a stemma, write graphml, reparse it, check that # the new data is there $t->language('Greek'); -my $stemma_enabled; -try { - $stemma_enabled = $t->enable_stemmata; -} catch { - ok( 1, "Skipping stemma tests without Analysis module" ); -} +my $stemma_enabled = $t->can('add_stemma'); if( $stemma_enabled ) { $t->add_stemma( 'dotfile' => 't/data/florilegium.dot' ); }