[pve-devel] r6482 - in pve-common/trunk/data: . PVE

svn-commits at proxmox.com svn-commits at proxmox.com
Wed Aug 17 11:39:59 CEST 2011


Author: dietmar
Date: 2011-08-17 11:39:59 +0200 (Wed, 17 Aug 2011)
New Revision: 6482

Added:
   pve-common/trunk/data/PVE/PodParser.pm
Modified:
   pve-common/trunk/data/ChangeLog
   pve-common/trunk/data/Makefile
   pve-common/trunk/data/PVE/CLIHandler.pm
   pve-common/trunk/data/PVE/RESTHandler.pm
Log:
split out pod generation code 


Modified: pve-common/trunk/data/ChangeLog
===================================================================
--- pve-common/trunk/data/ChangeLog	2011-08-16 05:10:03 UTC (rev 6481)
+++ pve-common/trunk/data/ChangeLog	2011-08-17 09:39:59 UTC (rev 6482)
@@ -1,3 +1,7 @@
+2011-08-17  Proxmox Support Team  <support at proxmox.com>
+
+	* PVE/PodParser.pm: split out pod generation code 
+
 2011-08-16  Proxmox Support Team  <support at proxmox.com>
 
 	* PVE/JSONSchema.pm (dump_config): a simply way to generate

Modified: pve-common/trunk/data/Makefile
===================================================================
--- pve-common/trunk/data/Makefile	2011-08-16 05:10:03 UTC (rev 6481)
+++ pve-common/trunk/data/Makefile	2011-08-17 09:39:59 UTC (rev 6482)
@@ -8,6 +8,7 @@
 
 LIB_SOURCES=			\
 	ProcFSTools.pm		\
+	PodParser.pm		\
 	CLIHandler.pm		\
 	RESTHandler.pm		\
 	JSONSchema.pm		\

Modified: pve-common/trunk/data/PVE/CLIHandler.pm
===================================================================
--- pve-common/trunk/data/PVE/CLIHandler.pm	2011-08-16 05:10:03 UTC (rev 6481)
+++ pve-common/trunk/data/PVE/CLIHandler.pm	2011-08-17 09:39:59 UTC (rev 6482)
@@ -1,19 +1,3 @@
-package PVE::PodParser;
-
-use Pod::Parser;
-
- at ISA = qw(Pod::Parser);
-
-sub command { 
-    my ($self, $cmd, $text, $line_num, $pod_para)  = @_;
-
-    $self->textblock($pod_para->raw_text(), $line_num, $pod_para);
-
-    if ($cmd eq 'head1' && $text =~ m/^\s*SYNOPSIS\s/ && $self->{synopsis}) {
-	$self->textblock($self->{synopsis}, $line_num, $pod_para);
-    }
-}
-
 package PVE::CLIHandler;
 
 use strict;
@@ -21,6 +5,7 @@
 
 use PVE::Exception qw(raise raise_param_exc);
 use PVE::RESTHandler;
+use PVE::PodParser;
 
 use base qw(PVE::RESTHandler);
 
@@ -125,13 +110,15 @@
 	$str =~ s/^USAGE: //;
 
 	$synopsis .= "\n" if $oldclass && $oldclass ne $class;
-	$synopsis .= " $str\n";
+	$str =~ s/\n/\n /g;
+	$synopsis .= " $str\n\n";
 	$oldclass = $class;
     }
 
     $synopsis .= "\n";
 
-    my $parser = PVE::PodParser->new(synopsis => $synopsis);
+    my $parser = PVE::PodParser->new();
+    $parser->{include}->{synopsis} = $synopsis;
     $parser->parse_from_file($podfn);
 }
 

