~ubuntu-branches/ubuntu/natty/quickly/natty

« back to all changes in this revision

Viewing changes to quickly/configurationhandler.py

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-07-24 18:16:30 UTC
  • Revision ID: james.westby@ubuntu.com-20090724181630-s2wo7hvahk8u0hqb
Tags: 0.1
Initial release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright 2009 Canonical Ltd.
 
3
# Author 2009 Didier Roche
 
4
#
 
5
# This file is part of Quickly
 
6
#
 
7
#This program is free software: you can redistribute it and/or modify it 
 
8
#under the terms of the GNU General Public License version 3, as published 
 
9
#by the Free Software Foundation.
 
10
 
 
11
#This program is distributed in the hope that it will be useful, but 
 
12
#WITHOUT ANY WARRANTY; without even the implied warranties of 
 
13
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
14
#PURPOSE.  See the GNU General Public License for more details.
 
15
 
 
16
#You should have received a copy of the GNU General Public License along 
 
17
#with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
import os
 
20
import sys
 
21
 
 
22
import gettext
 
23
from gettext import gettext as _
 
24
 
 
25
from quickly import tools
 
26
 
 
27
global_config = {} # retrieve from /etc/quickly
 
28
project_config = {} # retrieved from project/.quickly
 
29
 
 
30
def loadConfig(global_config=None):
 
31
    """ load configuration from /etc/quickly or .quickly file"""
 
32
 
 
33
    if global_config:
 
34
        quickly_file_path = '/etc/quickly'
 
35
        config = global_config
 
36
    else:
 
37
        # retrieve .quickly file
 
38
        try:
 
39
            quickly_file_path = tools.get_root_project_path()  + '/.quickly'
 
40
            config = project_config
 
41
        except tools.project_path_not_found:
 
42
            print _("ERROR: Can't load configuration in current path or its parent ones.")
 
43
            sys.exit(1)
 
44
        
 
45
        
 
46
    try:
 
47
        fileconfig = file(quickly_file_path, 'rb')
 
48
        for line in fileconfig: 
 
49
            fields = line.split('#')[0] # Suppress commentary after the value in configuration file and in full line
 
50
            fields = fields.split('=') # Separate variable from value
 
51
            # normally, we have two fields in "fields"
 
52
            if len(fields) == 2:
 
53
                config[fields[0].strip()] = fields[1].strip() 
 
54
        fileconfig.close()
 
55
    except (OSError, IOError), e:
 
56
        print _("ERROR: Can't load configuration in %s: %s" % (quickly_file_path, e))
 
57
        sys.exit(1)
 
58
 
 
59
 
 
60
def saveConfig(global_config=None, config_file_path=None):
 
61
    """ save the configuration file from config dictionnary in project or global quuickly file
 
62
 
 
63
    config_file_path is optional (needed by the create command, for instance).
 
64
    getcwd() is taken by default.    
 
65
    
 
66
    keep commentaries and layout from original file """
 
67
 
 
68
    if global_config:
 
69
        quickly_file_path = '/etc/quickly'
 
70
        config = global_config
 
71
    else:
 
72
        # retrieve .quickly file
 
73
        try:
 
74
            quickly_file_path = tools.get_root_project_path(config_file_path) + '/.quickly'
 
75
        # if no .quickly, create it using config_file_path or cwd
 
76
        except tools.project_path_not_found:
 
77
            if config_file_path is not None:
 
78
                quickly_file_path = os.path.abspath(config_file_path) + '/.quickly'
 
79
            else:
 
80
                quickly_file_path = os.getcwd() + "/.quickly"
 
81
            print _("WARNING: No .quickly file found. Initiate a new one")
 
82
        config = project_config
 
83
    
 
84
    try:
 
85
        filedest = file(quickly_file_path + '.swp', 'w')
 
86
        try:
 
87
            fileconfig = file(quickly_file_path, 'rb')
 
88
            remaingconfigtosave = config.copy()
 
89
            for line in fileconfig:
 
90
                fields = line.split('#')[0] # Suppress commentary after the value in configuration file and in full line
 
91
                fieldsafter = line.split('#')[1:]
 
92
                fields = fields.split('=') # Separate variable from value
 
93
                # normally, we have two fields in "fields" and it should be used by config tabular
 
94
                if len(fields) == 2 and fields[0].strip() in remaingconfigtosave:
 
95
                    line = fields[0].strip() + " = " + str(remaingconfigtosave.pop(fields[0].strip()))
 
96
                    if len(fieldsafter) > 0:
 
97
                        line = line + " #" + "#".join(fieldsafter) # fieldsafter already contains \n
 
98
                    else:
 
99
                        line = line + "\n"
 
100
                filedest.write(line) # commentaries or empty lines, anything other things which is not useful will be printed unchanged
 
101
            # write remaining data if some (new data not in the old config file).
 
102
            filedest.write("".join(elem + " = " + str(remaingconfigtosave[elem]) + '\n' for elem in remaingconfigtosave)) #\n here for last element (and not be added, when no iteration to do)
 
103
#            print "\n".join(elem + " = " + remaingconfigtosave[elem] for elem in remaingconfigtosave)
 
104
            fileconfig.close()
 
105
        except (OSError, IOError), e:      
 
106
            # write config file from scratch (no previous file found)
 
107
            filedest.write("\n".join(elem + " = " + str(config[elem]) for elem in config) + "\n")
 
108
        finally:
 
109
            filedest.close()
 
110
            os.rename(filedest.name, quickly_file_path)
 
111
    except AttributeError, e:
 
112
        print _("ERROR: Can't save configuration in %s" % quickly_file_path)
 
113
        return 1
 
114
    return 0
 
115
    
 
116