From a191a385cf4a892f574a7dbc72ee35de1ea83a50 Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Thu, 2 Aug 2018 09:38:05 +0200 Subject: [PATCH] begin imports --- resources/config/db.yml | 15 +++ resources/config/subdir/test.yml | 6 ++ resources/main.yml | 26 +++++ samples/{json-load.vala => json-loader.vala} | 0 samples/yaml-imports.vala | 66 ++++++++++++ samples/{yaml-load.vala => yaml-loader.vala} | 0 src/vala/Pluie/Yaml.BaseNode.vala | 12 +++ src/vala/Pluie/Yaml.Config.vala | 104 ++++++++++++++++++- src/vala/Pluie/Yaml.Node.vala | 6 ++ src/vala/Pluie/Yaml.Processor.vala | 2 +- 10 files changed, 232 insertions(+), 5 deletions(-) create mode 100644 resources/config/db.yml create mode 100644 resources/config/subdir/test.yml create mode 100644 resources/main.yml rename samples/{json-load.vala => json-loader.vala} (100%) create mode 100644 samples/yaml-imports.vala rename samples/{yaml-load.vala => yaml-loader.vala} (100%) diff --git a/resources/config/db.yml b/resources/config/db.yml new file mode 100644 index 0000000..9e42ab6 --- /dev/null +++ b/resources/config/db.yml @@ -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 diff --git a/resources/config/subdir/test.yml b/resources/config/subdir/test.yml new file mode 100644 index 0000000..350d918 --- /dev/null +++ b/resources/config/subdir/test.yml @@ -0,0 +1,6 @@ +map1: + sp1 : val1 + sp2 : val2 + sp3 : val3 +map2: + sp4 : val4 diff --git a/resources/main.yml b/resources/main.yml new file mode 100644 index 0000000..c17909c --- /dev/null +++ b/resources/main.yml @@ -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 diff --git a/samples/json-load.vala b/samples/json-loader.vala similarity index 100% rename from samples/json-load.vala rename to samples/json-loader.vala diff --git a/samples/yaml-imports.vala b/samples/yaml-imports.vala new file mode 100644 index 0000000..706077d --- /dev/null +++ b/samples/yaml-imports.vala @@ -0,0 +1,66 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @software : lib-yaml + * @version : 0.3 + * @date : 2018 + * @licence : GPLv3.0 + * @author : a-Sansara <[dev]at[pluie]dot[org]> + * @copyright : 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 . + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + +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; + +} diff --git a/samples/yaml-load.vala b/samples/yaml-loader.vala similarity index 100% rename from samples/yaml-load.vala rename to samples/yaml-loader.vala diff --git a/src/vala/Pluie/Yaml.BaseNode.vala b/src/vala/Pluie/Yaml.BaseNode.vala index a86f3db..fa04af0 100644 --- a/src/vala/Pluie/Yaml.BaseNode.vala +++ b/src/vala/Pluie/Yaml.BaseNode.vala @@ -114,6 +114,18 @@ public class Pluie.Yaml.BaseNode : Object, Pluie.Yaml.Node 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 * @param the name of clone diff --git a/src/vala/Pluie/Yaml.Config.vala b/src/vala/Pluie/Yaml.Config.vala index 13321b8..c4ac5da 100644 --- a/src/vala/Pluie/Yaml.Config.vala +++ b/src/vala/Pluie/Yaml.Config.vala @@ -3,20 +3,31 @@ */ public class Pluie.Yaml.Config { + const char IMPORTS_SPE = '^'; /** * current path */ - public string? path { get; internal set; default = null; } + public string? path { get; internal set; default = null; } /** * Yaml Loader */ - public Yaml.Loader loader { internal get; internal set; } + public Yaml.Loader loader { internal get; internal set; } /** * Yaml Finder */ - public Yaml.Finder finder { internal get; internal set; } + public Yaml.Finder finder { internal get; internal set; } + + /** + * imports var + */ + Gee.HashMap varmap { get; internal set; } + + /** + * imports paths + */ + Gee.HashMap paths { get; internal set; } /** * construct a Yaml Config for specifiyed path @@ -26,8 +37,9 @@ public class Pluie.Yaml.Config Yaml.BaseNode.mode = mode; this.path = path; 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.get_imports (); } } @@ -43,4 +55,88 @@ public class Pluie.Yaml.Config 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 (); + this.paths = new Gee.HashMap (); + 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; + } + } diff --git a/src/vala/Pluie/Yaml.Node.vala b/src/vala/Pluie/Yaml.Node.vala index db2eeff..1dcea4e 100644 --- a/src/vala/Pluie/Yaml.Node.vala +++ b/src/vala/Pluie/Yaml.Node.vala @@ -65,6 +65,12 @@ public interface Pluie.Yaml.Node : Object */ 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 * @param child the childto add diff --git a/src/vala/Pluie/Yaml.Processor.vala b/src/vala/Pluie/Yaml.Processor.vala index 2d832db..27fefe4 100644 --- a/src/vala/Pluie/Yaml.Processor.vala +++ b/src/vala/Pluie/Yaml.Processor.vala @@ -159,7 +159,7 @@ public class Pluie.Yaml.Processor */ public bool run () { -//~ if (Pluie.Yaml.Scanner.DEBUG) this.read (); + if (Pluie.Yaml.Scanner.DEBUG) this.read (); this.root = new Yaml.NodeRoot (); this.prev_node = this.root; this.parent_node = this.root;