Added: pve-common/trunk/data/PVE/PodParser.pm
===================================================================
--- pve-common/trunk/data/PVE/PodParser.pm	                        (rev 0)
+++ pve-common/trunk/data/PVE/PodParser.pm	2011-08-17 09:39:59 UTC (rev 6482)
@@ -0,0 +1,105 @@
+package PVE::PodParser;
+
+use strict;
+use Pod::Parser;
+use base qw(Pod::Parser);
+
+my $stdinclude = {
+    pve_copyright => <<EODATA,
+\=head1 COPYRIGHT AND DISCLAIMER
+
+Copyright (C) 2007-2011 Proxmox Server Solutions GmbH
+
+This program is free software: you can redistribute it and\/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
+
+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 Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see L<http://www.gnu.org/licenses/>.
+EODATA
+};
+
+sub command { 
+    my ($self, $cmd, $text, $line_num, $pod_para)  = @_;
+
+    if (($cmd eq 'include' && $text =~ m/^\s*(\S+)\s/)) {
+	my $incl = $1;
+	my $data = $stdinclude->{$incl} ? $stdinclude->{$incl} :
+	    $self->{include}->{$incl};
+	chomp $data;
+	$self->textblock("$data\n\n", $line_num, $pod_para);
+    } else {
+	$self->textblock($pod_para->raw_text(), $line_num, $pod_para);
+    }
+}
+
+# helpers used to generate our manual pages
+
+sub schema_get_type_text {
+    my ($phash) = @_;
+
+    if ($phash->{typetext}) {
+	return $phash->{typetext};
+    } elsif ($phash->{enum}) {
+	return "(" . join(' | ', sort @{$phash->{enum}}) . ")";
+    } elsif ($phash->{pattern}) {
+	return $phash->{pattern};
+    } elsif ($phash->{type} eq 'integer' || $phash->{type} eq 'number') {
+	if (defined($phash->{minimum}) && defined($phash->{maximum})) {
+	    return "$phash->{type} ($phash->{minimum} - $phash->{maximum})";
+	} elsif (defined($phash->{minimum})) {
+	    return "$phash->{type} ($phash->{minimum} - N)";
+	} elsif (defined($phash->{maximum})) {
+	    return "$phash->{type} (-N - $phash->{maximum})";
+	}
+    }
+
+    my $type = $phash->{type} || 'string';
+
+    return $type;
+}
+
+# generta epop from JSON schema properties
+sub dump_properties {
+    my ($properties) = @_;
+
+    my $data = "=over 1\n\n";
+
+    my $idx_param = {}; # -vlan\d+ -scsi\d+
+
+    foreach my $key (sort keys %$properties) {
+	my $d = $properties->{$key};
+	my $base = $key;
+	if ($key =~ m/^([a-z]+)(\d+)$/) {
+	    my $name = $1;
+	    next if $idx_param->{$name};
+	    $idx_param->{$name} = 1;
+	    $base = "${name}[n]";
+	}
+
+	my $descr = $d->{description} || 'No description avalable.';
+	chomp $descr;
+
+	if (defined(my $dv = $d->{default})) {
+	    my $multi = $descr =~ m/\n\n/; # multi paragraph ?
+	    $descr .= $multi ? "\n\n" : " ";
+	    $descr .= "Default value is '$dv'.";
+	}
+
+	my $typetext = schema_get_type_text($d);
+	$data .= "=item $base: $typetext\n\n";
+	$data .= "$descr\n\n";
+    }
+
+    $data .= "=back";
+
+    return $data;
+}
+
+1;

Modified: pve-common/trunk/data/PVE/RESTHandler.pm
===================================================================
--- pve-common/trunk/data/PVE/RESTHandler.pm	2011-08-16 05:10:03 UTC (rev 6481)
+++ pve-common/trunk/data/PVE/RESTHandler.pm	2011-08-17 09:39:59 UTC (rev 6482)
@@ -7,6 +7,7 @@
 use PVE::SafeSyslog;
 use PVE::Exception qw(raise raise_param_exc);
 use PVE::JSONSchema;
+use PVE::PodParser;
 use HTTP::Status qw(:constants :is status_message);
 use Text::Wrap;
 use Storable qw(dclone);
@@ -350,7 +351,6 @@
 #   'short'    ... command line only (one line)
 #   'full'     ... also include description
 # $hidepw      ... hide password option (use this if you provide a read passwork callback)
-
 sub usage_str {
     my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw) = @_;
 
@@ -373,43 +373,17 @@
 	$args .= $prop->{$p} && $prop->{$p}->{optional} ? "[<$p>]" : "<$p>";
     }
 
-    my $get_type_text = sub {
-	my ($k) = @_;
-
-	my $phash = $prop->{$k};
-
-	if ($phash->{typetext}) {
-	    return $phash->{typetext};
-	} elsif ($phash->{enum}) {
-	    return "(" . join(' | ', sort @{$phash->{enum}}) . ")";
-	} elsif ($phash->{pattern}) {
-	    return $phash->{pattern};
-	} elsif ($phash->{type} eq 'integer' || $phash->{type} eq 'number') {
-	    if (defined($phash->{minimum}) && defined($phash->{maximum})) {
-		return "$phash->{type} ($phash->{minimum} - $phash->{maximum})";
-	    } elsif (defined($phash->{minimum})) {
-		return "$phash->{type} ($phash->{minimum} - N)";
-	    } elsif (defined($phash->{maximum})) {
-		return "$phash->{type} (-N - $phash->{maximum})";
-	    }
-	}
-
-	my $type = $phash->{type} || 'string';
-
-	return $type;
-    };
-
     my $get_prop_descr = sub {
 	my ($k, $display_name) = @_;
  
 	my $phash = $prop->{$k};
 
 	my $res = '';
-
+	
 	my $descr = $phash->{description} || "no description available";
 	chomp $descr;
 
-	my $type =  &$get_type_text($k);
+	my $type = PVE::PodParser::schema_get_type_text($phash);
 
 	if ($hidepw && $k eq 'password') {
 	    $type = '';




More information about the pve-devel mailing list