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
|
# Copyright (c) 1997-2020
# Ewgenij Gawrilow, Michael Joswig, and the polymake team
# Technische Universität Berlin, Germany
# https://polymake.org
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version: http://www.gnu.org/licenses/gpl.txt.
#
# This program 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 General Public License for more details.
#-------------------------------------------------------------------------------
#
# This file is part of the polymake database interface polyDB.
#
# @author Silke Horn, Andreas Paffenholz
# http://www.mathematik.tu-darmstadt.de/~paffenholz
#
CREDIT polyDB
package PolyDB::Client;
use Term::ANSIColor;
use Term::ReadKey;
use Text::Wrap;
# @category Database Access
# Get a list of all collection names
# @param String section only return names of collections from the given section
# @option String filter regular expression for more complex filtering of collection names
# Think of correct escaping of special characters: \\. for a literal dot, \\w for a name character.
user_method list_collections(;$ { filter => undef }) {
$_[2]->{recursive} = -1;
&get_collection_names;
}
# @category Database Access
# Get a list of section names
# @param String section return names of sub-sections of the given section;
# by default, names of top-level sections are returned
# @option Bool recursive return names of sub-sections on all levels
# @option String filter regular expression for more complex filtering of section names
# Think of correct escaping of special characters: \\. for a literal dot, \\w for a name character.
user_method list_sections(;$ { filter => undef, recursive => false }) {
&get_collection_names;
}
# @category Database Administration
# Common options for writing section doc
options %doc_options = (
# String section section name
section => $PolyDB::default::db_section_name,
# Bool update default false
update => false,
# Bool verbose default false
verbose => false,
# Bool replace default false
replace => false
);
# @category Database Administration
# Set documentation for a section
# @param HASH doc the documentation
# @options %doc_options
user_method set_section_doc($; %doc_options) {
my ($self, $doc, $options) = @_;
my $version = $doc->{polydb_version} // $PolyDB::default::db_polydb_version;
$doc->{"_id"} = $options->{section}.".$version";
$doc->{section} = split /\./, $options->{section};
$doc->{sectionDepth} = scalar($doc->{section});
my $db = $self->SUPER::get_database($default::db_name);
my $col = $db->get_collection("_sectionInfo.".$options->{section});
if ( $options->{replace} ) { $options->{update} = true; }
my $output = $col->find_one({'_id' => $doc->{id}});
die "documentation already set but neither option update nor replace given\n" if ( $output && !$options->{update} );
if ( !$output ) { $options->{update} = false; }
if ( !$options->{update} ) {
$output = $col->insert_one($doc);
} else {
if ( $options->{replace} ) {
$output = $col->replace_one({'_id' => $doc->{id}}, $doc);
} else {
$output = $col->update_one({'_id' => $doc->{id}}, {'$set' => $doc});
}
}
if ($options->{verbose}) {
if ($output->acknowledged) {
print "successfully set documentation for $options->{section}\n"
} else {
print "an error occurred when trying to set the documentation for $options->{section}:\n$output\n";
}
}
}
# @category Database Access
# Print information about available databases and collections.
# @option String section name of the database, default: all available databases
# @option String collection name of the collection, default: all available collections
# @option Int info_level 0: only names,
# 1: short description (default if no collection is given),
# 2: description,
# 3: description, authors, maintainers,
# 4: full info
# @option Bool colored
user_method info(; {section => undef, collection => undef, info_level => undef, colored => true}) {
my ($self, $options) = @_;
$options->{info_level} = defined($options->{collection}) ? 5 : 1;
if ( !defined($options->{section}) && defined($options->{collection}) ) {
($options->{section}, $options->{collection}) = $options->{collection} =~ /([\w.]+)\.([\w]+)/;
}
print "===============\navailable polydb collections\n===============\n";
$self->print_sections_at_level($options->{section}, $options->{collection}, $self, $options->{info_level}, 0, $options->{colored});
}
sub print_formatted {
my ($indent, $text, $colored) = @_;
state $in_terminal = ( -t STDIN && $PolyDB::default::pretty_print_doc ) ? true : false;
if ( $in_terminal ) {
my $initial_tab = "\t"x$indent;
my $subsequent_tab = "\t"x$indent;
$subsequent_tab .= " ";
my ($w) = GetTerminalSize();
local($Text::Wrap::columns) = $w;
if ( $colored ) {
if ( $text =~ /SECTION/ ) {
print color($PolyDB::default::db_section_color);
} elsif ($text =~ /COLLECTION/) {
print color($PolyDB::default::db_collection_color);
} else {
print color('reset');
}
}
print wrap($initial_tab, $subsequent_tab, $text);
print color('reset');
} else {
print $text;
}
print "\n";
}
sub print_name_with_info {
my ($name) = @_;
my $ret = " ".$name->{name};
for my $tag (("affiliation", "email")) {
$ret .= ", ".$name->{$tag} if defined($name->{$tag});
}
$ret .= " (".$name->{remark}.")" if defined($name->{remark});
return $ret;
}
sub print_list_of_names {
my ($names, $title, $indent) = @_;
if ( ref($names) eq "ARRAY" ) {
print_formatted($indent, $title.": ");
foreach my $name (@{$names}) {
print_formatted($indent, print_name_with_info($name));
}
} else {
print_formatted($indent, $title.": ".$names);
}
}
sub print_references {
my ($res,$indent) = @_;
foreach my $ref (@$res) {
print_formatted($indent, "Cite:" );
if ( ref($ref) eq "HASH" ) {
print_formatted($indent+1, $ref->{authors}.", ".$ref->{title}.", ".$ref->{bib});
if ( defined($ref->{links}) ) {
print_formatted($indent+1, " obtain at:");
foreach my $link (@{$ref->{links}}) {
print_formatted($indent+2,$link->{type}.": ".$link->{link});
}
}
} else {
print_formatted($indent,$ref);
}
}
}
sub print_webpage {
my ($res,$indent) = @_;
foreach my $ref (@$res) {
print_formatted($indent,"Online Resources:");
if ( ref($ref) eq "HASH" ) {
print_formatted($indent+1,$ref->{description}.": ".$ref->{address});
} else {
print_formatted($indent+1,$ref);
}
}
}
sub print_collections_at_level {
my ($self,$sectionname, $collectionname, $idb, $info_level, $indent, $colored) = @_;
my $filter = "_collectionInfo.".$sectionname;
if ( $collectionname eq "" ) {
$filter .= '\.[\w]+$';
} else {
$filter .= "\\.".$collectionname;
}
my $db = $self->SUPER::get_database($default::db_name);
my @collections = map { $_->{name} }
$db->list_collections({ name => { '$regex' => $filter } }, { authorizedCollections => true, nameOnly => true })->all;
my $polydb_version = $PolyDB::default::db_polydb_version;
return if !scalar(@collections);
foreach my $colname (sort { lc($a) cmp lc($b) } @collections) {
my $id = $colname.".".$polydb_version;
$id =~ s/_collectionInfo.//;
my $icol = $db->get_collection($colname);
my $res = $icol->find_one({ "_id" => $id });
next if !defined($res);
print "\n";
print_formatted($indent,"COLLECTION: ".$res->{collection}, $colored);
next if $info_level < 1;
print_formatted($indent, $info_level == 1 ? $res->{short_description} : $res->{description}, $colored);
next if $info_level < 3;
print_list_of_names($res->{author}, "Author(s)", $indent) if defined($res->{author});
print_list_of_names($res->{contributor},"Contributor(s)",$indent) if defined($res->{contributor});
print_list_of_names($res->{maintainer}, "Maintainer(s)", $indent) if defined($res->{maintainer});
next if $info_level < 4;
print_references($res->{references}) if defined($res->{references});
print_webpage($res->{webpage}, $indent) if defined($res->{webpage});
}
}
sub print_sections_at_level {
my ($self, $sectionname, $collectionname, $idb, $info_level, $indent, $colored) = @_;
my $polydb_version = $PolyDB::default::db_polydb_version;
foreach my $sectionname (sort { lc($a) cmp lc($b) } $self->list_sections(filter=>$sectionname) ) {
my $id = "$sectionname.$polydb_version";
my $db = $self->SUPER::get_database($default::db_name);
my $icol = $db->get_collection("_sectionInfo.$sectionname");
my $res = $icol->find_one({ "_id" => $id });
print "\n";
print_formatted($indent, "SECTION: ".$sectionname, $colored);
if ( $info_level > 0 ) {
print_formatted($indent, $info_level == 1 ? $res->{short_description} : $res->{description}, $colored);
}
$self->print_sections_at_level($sectionname.".", $collectionname, $idb, $info_level,$indent+1, $colored);
$self->print_collections_at_level($sectionname, $collectionname, $idb, $info_level,$indent+1, $colored);
}
}
package PolyDB::Collection;
# @category Database Access
# Get a JSON validation schema for objects stored in the collection
# @return Schema
user_method get_schema() { &get_own_schema }
# @category Database Collection Administration
# Common options for writing collection doc
options %doc_options = (
# Bool update default false
update => false,
# Bool verbose default false
verbose => false,
# Bool replace default false
replace => false
);
# @category Database Collection Administration
# Set documentation for a collection
# @param HASH doc the documentation
# @options %doc_options
user_method set_collection_doc ($; \%doc_options) {
my ($self, $doc, $options) = @_;
my $collection = $self->{name};
my $version = $doc->{version} // $PolyDB::default::db_polydb_version;
my $id = "$collection.$version";
$doc->{"_id"} = $id;
my $col = $self->get_own_info_collection();
if ( $options->{replace} ) { $options->{update} = true; }
my $output = $col->find_one({'_id' => $id});
die "documentation already set but neither option update nor replace given\n" if ( $output && !$options->{update} );
if ( !$output ) { $options->{update} = false; }
if ( !$options->{update} ) {
$output = $col->insert_one($doc);
} else {
if ( $options->{replace} ) {
$output = $col->replace_one({'_id' => $id}, $doc);
} else {
$output = $col->update_one({'_id' => $id}, {'$set' => $doc});
}
}
if ($options->{verbose}) {
if ($output->acknowledged) {
print "successfully updated documentation for $collection in section $section\n"
} else {
print "an error occurred when trying to update the documentation for $collection in section $section:\n$output\n";
}
}
}
# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
|