/usr/lib/python3/dist-packages/setoptconf/source/filebased.py is in python3-setoptconf 0.2.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import os.path
from copy import deepcopy
from ..config import Configuration
from .base import Source
__all__ = (
'HomeDirectory',
'ConfigDirectory',
'FileBasedSource',
)
class DirectoryModifier(object):
def __init__(self, target_file):
self.target_file = target_file
def __call__(self):
raise NotImplementedError()
class HomeDirectory(DirectoryModifier):
def __call__(self):
return os.path.expanduser(
os.path.join(
'~',
self.target_file,
)
)
class ConfigDirectory(DirectoryModifier):
def __call__(self):
config_dir = os.getenv('XDG_CONFIG_HOME') \
or os.path.expanduser(
os.path.join(
'~',
'.config',
)
)
return os.path.join(config_dir, self.target_file)
class FileBasedSource(Source):
def __init__(self, files, base_path=None, combine=False):
super(FileBasedSource, self).__init__()
if isinstance(files, (str, DirectoryModifier)):
files = [files]
elif not isinstance(files, (tuple, list)):
raise TypeError('files must be a string or list of strings')
self.files = []
for target in files:
if isinstance(target, str):
self.files.append(target)
elif isinstance(target, DirectoryModifier):
self.files.append(target())
else:
raise TypeError('files must be a string or list of strings')
self.base_path = base_path or os.getcwd()
self.combine = combine
def get_config(self, settings, manager=None, parent=None):
parsed_settings = []
for file_source in self.files:
if os.path.isabs(file_source):
file_path = file_source
else:
file_path = os.path.join(self.base_path, file_source)
if os.path.exists(file_path):
file_settings = self.get_settings_from_file(
file_path,
deepcopy(settings),
manager=manager,
)
if file_settings:
parsed_settings.append(file_settings)
if not self.combine:
# No need to gather any more, we only want one.
break
if parsed_settings:
config = parent
for parsed_setting in reversed(parsed_settings):
config = Configuration(
settings=parsed_setting,
parent=config,
)
else:
config = Configuration(settings=settings, parent=parent)
return config
def get_settings_from_file(self, file_path, settings, manager=None):
raise NotImplementedError()
|