File: config.py

package info (click to toggle)
subuser 0.6.2-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 4,208 kB
  • sloc: python: 5,201; sh: 380; makefile: 73
file content (51 lines) | stat: -rwxr-xr-x 1,719 bytes parent folder | download
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
# -*- coding: utf-8 -*-
"""
The Config class is used to hold user wide settings.
"""

#external imports
import os
#internal imports
from subuserlib.classes import userOwnedObject
from subuserlib import loadMultiFallbackJsonConfigFile
from subuserlib import paths

class Config(userOwnedObject.UserOwnedObject, dict):
  def __init__(self,user):
    self.__delitem__ = None
    self.__setitem__ = None
    userOwnedObject.UserOwnedObject.__init__(self,user)
    self._loadConfig()

  def _getSubuserConfigPaths(self):
    """ Returns a list of paths to config.json files in order that they should be looked in. """
    configFileInHomeDir = os.path.join(self.user.homeDir,".subuser","config.json")
    configFileInEtc = "/etc/subuser/config.json"
    configFileInSubuserDir = paths.getSubuserDataFile("config.json")
    return [configFileInHomeDir,configFileInEtc,configFileInSubuserDir]

  def _expandPathsInConfig(self,config):
    """
    Go through a freshly loaded config file and expand any environment variables in the paths.
    """
    pathsToExpand = [
      "bin-dir"
      ,"registry-dir"
      ,"installed-images-list"
      ,"locked-subusers-path"
      ,"subuser-home-dirs-dir"
      ,"repositories-dir"
      ,"runtime-cache"
      ,"lock-dir"
      ,"volumes-dir"]
    loadMultiFallbackJsonConfigFile.expandPathsInDict(self.user.homeDir,pathsToExpand,config)

  def _loadConfig(self):
    """
    Loads the subuser config: a dictionary of settings used by subuser.
    """
    configFileHierarchy = self._getSubuserConfigPaths()
    config = loadMultiFallbackJsonConfigFile.getConfig(configFileHierarchy)
    self._expandPathsInConfig(config)
    for key,entry in config.items():
      self[key] = entry