From 4066612baaab02cc5a31caf6c01d774cdd20e0c2 Mon Sep 17 00:00:00 2001
From: Alain Takoudjou <alain.takoudjou@nexedi.com>
Date: Wed, 30 Nov 2016 17:55:01 +0100
Subject: [PATCH] zero_knowledge: allow to set destination folder of
 configuration file

---
 slapos/recipe/zero_knowledge.py | 42 ++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/slapos/recipe/zero_knowledge.py b/slapos/recipe/zero_knowledge.py
index 69e9e4bd2..c136c61bb 100644
--- a/slapos/recipe/zero_knowledge.py
+++ b/slapos/recipe/zero_knowledge.py
@@ -36,32 +36,36 @@ class WriteRecipe(GenericBaseRecipe):
   """
   """
   def __init__(self, buildout, name, options):
-    if not "filename" in options:
-      raise zc.buildout.UserError("You have to provide the parameter \"filename\"")
+    if not "filename" in options and not "file-path" in options:
+      raise zc.buildout.UserError("You have to provide the parameter either \"filename\" or \"file-path\"")
 
-    self.filename = options['filename'].strip()
-    self.path = os.path.join(buildout['buildout']['directory'], self.filename)
+    self._options = options.copy()
+    if options.get('filename'):
+      self.filename = options['filename'].strip()
+      self.path = os.path.join(buildout['buildout']['directory'], self.filename)
+      del _options['filename']
+    else:
+      self.path = options['file-path'].strip()
+      del options['file-path']
+    del self._options['recipe']
     self.name = name
-    self.options = options.copy()
-    del self.options['filename']
-    del self.options['recipe']
+
+  def install(self):
 
     # Set up the parser, and write config file if needed
     self.parser = ConfigParser.ConfigParser()
     try:
       self.parser.read(self.path)
       #clean_options(options)
-      for key in self.options:
+      for key in self._options:
         if key not in self.parser.options(self.name):
-          self.parser.set(self.name, key, self.options[key])
+          self.parser.set(self.name, key, self._options[key])
       with open(self.path, 'w') as file:
         self.parser.write(file)
     # If the file or section do not exist
     except (ConfigParser.NoSectionError, IOError) as e:
       self.full_install()
 
-  install = update = lambda self: []
-
   def full_install(self):
     """XXX-Nicolas : when some parameter's value is changed in
     buildout profile, this will override custom user defined values"""
@@ -69,21 +73,25 @@ class WriteRecipe(GenericBaseRecipe):
     if self.parser.has_section(self.name):
       self.parser.remove_section(self.name)
     self.parser.add_section(self.name)
-    for key in self.options:
-      self.parser.set(self.name, key, self.options[key])
+    for key in self._options:
+      self.parser.set(self.name, key, self._options[key])
     with open(self.path, 'w') as file:
       self.parser.write(file)
 
+  update = install
 
 class ReadRecipe(GenericBaseRecipe):
   """
   """
   def __init__(self, buildout, name, options):
-    if not "filename" in options:
-      raise zc.buildout.UserError("You have to provide the parameter \"filename\"")
+    if not "filename" in options and not "file-path" in options:
+      raise zc.buildout.UserError("You have to provide the parameter either \"filename\" or file-path")
 
-    self.filename = options['filename'].strip()
-    self.path = os.path.join(buildout['buildout']['directory'], self.filename)
+    if options.get('filename'):
+      self.filename = options['filename'].strip()
+      self.path = os.path.join(buildout['buildout']['directory'], self.filename)
+    else:
+      self.path = options['file-path'].strip()
 
     # Set up the parser, and write config file if needed
     self.parser = ConfigParser.ConfigParser()
-- 
2.30.9