예제 #1
0
파일: yatc.py 프로젝트: nixargh/yatc
class Config():
    """Operations with configuration."""
    def __init__(self):
        from crypt import Crypt
        self.configFile = os.path.expanduser("~/.yatc/yatc.conf")
        self.config = {}
        self.crypt = Crypt()
        logging.info("Config initialized.")

    def createConfig(self):
        file = open(self.configFile, "wb")
        string = self.crypt.encryptString("admuser=%s\n" % getAdmuser())
        file.write(string)
        file.close()

    def read(self):
        """Read config from file."""
        if not os.path.isfile(self.configFile):
            self.createConfig()
        file = open(self.configFile, "rb")
        conf = {} 
        cryptedInfo = file.read()
        info = self.crypt.decryptString(cryptedInfo)
        settings = info.rsplit("\n")
        for line in settings:
            if len(line) > 0:
                attr, value = line.rsplit("=")
                conf[attr] = value
        file.close()
        logging.info("Configuration read.")
        logging.debug(conf)
        self.config = conf

    def write(self):
        """Write config to file."""
        # don't save user if this option unchecked
        if self.config.get("login"):
            if self.config["saveUser"] == 0:
                del self.config["login"]

        # create settings string
        string = ""
        for attr, value in self.config.items():
            string = string + ("%s=%s\n" % (attr, value))
        cryptedLine = self.crypt.encryptString(string)
        # write settings to file
        file = open(self.configFile, "wb")
        file.write(cryptedLine)
        file.close() 
        logging.info("Configuration wrote.")

    def get(self):
        """Get current configuration."""
        logging.info("Configuration is obtained.")
        return self.config

    def put(self, config):
        """Put changed configuration."""
        logging.info("Configuration is updated.")
        self.config = config