/usr/share/perl5/Catmandu.pm is in libcatmandu-perl 1.0700-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 | package Catmandu;
use Catmandu::Sane;
our $VERSION = '1.07';
use Catmandu::Env;
use Catmandu::Util qw(:is);
use File::Spec;
use namespace::clean;
use Sub::Exporter::Util qw(curry_method);
use Sub::Exporter -setup => {
exports => [
config => curry_method,
log => curry_method,
store => curry_method,
fixer => curry_method,
importer => curry_method,
exporter => curry_method,
export => curry_method,
export_to_string => curry_method,
import_from_string => curry_method
],
collectors => {'-load' => \'_import_load', ':load' => \'_import_load',},
};
sub _import_load {
my ($self, $value, $data) = @_;
if (is_array_ref $value) {
$self->load(@$value);
}
else {
$self->load;
}
1;
}
sub _env {
my ($class, $env) = @_;
state $loaded_env;
$loaded_env = $env if defined $env;
$loaded_env
||= Catmandu::Env->new(load_paths => $class->default_load_path);
}
sub log {$_[0]->_env->log}
sub default_load_path { # TODO move to Catmandu::Env
my ($class, $path) = @_;
state $default_path;
$default_path = $path if defined $path;
$default_path //= do {
my $script = File::Spec->rel2abs($0);
my ($script_vol, $script_path, $script_name)
= File::Spec->splitpath($script);
my @dirs = grep length, File::Spec->splitdir($script_path);
if ($dirs[-1] eq 'bin') {
pop @dirs;
File::Spec->catdir(File::Spec->rootdir, @dirs);
}
else {
$script_path;
}
};
}
sub load {
my $class = shift;
my $paths = [@_ ? @_ : $class->default_load_path];
my $env = Catmandu::Env->new(load_paths => $paths);
$class->_env($env);
$class;
}
sub roots {
$_[0]->_env->roots;
}
sub root {
$_[0]->_env->root;
}
sub config {
my ($class, $config) = @_;
if ($config) {
my $env = Catmandu::Env->new(load_paths => $class->_env->load_paths);
$env->_set_config($config);
$class->_env($env);
}
$class->_env->config;
}
sub default_store {$_[0]->_env->default_store}
sub store {
my $class = shift;
$class->_env->store(@_);
}
sub default_fixer {$_[0]->_env->default_fixer}
sub fixer {
my $class = shift;
$class->_env->fixer(@_);
}
sub default_importer {$_[0]->_env->default_importer}
sub default_importer_package {$_[0]->_env->default_importer_package}
sub importer {
my $class = shift;
$class->_env->importer(@_);
}
sub default_exporter {$_[0]->_env->default_exporter}
sub default_exporter_package {$_[0]->_env->default_exporter_package}
sub exporter {
my $class = shift;
$class->_env->exporter(@_);
}
sub export {
my $class = shift;
my $data = shift;
my $exporter = $class->_env->exporter(@_);
is_hash_ref($data) ? $exporter->add($data) : $exporter->add_many($data);
$exporter->commit;
return;
}
sub export_to_string {
my $class = shift;
my $data = shift;
my $name = shift;
my %opts = ref $_[0] ? %{$_[0]} : @_;
my $str = "";
my $exporter = $class->_env->exporter($name, %opts, file => \$str);
is_hash_ref($data) ? $exporter->add($data) : $exporter->add_many($data);
$exporter->commit;
$str;
}
sub import_from_string {
my $class = shift;
my $str = shift;
my $name = shift;
my %opts = ref $_[0] ? %{$_[0]} : @_;
$class->_env->importer($name, %opts, file => \$str)->to_array();
}
sub define_importer {
my $class = shift;
my $name = shift;
my $package = shift;
my $options = ref $_[0] ? $_[0] : {@_};
$class->config->{importer}{$name}
= {package => $package, options => $options};
}
sub define_exporter {
my $class = shift;
my $name = shift;
my $package = shift;
my $options = ref $_[0] ? $_[0] : {@_};
$class->config->{exporter}{$name}
= {package => $package, options => $options};
}
sub define_store {
my $class = shift;
my $name = shift;
my $package = shift;
my $options = ref $_[0] ? $_[0] : {@_};
$class->config->{store}{$name}
= {package => $package, options => $options};
}
sub define_fixer {
my $class = shift;
my $name = shift;
my $fixes = ref $_[0] ? $_[0] : [@_];
$class->config->{fixer}{$name} = $fixes;
}
1;
__END__
=pod
=head1 NAME
Catmandu - a data toolkit
=head1 SYNOPSIS
# From the command line
# Convert data from one format to another
$ catmandu convert JSON to CSV < data.json
$ catmandu convert CSV to YAML < data.csv
$ catmandu convert MARC to YAML < data.mrc
# Fix data, add, delete, change fields
$ catmandu convert JSON --fix 'move_field(title,my_title)' < data.json
$ catmandu convert JSON --fix all_my_fixes.txt < data.json
# Use a moustache preprocessor on the fix script
$ catmandu convert JSON --fix all_my_fixes.txt --var opt1=foo --var opt2=bar < data.json
# Import data into a database
# Requires: Catmandu::MongoDB and Catmandu::ElasticSearch
$ catmandu import YAML to MongoDB --database_name bibliography < data.yml
$ catmandu import CSV to ElasticSearch --index_name mystuff < data.csv
# Export data from a database
# Requires: Catmandu::MongoDB and Catmandu::ElasticSearch
$ catmandu export MongoDB --database_name bibliography to YAML > data.yml
$ catmandu export ElasticSearch --index_name mystuff to CSV > data.csv
# Copy data from one store to another
$ catmandu copy MongoDB --database_name mydb to ElasticSearch --index_name mydb
# Show the contents of catmandu.yml
$ catmandu config
# Count items in a store
$ catmandu count test1
# Delete items from store
$ catmandu delete test1 --query 'title:"My Rabbit"'
# run a fix script
$ catmandu run myfixes.fix
# or, create an executable fix script
$ cat myfixes.fix
#!/usr/local/bin/catmandu run
do importer(OAI,url:"http://biblio.ugent.be/oai")
retain(_id)
end
$ chmod 755 myfixes.fix
$ ./myfixes.fix
# From Perl
use Catmandu;
# If you have Catmandu::OAI and Catmandu::MongoDB installed
my $importer = Catmandu->importer('OAI',url => 'https://biblio.ugent.be/oai')
my $store = Catmandu->store('MongoDB',database_name => 'test');
# Import all the OAI records into MongoDB
$store->add_many($importer);
# Export all the MongoDB records to YAML and apply some fixes
# myfixes.txt:
# upcase(title.*)
# remove_field(_metadata)
# join_field(creator,'; ')
# join_field(subject,'-- ')
my $fixer = Catmandu->fixer('myfixes.txt');
my $exporter = Catmandu->exporter('YAML');
$exporter->add_many( $fixer->fix($store) );
$exporter->commit;
=head1 DESCRIPTION
Catmandu provides a command line client and a Perl API to ease the export (E)
transformation (T) and loading (L) of data into databases or data file, ETL in short.
Most of the daily work processing structured data can be done on the command line
executing the C<catmandu> command. With our catmandu command ETL processing is available
in a Perl context. Catmandu is different from other
ETL tools by its focus on command line processing with much support for dataformats
available in (academic) libraries: MARC, MODS, OAI and SRU. But, also generic formats such
as JSON, YAML, CVS, Excel, XML, RDF, Atom are supported.
Read :
=over
=item * L<Catmandu::Introduction> for a primer on the command line capabilities of Catmandu.
=item * L<Catmandu::Importer> for the basics of importing
=item * L<Catmandu::Fix> for the basics of transformations
=item * L<Catmandu::Exporter> for the basics of exporting
=item * L<Catmandu::Store> for the basics of storing information
=item * Or, visit our website at L<http://librecat.org/> and our blog L<https://librecatproject.wordpress.com/>
for many tutorials
=back
The documentation below describes the methods available when including Catmandu as
part of a Perl script. For an overview of the command line tool itself read the
documentation on L<catmandu>.
=head1 USE
To include Catmandu in a Perl script it should be loaded with a C<use> command:
use Catmandu;
By default no methods are imported into the Perl context. To import all or some Catmandu methods,
provide them as a list to the C<use> command:
use Catmandu -all;
use Catmandu qw(config store exporter);
Catmandu can load configuration options for exports, importers, fixers via configuration
files (see the CONFIG section below). When adding the --load option (optionally with a path) to the
C<use> command, these configuration files will be loaded at the start of your script.
use Catmandu -load;
use Catmandu --load => ['/my/config/directory'];
# or use all the options
use Catmandu -all -load => [qw(/config/path' '/another/config/path)];
=head1 CLASS METHODS
=head2 log
Return the current L<Log::Any> logger.
use Catmandu;
use Log::Any::Adapter;
use Log::Log4perl;
Log::Any::Adapter->set('Log4perl'); # requires Log::Any::Adapter::Log4perl
Log::Log4perl::init('./log4perl.conf');
my $logger = Catmandu->log;
$logger->info("Starting main program");
with log4perl.conf like:
# Send a copy of all logging messages to STDERR
log4perl.rootLogger=DEBUG,STDERR
# Logging specific for your main program
log4perl.category.myprog=INFO,STDERR
# Logging specific for on part of Catmandu
log4perl.category.Catmandu::Fix=DEBUG,STDERR
# Where to send the STDERR output
log4perl.appender.STDERR=Log::Log4perl::Appender::Screen
log4perl.appender.STDERR.stderr=1
log4perl.appender.STDERR.utf8=1
log4perl.appender.STDERR.layout=PatternLayout
log4perl.appender.STDERR.layout.ConversionPattern=%d [%P] - %p %l time=%r : %m%n
=head2 default_load_path(['/default/path'])
Returns the default location where L<Catmandu> looks for configuration and lib
when called with no argument. Sets the default location if a path is given.
The default load path is the script directory or it's parent if the script
directory is C<bin>.
=head2 load
Load all the configuration options in the catmandu.yml configuration file.
See CONFIG below for extended examples of configuration options.
=head2 load('/path', '/another/path')
Load all the configuration options stored at alternative paths.
A load path C<':up'> will search upwards from your program for configuration.
See CONFIG below for extended examples of configuration options.
=head2 roots
Returns an ARRAYREF of paths where configuration was found. Note that this list
is empty before C<load>.
=head2 root
Returns the first path where configuration was found. Note that this is
C<undef> before C<load>.
=head2 config
Returns the current configuration as a HASHREF.
=head2 config($config)
Set a new configuration and reload the environment.
=head2 default_store
Return the name of the default store.
=head2 store([NAME])
Return an instance of L<Catmandu::Store>. The NAME is a name of a L<Catmandu::Store> or the
name of a store configured in a catmandu.yml configuration file. When no NAME is given, the
'default' store in the configuration file will be used.
E.g. if the configuration file 'catmandu.yml' contains:
store:
default:
package: ElasticSearch
options:
index_name: blog
test:
package: Mock
then in your program:
# This will use ElasticSearch
my $store = Catmandu->store('ElasticSearch', index_name => 'blog');
# or because we have a 'default' set in the configuration file
my $store = Catmandu->store('default');
# or because 'default' will be used when no name was provided
my $store = Catmandu->store;
# This will use Mock
my $store = Catmandu->store('test');
Configuration settings can be overwritten by the store command:
my $store2 = Catmandu->store('default', index_name => 'test2');
=head2 default_fixer
Return the name of the default fixer.
=head2 fixer(NAME)
=head2 fixer(FIX,FIX)
=head2 fixer([FIX])
Return an instance of L<Catmandu::Fix>. NAME can be the name of a fixer section
in a catmandu.yml file. Or, one or more L<Catmandu::Fix>-es can be provided inline.
E.g. if the configuration file 'catmandu.yml' contains:
fixer:
default:
- do_this()
- do_that()
then in your program al these lines below will create the same fixer:
my $fixer = Catmandu->fixer('do_this()', 'do_that()');
my $fixer = Catmandu->fixer(['do_this()', 'do_that()']);
my $fixer = Catmandu->fixer('default');
my $fixer = Catmandu->fixer(); # The default name is 'default'
FIX-es can be also written to a Fix script. E.g. if myfixes.txt contains:
do_this()
do_that()
then the above code will even be equivalent to:
my $fixer = Catmandu->fixer('myfixes.txt');
=head2 default_importer
Return the name of the default importer.
=head2 default_importer_package
Return the name of the default importer package if no
package name is given in the config or as a param.
=head2 importer(NAME)
Return an instance of L<Catmandu::Importer>. The NAME is a name of a L<Catmandu::Importer> or the
name of a importer configured in a catmandu.yml configuration file. When no NAME is given, the
'default' importer in the configuration file will be used.
E.g. if the configuration file 'catmandu.yml' contains:
importer:
default:
package: OAI
options:
url: http://www.instute.org/oai/
then in your program all these lines will be equivalent:
my $importer = Catmandu->importer('OAI', url => 'http://www.instute.org/oai/');
my $importer = Catmandu->importer('default');
my $importer = Catmandu->importer(); # The default name is 'default'
Configuration settings can be overwritten by the importer command:
my $importer2 = Catmandu->importer('default', url => 'http://other.institute.org');
=head2 default_exporter
Return the name of the default exporter.
=head2 default_exporter_package
Return the name of the default exporter package if no
package name is given in the config or as a param.
=head2 exporter([NAME])
Return an instance of L<Catmandu::Exporter> with name NAME (or the default when
no name is given). The NAME is set in the configuration file (see 'importer').
=head2 export($data,[NAME])
Export data using a default or named exporter or exporter instance.
Catmandu->export({ foo=>'bar'});
my $importer = Catmandu::Importer::Mock->new;
Catmandu->export($importer, 'YAML', file => '/my/file');
Catmandu->export($importer, 'my_exporter');
Catmandu->export($importer, 'my_exporter', exporter_option => '...' , ...);
Catmantu->export($importer, Catmandu::Exporter::YAML->new);
=head2 export_to_string
Export data using a default or named exporter to a string.
my $importer = Catmandu::Importer::Mock->new;
my $yaml = Catmandu->export_to_string($importer, 'YAML');
# is the same as
my $yaml = "";
Catmandu->export($importer, 'YAML', file => \$yaml);
=head2 import_from_string
Import data from a string using a default or named importer.
Return value should be an array of hashes.
my $json = qq([{"name":"Nicolas"}]);
{
my $record = Catmandu->import_from_string( $json, "JSON" );
}
# is the same as
{
my $record = Catmandu->importer('JSON', file => \$json)->to_array()
}
=head2 define_importer
Configure a new named importer.
Catmandu->define_importer(books => CSV => (fields => 'title,author,publisher'));
Catmandu->importer(books => (file => 'mybooks.csv'))->each(sub {
my $book = shift;
say $book->{title};
});
# this is equivalent to
Catmandu->config->{importer}{books} = {
package => 'CSV',
options => {
fields => 'title,author,publisher',
},
}
=head2 define_exporter
Configure a new named exporter.
Catmandu->define_exporter('books', 'CSV', fix => 'capitalize(title)');
my $csv = Catmandu->export_to_string({title => 'nexus'}, 'books');
# this is equivalent to
Catmandu->config->{exporter}{books} = {
package => 'CSV',
options => {
fix => 'capitalize(title)',
},
}
=head2 define_store
Configure a new named store.
Catmandu->define_store(mydb => MongoDB => (database_name => 'mydb'));
Catmandu->store->bag('books')->get(1234);
# this is equivalent to
Catmandu->config->{store}{mydb} = {
package => 'MongoDB',
options => {
database_name => 'mydb',
},
}
=head2 define_fixer
Configure a new named fixer.
Catmandu->define_fixer('cleanup', [
'trim(title)',
'capitalize(title)',
'remove_field(junk)',
# ...
]);
Catmandu->fixer('cleanup')->fix($record);
=head1 EXPORTS
=over
=item config
Same as C<< Catmandu->config >>.
=item store
Same as C<< Catmandu->store >>.
=item importer
Same as C<< Catmandu->importer >>.
=item exporter
Same as C<< Catmandu->exporter >>.
=item export
Same as C<< Catmandu->export >>.
=item export_to_string
Same as C<< Catmandu->export_to_string >>.
=item import_from_string
Same as C<< Catmandu->import_from_string >>.
=item fixer
Same as C<< Catmandu->fixer >>.
=item log
Same as C<< Catmandu->log >>.
=item -all/:all
Import everything.
=item -load/:load
use Catmandu -load;
use Catmandu -load => [];
# is the same as
Catmandu->load;
use Catmandu -load => ['/config/path'];
# is the same as
Catmandu->load('/config/path');
=back
=head1 CONFIG
Catmandu configuration options can be stored in files in the root directory of
your programming project. The file can be YAML, JSON or Perl and is called
C<catmandu.yml>, C<catmandu.json> or C<catmandu.pl>. In this file you can set
the default Catmandu stores and exporters to be used. Here is an example of a
C<catmandu.yml> file:
store:
default:
package: ElasticSearch
options:
index_name: myrepository
exporter:
default:
package: YAML
=head2 Split config
For large configs it's more convenient to split the config into several files.
You can do so by having multiple config files starting with catmandu*.
catmandu.general.yml
catmandu.db.yml
...
Split config files are processed and merged by L<Config::Onion>.
=head2 Deeply nested config structures
Config files can indicate a path under which their keys will be nested. This
makes your configuration more readable by keeping indentation to a minimum.
A config file containing
_prefix:
foo:
bar:
baz: 1
will be loaded as
foo:
bar:
baz: 1
See L<Config::Onion> for more information on how this works.
=head1 SEE ALSO
=over 4
=item documentation
L<http://librecat.org/Catmandu/>
=item command line client
L<catmandu>
=item core modules
L<Catmandu::Importer>
L<Catmandu::Exporter>,
L<Catmandu::Store>,
L<Catmandu::Fix>,
L<Catmandu::Iterable>
=item extended features
L<Catmandu::Validator>
=back
=head1 AUTHOR
Nicolas Steenlant, C<< <nicolas.steenlant at ugent.be> >>
=head1 CONTRIBUTORS
Magnus Enger, C<< magnus at enger.priv.no >>
Nicolas Franck, C<< nicolas.franck at ugent.be >>
Patrick Hochstenbach, C<< patrick.hochstenbach at ugent.be >>
Vitali Peil, C<< vitali.peil at uni-bielefeld.de >>
Christian Pietsch, C<< christian.pietsch at uni-bielefeld.de >>
Dave Sherohman, C<< dave.sherohman at ub.lu.se >>
Jakob Voss, C<< nichtich at cpan.org >>
Snorri Briem, C<< snorri.briem at ub.lu.se >>
Johann Rolschewski, C<< jorol at cpan.org >>
Pieter De Praetere, C<< pieter.de.praetere at helptux.be >>
Doug Bell
Upsana, C<< me at upasana.me >>
Stefan Weil
Tom Hukins
=head1 QUESTIONS, ISSUES & BUG REPORTS
For any questions on the use of our modules please join our mailing list at:
librecat-dev@lists.uni-bielefeld.de
or send in your bug reports or feature requests to our issue tracker at:
https://github.com/LibreCat/Catmandu/issues
=head1 LICENSE AND COPYRIGHT
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See L<http://dev.perl.org/licenses/> for more information.
=cut
|