begin imports

This commit is contained in:
a-Sansara 2018-08-02 09:38:05 +02:00
parent 133147205b
commit a191a385cf
10 changed files with 232 additions and 5 deletions

15
resources/config/db.yml Normal file
View File

@ -0,0 +1,15 @@
bo :
driver : mysql
host : db.local.dkr.pluie.org
dbname : local_bo_sub
user : dev
password : mysql
charset : utf8
therapy :
driver : mysql
host : db.local.dkr.pluie.org
dbname : local_therapy
user : dev
password : mysql
charset : utf8

View File

@ -0,0 +1,6 @@
map1:
sp1 : val1
sp2 : val2
sp3 : val3
map2:
sp4 : val4

26
resources/main.yml Normal file
View File

@ -0,0 +1,26 @@
project:
name : blabla
env : local
debug : 1
# | use special key word '^imports' to import other yaml config files in
# | current yaml document
# | imports must be uniq and a direct child of root node
# | imported files are injected as mapping nodes at top document level
# | so you cannot use keys that already exists in the document
^imports :
# you can redefine default import path with the special key '^path'
# if you do not use it, the default path value will be the current directory
# redefined path values are relative to the current directory (if a relative path
# is provided)
^path : ./config
# you can also define any other var by prefixing key with ~
^dir : subdir
# and use it enclosed by ^
# here final test path will be "./config/subdir/test.yml"
test : ^dir^/test.yml
# here final db path will be "./config/db.yml"
db : db.yml
bilbo : tato

66
samples/yaml-imports.vala Normal file
View File

@ -0,0 +1,66 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @software : lib-yaml <https://git.pluie.org/pluie/lib-yaml>
* @version : 0.3
* @date : 2018
* @licence : GPLv3.0 <http://www.gnu.org/licenses/>
* @author : a-Sansara <[dev]at[pluie]dot[org]>
* @copyright : pluie.org <http://www.pluie.org/>
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* This file is part of lib-yaml.
*
* lib-yaml is free software (free as in speech) : 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 3 of the License,
* or (at your option) any later version.
*
* lib-yaml 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.
*
* You should have received a copy of the GNU General Public License
* along with lib-yaml. If not, see <http://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
using GLib;
using Gee;
using Pluie;
int main (string[] args)
{
Echo.init(false);
var pwd = Environment.get_variable ("PWD");
//~ var path = Path.build_filename (pwd, "resources/main.yml");
var path = "./resources/main.yml";
var done = false;
of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara");
Pluie.Yaml.Scanner.DEBUG = true;
var config = new Yaml.Config (path);
var spath = "^imports";
var node = config.get (spath);
if ((done = node != null)) {
of.action ("retriew node from Yaml.Config", spath);
if ((node as Yaml.NodeMap).map.has_key ("^path")) {
of.echo ((node as Yaml.NodeMap).map["^path"].to_string (false));
}
of.echo (node.to_string (false));
}
of.rs (done);
of.echo ();
of.echo (pwd);
return (int) done;
}

View File

