2
# -*- coding: utf-8 -*-
4
# This file is part of lum, the LDAP User Manager
6
# Author: Leonardo Robol <robol@poisson.phc.unipi.it>
12
from ConfigParser import SafeConfigParser as ConfigParser
14
class Configuration():
15
"""Configuration objects allow
16
code to fetch user preferences and
17
to change them permanently rewriting
18
the user Confugration file."""
23
# Check if configuration directory exists
24
# or can be created. If it doesn't, load default
25
# settings and do not save new values
26
if (self.user_has_directory()):
27
self.__persistent = True
28
self.__lock_file = os.path.expanduser("~/.lum/.lock")
30
self.__persistent = False
31
self.__lock_file = "/tmp/.lock_lum"
33
# Load configuration default values and, if it exists,
34
# load values in the configuration file
35
self.__conf = ConfigParser()
37
if self.__persistent and os.path.exists(self.__conf_file):
39
self.__conf.read(self.__conf_file)
41
self.__persistent = False
43
def user_has_directory(self):
44
"""Check if user has a .lum directory in the
45
home or if it can be created.
48
conf_dir = os.path.expanduser("~/.lum")
49
self.__conf_file = os.path.join(conf_dir, "lum.conf")
51
if os.path.exists(conf_dir):
61
def get(self, section, option):
64
return self.__conf.get(section, option)
67
"""Lock configuration directory"""
68
while os.path.exists(self.__lock_file):
70
with open(self.__lock_file, "w") as lock_file:
71
lock_file.write("Lum lock")
74
"""Unlock configuration directory"""
75
if os.path.exists(self.__lock_file):
76
os.remove(self.__lock_file)
78
def set(self, section, option, value):
80
self.__conf.set(section, option, value)
84
with open(self.__conf_file, "w") as conf_file:
85
self.__conf.write(conf_file)
87
self.__persistent = False
92
return self.__conf.options()
95
return self.__conf.sections()
97
def remove_section(self, section):
98
self.__conf.remove_section(section)
100
def has_section(self, section):
101
return self.__conf.has_section(section)
103
def has_option(self, section, option):
104
"""Check if option exists in section"""
105
return self.__conf.has_option(section, option)
107
def add_section(self, section):
108
self.__conf.add_section(section)
110
def remove_option(self, section, option):
112
self.__conf.remove_option(section, option)