Commit 7a0c1e99 authored by Alain Takoudjou's avatar Alain Takoudjou

zero_knowledge: allow to set destination folder of configuration file

parent 1569f475
...@@ -36,32 +36,36 @@ class WriteRecipe(GenericBaseRecipe): ...@@ -36,32 +36,36 @@ class WriteRecipe(GenericBaseRecipe):
""" """
""" """
def __init__(self, buildout, name, options): def __init__(self, buildout, name, options):
if not "filename" in options: if not "filename" in options and not "file-path" in options:
raise zc.buildout.UserError("You have to provide the parameter \"filename\"") raise zc.buildout.UserError("You have to provide the parameter either \"filename\" or \"file-path\"")
self.filename = options['filename'].strip() self._options = options.copy()
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)
del _options['filename']
else:
self.path = options['file-path'].strip()
del options['file-path']
del self._options['recipe']
self.name = name self.name = name
self.options = options.copy()
del self.options['filename'] def install(self):
del self.options['recipe']
# Set up the parser, and write config file if needed # Set up the parser, and write config file if needed
self.parser = ConfigParser.ConfigParser() self.parser = ConfigParser.ConfigParser()
try: try:
self.parser.read(self.path) self.parser.read(self.path)
#clean_options(options) #clean_options(options)
for key in self.options: for key in self._options:
if key not in self.parser.options(self.name): 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: with open(self.path, 'w') as file:
self.parser.write(file) self.parser.write(file)
# If the file or section do not exist # If the file or section do not exist
except (ConfigParser.NoSectionError, IOError) as e: except (ConfigParser.NoSectionError, IOError) as e:
self.full_install() self.full_install()
install = update = lambda self: []
def full_install(self): def full_install(self):
"""XXX-Nicolas : when some parameter's value is changed in """XXX-Nicolas : when some parameter's value is changed in
buildout profile, this will override custom user defined values""" buildout profile, this will override custom user defined values"""
...@@ -69,21 +73,25 @@ class WriteRecipe(GenericBaseRecipe): ...@@ -69,21 +73,25 @@ class WriteRecipe(GenericBaseRecipe):
if self.parser.has_section(self.name): if self.parser.has_section(self.name):
self.parser.remove_section(self.name) self.parser.remove_section(self.name)
self.parser.add_section(self.name) self.parser.add_section(self.name)
for key in self.options: for key in self._options:
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: with open(self.path, 'w') as file:
self.parser.write(file) self.parser.write(file)
update = install
class ReadRecipe(GenericBaseRecipe): class ReadRecipe(GenericBaseRecipe):
""" """
""" """
def __init__(self, buildout, name, options): def __init__(self, buildout, name, options):
if not "filename" in options: if not "filename" in options and not "file-path" in options:
raise zc.buildout.UserError("You have to provide the parameter \"filename\"") raise zc.buildout.UserError("You have to provide the parameter either \"filename\" or file-path")
self.filename = options['filename'].strip() if options.get('filename'):
self.path = os.path.join(buildout['buildout']['directory'], self.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 # Set up the parser, and write config file if needed
self.parser = ConfigParser.ConfigParser() self.parser = ConfigParser.ConfigParser()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment