summaryrefslogtreecommitdiff
path: root/src/virtualenv/pyenv_cfg.py
blob: 06a8c81620575bd27523cf48789ab882c8786361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import absolute_import, unicode_literals

import logging


class PyEnvCfg(object):
    def __init__(self, content, path):
        self.content = content
        self.path = path

    @classmethod
    def from_folder(cls, folder):
        return cls.from_file(folder / "pyvenv.cfg")

    @classmethod
    def from_file(cls, path):
        content = cls._read_values(path) if path.exists() else {}
        return PyEnvCfg(content, path)

    @staticmethod
    def _read_values(path):
        content = {}
        for line in path.read_text().splitlines():
            equals_at = line.index("=")
            key = line[:equals_at].strip()
            value = line[equals_at + 1 :].strip()
            content[key] = value
        return content

    def write(self):
        with open(str(self.path), "wt") as file_handler:
            logging.debug("write %s", self.path)
            for key, value in self.content.items():
                line = "{} = {}".format(key, value)
                logging.debug("\t%s", line)
                file_handler.write(line)
                file_handler.write("\n")

    def refresh(self):
        self.content = self._read_values(self.path)
        return self.content

    def __setitem__(self, key, value):
        self.content[key] = value

    def __getitem__(self, key):
        return self.content[key]

    def __contains__(self, item):
        return item in self.content

    def update(self, other):
        self.content.update(other)
        return self