Subversion Repositories basico

Rev

Rev 133 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: settings.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Settings service


from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import Pango
from gi.repository.GdkPixbuf import Pixbuf

import json

from .service import Service
from .env import FILE

#COMMENT Default settings for SAP module
#COMMENT LOGIN_PAGE_URL = "https://accounts.sap.com"
#COMMENT LOGOUT_PAGE_URL = "https://accounts.sap.com/ui/logout"
#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"
#COMMENT SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
#COMMENT SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
#COMMENT TIMEOUT = 5


class Settings(Service):
    def initialize(self):
        self.debug("Basico config file: %s" % FILE['CNF'])
        config = self.load()


    def get(self, section, key):
        config = self.load()
        try:
            value = config[section][key]
            self.debug("[%s][%s] = %s" % (section, key, value))
            return value
        except:
            self.debug("[%s][%s] = None" % (section, key))
            return None

    def set(self, section, key, value):
        config = self.load()
        try:
            config[section][key] = value
            self.save(config)
        except:
            self.log.error("Setting not saved")
            self.log.error(self.get_traceback())


    def load(self):
        try:
            with open(FILE['CNF'], 'r') as fp:
                config = json.load(fp)
        except Exception as error:
            self.debug("Config file not found. Creating a new one")
            config = {}
            self.save(config)

        return config


    def save(self, config=None):
        if config is None:
            self.log.error("A dictionary with all settings must be provided")
            return
        with open(FILE['CNF'], 'w') as fp:
            json.dump(config, fp)
        self.debug("Settings saved successfully")