Subversion Repositories basico

Rev

Rev 180 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: settings.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Settings service
7
 
8
 
9
from gi.repository import Gtk
10
from gi.repository import Gio
11
from gi.repository import Pango
12
from gi.repository.GdkPixbuf import Pixbuf
13
 
126 t00mlabs 14
import json
15
 
233 t00m 16
from basico.core.service import Service
17
from basico.core.env import FILE
4 t00mlabs 18
 
133 t00mlabs 19
#COMMENT Default settings for SAP module
20
#COMMENT LOGIN_PAGE_URL = "https://accounts.sap.com"
21
#COMMENT LOGOUT_PAGE_URL = "https://accounts.sap.com/ui/logout"
22
#COMMENT ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')" #$expand=LongText" #?$expand=LongText,RefTo,RefBy"
23
#COMMENT SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
24
#COMMENT SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
25
#COMMENT TIMEOUT = 5
74 t00mlabs 26
 
27
 
4 t00mlabs 28
class Settings(Service):
29
    def initialize(self):
133 t00mlabs 30
        self.debug("Basico config file: %s" % FILE['CNF'])
180 t00m 31
        config = self.load()
74 t00mlabs 32
 
180 t00m 33
 
126 t00mlabs 34
    def get(self, section, key):
35
        config = self.load()
36
        try:
37
            value = config[section][key]
133 t00mlabs 38
            self.debug("[%s][%s] = %s" % (section, key, value))
126 t00mlabs 39
            return value
40
        except:
133 t00mlabs 41
            self.debug("[%s][%s] = None" % (section, key))
126 t00mlabs 42
            return None
74 t00mlabs 43
 
126 t00mlabs 44
    def set(self, section, key, value):
45
        config = self.load()
46
        try:
47
            config[section][key] = value
48
            self.save(config)
49
        except:
50
            self.log.error("Setting not saved")
51
            self.log.error(self.get_traceback())
74 t00mlabs 52
 
53
 
126 t00mlabs 54
    def load(self):
55
        try:
56
            with open(FILE['CNF'], 'r') as fp:
57
                config = json.load(fp)
58
        except Exception as error:
133 t00mlabs 59
            self.debug("Config file not found. Creating a new one")
126 t00mlabs 60
            config = {}
61
            self.save(config)
74 t00mlabs 62
 
126 t00mlabs 63
        return config
74 t00mlabs 64
 
82 t00mlabs 65
 
126 t00mlabs 66
    def save(self, config=None):
67
        if config is None:
68
            self.log.error("A dictionary with all settings must be provided")
69
            return
70
        with open(FILE['CNF'], 'w') as fp:
71
            json.dump(config, fp)
133 t00mlabs 72
        self.debug("Settings saved successfully")