@ -114,6 +114,18 @@ public class Pluie.Yaml.BaseNode : Object, Pluie.Yaml.Node
this.anchor = id; this.anchor = id;
} }
/**
*
*/
public string? val ()
{
string v = null;
if (this.node_type.is_single_pair ()) {
v = (this as Yaml.NodeSinglePair).scalar ().data;
}
return v;
}
/** /**
* clone current node * clone current node
* @param the name of clone * @param the name of clone

View File

@ -3,20 +3,31 @@
*/ */
public class Pluie.Yaml.Config public class Pluie.Yaml.Config
{ {
const char IMPORTS_SPE = '^';
/** /**
* current path * current path
*/ */
public string? path { get; internal set; default = null; } public string? path { get; internal set; default = null; }
/** /**
* Yaml Loader * Yaml Loader
*/ */
public Yaml.Loader loader { internal get; internal set; } public Yaml.Loader loader { internal get; internal set; }
/** /**
* Yaml Finder * Yaml Finder
*/ */
public Yaml.Finder finder { internal get; internal set; } public Yaml.Finder finder { internal get; internal set; }
/**
* imports var
*/
Gee.HashMap<string, string> varmap { get; internal set; }
/**
* imports paths
*/
Gee.HashMap<string, string> paths { get; internal set; }
/** /**
* construct a Yaml Config for specifiyed path * construct a Yaml Config for specifiyed path
@ -26,8 +37,9 @@ public class Pluie.Yaml.Config
Yaml.BaseNode.mode = mode; Yaml.BaseNode.mode = mode;
this.path = path; this.path = path;
if (this.path != null) { if (this.path != null) {
this.loader = new Yaml.Loader (this.path); this.loader = new Yaml.Loader (this.path, true, true);
this.finder = new Yaml.Finder(this.loader.get_nodes ()); this.finder = new Yaml.Finder(this.loader.get_nodes ());
this.get_imports ();
} }
} }
@ -43,4 +55,88 @@ public class Pluie.Yaml.Config
return node; return node;
} }
/**
* find node matching specifiyed keyPath
*/
public void get_imports ()
{
var node = this.get("^imports") as Yaml.NodeMap;
if (node != null) {
this.get_imports_var(node);
var dir = this.strip_path(Path.get_dirname (this.path));
if (this.varmap.has_key ("path")) {
var p = this.strip_path(this.varmap["path"]);
if (p != null) {
dir = Path.is_absolute(p) ? p : Path.build_filename(dir, p);
}
}
of.keyval ("import path", dir);
this.update_var (node, dir);
}
}
/**
*
*/
private void update_var (Yaml.NodeMap node, string path)
{
this.varmap.set ("path", path);
string? file = null;
foreach (var entry in node.map.entries) {
if (entry.key[0] != IMPORTS_SPE) {
message (entry.key);
var val = entry.value.val ();
message ("%s = %s", entry.key, val);
if (!Path.is_absolute (val)) {
val = Path.build_filename(path, val);
message ("new relative %s", val);
}
message (" == update var == ");
foreach (var v in this.varmap.entries) {
if (v.key != "path") {
message ("-- var %s", v.key);
entry.value.data = val.replace ("^%s^".printf (v.key), v.value);
node.map[entry.key] = entry.value;
of.echo ("%s : %s".printf (entry.key, node.map[entry.key].val () ));
of.keyval (entry.key, entry.value.data);
//~ this.paths[entry.key] = entry.value.data;
}
}
}
}
foreach (var entry in this.paths.entries) {
of.keyval (entry.key, entry.value);
}
}
/**
*
*/
private void get_imports_var (Yaml.NodeMap node)
{
this.varmap = new Gee.HashMap<string, string> ();
this.paths = new Gee.HashMap<string, string> ();
foreach (var entry in node.map.entries) {
if (entry.key[0] == IMPORTS_SPE) {
this.varmap.set (entry.key.substring (1), entry.value.val ());
}
}
of.echo ("");
foreach (var entry in this.varmap.entries) {
of.keyval (entry.key, entry.value);
}
of.echo ("");
of.echo ("");
}
private string? strip_path(string? path)
{
string? s = path;
if (s != null && !Path.is_absolute (s) && s.substring (0, 2) == "./") {
s = s.substring (2);
}
return s;
}
} }

View File

@ -65,6 +65,12 @@ public interface Pluie.Yaml.Node : Object
*/ */
public abstract bool add (Yaml.Node node); public abstract bool add (Yaml.Node node);
/**
* add a child node to current collection (mapping or sequence) node
* @param child the Yaml.Node child to add
*/
public abstract string? val ();
/** /**
* stuff on changing parent node * stuff on changing parent node
* @param child the childto add * @param child the childto add

View File

@ -159,7 +159,7 @@ public class Pluie.Yaml.Processor
*/ */
public bool run () public bool run ()
{ {
//~ if (Pluie.Yaml.Scanner.DEBUG) this.read (); if (Pluie.Yaml.Scanner.DEBUG) this.read ();
this.root = new Yaml.NodeRoot (); this.root = new Yaml.NodeRoot ();
this.prev_node = this.root; this.prev_node = this.root;
this.parent_node = this.root; this.parent_node = this.root;