~leo.robol/lum/trunk

« back to all changes in this revision

Viewing changes to lum/configuration.py

  • Committer: Leonardo Robol
  • Date: 2010-09-19 15:31:17 UTC
  • Revision ID: git-v1:281ceece5234622daba78fabf0efaf6cc893693c
Moved source to better suit setup.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# -*- coding: utf-8 -*-
3
 
#
4
 
# This file is part of lum, the LDAP User Manager
5
 
6
 
# Author: Leonardo Robol <robol@poisson.phc.unipi.it>
7
 
# Date: 5 Sep 2010
8
 
#
9
 
#
10
 
 
11
 
import os
12
 
from ConfigParser import SafeConfigParser as ConfigParser
13
 
 
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."""
19
 
 
20
 
    def __init__(self):
21
 
    
22
 
        
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")
29
 
        else:
30
 
            self.__persistent = False
31
 
            self.__lock_file = "/tmp/.lock_lum"
32
 
            
33
 
        # Load configuration default values and, if it exists,
34
 
        # load values in the configuration file
35
 
        self.__conf = ConfigParser()
36
 
        
37
 
        if self.__persistent and os.path.exists(self.__conf_file):
38
 
            try:
39
 
                self.__conf.read(self.__conf_file)
40
 
            except OSError:
41
 
                self.__persistent = False
42
 
            
43
 
    def user_has_directory(self):
44
 
        """Check if user has a .lum directory in the
45
 
        home or if it can be created.
46
 
        """
47
 
        
48
 
        conf_dir = os.path.expanduser("~/.lum")
49
 
        self.__conf_file = os.path.join(conf_dir, "lum.conf")
50
 
        
51
 
        if os.path.exists(conf_dir):
52
 
            return True
53
 
        else:
54
 
            try:
55
 
                os.mkdir(conf_dir)
56
 
            except OSError:
57
 
                return False
58
 
                
59
 
        return True
60
 
        
61
 
    def get(self, section, option):
62
 
        """Get an option"""
63
 
        
64
 
        return self.__conf.get(section, option)
65
 
        
66
 
    def lock(self):
67
 
        """Lock configuration directory"""
68
 
        while os.path.exists(self.__lock_file):
69
 
            sleep(0.03)
70
 
        with open(self.__lock_file, "w") as lock_file:
71
 
            lock_file.write("Lum lock")
72
 
    
73
 
    def unlock(self):
74
 
        """Unlock configuration directory"""
75
 
        if os.path.exists(self.__lock_file):
76
 
            os.remove(self.__lock_file)
77
 
        
78
 
    def set(self, section, option, value):
79
 
        """Set an option"""
80
 
        self.__conf.set(section, option, value)
81
 
        if self.__persistent:
82
 
            try:
83
 
                self.lock()
84
 
                with open(self.__conf_file, "w") as conf_file:
85
 
                    self.__conf.write(conf_file)
86
 
            except OSError:
87
 
                self.__persistent = False
88
 
            finally:
89
 
                self.unlock()
90
 
                
91
 
    def options(self):
92
 
        return self.__conf.options()
93
 
        
94
 
    def sections(self):
95
 
        return self.__conf.sections()
96
 
        
97
 
    def remove_section(self, section):
98
 
        self.__conf.remove_section(section)
99
 
        
100
 
    def has_section(self, section):
101
 
        return self.__conf.has_section(section)
102
 
    
103
 
    def has_option(self, section, option):
104
 
        """Check if option exists in section"""
105
 
        return self.__conf.has_option(section, option)
106
 
        
107
 
    def add_section(self, section):
108
 
        self.__conf.add_section(section)
109
 
        
110
 
    def remove_option(self, section, option):
111
 
        """Remove option"""
112
 
        self.__conf.remove_option(section, option)
113
 
    
114