1
# -*- coding: utf-8 -*-
2
# Copyright 2009 Canonical Ltd.
3
# Author 2009 Didier Roche
5
# This file is part of Quickly
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.
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.
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/>.
23
from gettext import gettext as _
25
from quickly import tools
27
global_config = {} # retrieve from /etc/quickly
28
project_config = {} # retrieved from project/.quickly
30
def loadConfig(global_config=None):
31
""" load configuration from /etc/quickly or .quickly file"""
34
quickly_file_path = '/etc/quickly'
35
config = global_config
37
# retrieve .quickly file
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.")
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"
53
config[fields[0].strip()] = fields[1].strip()
55
except (OSError, IOError), e:
56
print _("ERROR: Can't load configuration in %s: %s" % (quickly_file_path, e))
60
def saveConfig(global_config=None, config_file_path=None):
61
""" save the configuration file from config dictionnary in project or global quuickly file
63
config_file_path is optional (needed by the create command, for instance).
64
getcwd() is taken by default.
66
keep commentaries and layout from original file """
69
quickly_file_path = '/etc/quickly'
70
config = global_config
72
# retrieve .quickly file
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'
80
quickly_file_path = os.getcwd() + "/.quickly"
81
print _("WARNING: No .quickly file found. Initiate a new one")
82
config = project_config
85
filedest = file(quickly_file_path + '.swp', 'w')
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
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)
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")
110
os.rename(filedest.name, quickly_file_path)
111
except AttributeError, e:
112
print _("ERROR: Can't save configuration in %s" % quickly_file_path)