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

svn-commits at proxmox.com svn-commits at proxmox.com
Mon Aug 15 11:50:14 CEST 2011


Author: dietmar
Date: 2011-08-15 11:50:14 +0200 (Mon, 15 Aug 2011)
New Revision: 6479

Modified:
   pve-common/trunk/data/ChangeLog
   pve-common/trunk/data/PVE/JSONSchema.pm
Log:
	* PVE/JSONSchema.pm (parse_config): a simply way to verify
	key/value configuration files.



Modified: pve-common/trunk/data/ChangeLog
===================================================================
--- pve-common/trunk/data/ChangeLog	2011-08-15 08:36:08 UTC (rev 6478)
+++ pve-common/trunk/data/ChangeLog	2011-08-15 09:50:14 UTC (rev 6479)
@@ -1,3 +1,8 @@
+2011-08-15  Proxmox Support Team  <support at proxmox.com>
+
+	* PVE/JSONSchema.pm (parse_config): a simply way to verify
+	key/value configuration files.
+
 2011-08-11  Proxmox Support Team  <support at proxmox.com>
 
 	* PVE/*: remove useless 'fixme' comments.

Modified: pve-common/trunk/data/PVE/JSONSchema.pm
===================================================================
--- pve-common/trunk/data/PVE/JSONSchema.pm	2011-08-15 08:36:08 UTC (rev 6478)
+++ pve-common/trunk/data/PVE/JSONSchema.pm	2011-08-15 09:50:14 UTC (rev 6479)
@@ -909,4 +909,47 @@
     return $opts;
 }
 
+# A way to parse configuration data by giving a json schema
+sub parse_config {
+    my ($schema, $filename, $raw) = @_;
+
+    # do fast check (avoid validate_schema($schema))
+    die "got strange schema" if !$schema->{type} || 
+	!$schema->{properties} || $schema->{type} ne 'object';
+
+    my $cfg = {};
+
+    while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
+	my $line = $1;
+ 
+	next if $line =~ m/^\#/; # skip comment lines
+	next if $line =~ m/^\s*$/; # skip empty lines
+
+	if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
+	    my $key = $1;
+	    my $value = $2;
+	    if ($schema->{properties}->{$key} && 
+		$schema->{properties}->{$key}->{type} eq 'boolean') {
+
+		$value = 1 if $value =~ m/^(1|on|yes|true)$/i; 
+	        $value = 0 if $value =~ m/^(0|off|no|false)$/i; 
+	    }
+	    $cfg->{$key} = $value;
+	} else {
+	    warn "ignore config line: $line\n"
+	}
+    }
+
+    my $errors = {};
+    check_prop($cfg, $schema, '', $errors);
+
+    foreach my $k (keys %$errors) {
+	warn "parse error in '$filename' - '$k': $errors->{$k}\n";
+	delete $cfg->{$k};
+    } 
+
+    return $cfg;
+}
+
+
 1;



More information about the pve-devel mailing list