Skip to content

Commit 78bdfb2

Browse files
authored
Merge pull request #141 from ssl-hep:gordonwatts/issue65
Look at parent directories for configuration file
2 parents c4cdb2b + e9bc137 commit 78bdfb2

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ do_query(ds) # Cache is not ignored
136136
As mentioned above, the `.servicex` file is read to pull a configuration. The search path for this file:
137137

138138
1. Your current working directory
139-
2. Your home directory
139+
2. Any working directory above your current working directory.
140+
3. Your home directory
140141

141142
The file can be named any of the following (ordered by precedence):
142143

servicex/ConfigSettings.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,30 @@ def _add_local_source(self):
2121
'''
2222
Look for a '.xxx" file in the local directory
2323
'''
24-
self._add_from_path(Path(f'{self.appname}.yaml'))
25-
self._add_from_path(Path(f'{self.appname}.yml'))
26-
self._add_from_path(Path(f'.{self.appname}'))
24+
self._add_from_path(Path(f'{self.appname}.yaml'), walk_up_tree=True)
25+
self._add_from_path(Path(f'{self.appname}.yml'), walk_up_tree=True)
26+
self._add_from_path(Path(f'.{self.appname}'), walk_up_tree=True)
2727

28-
def _add_from_path(self, p: Path):
29-
if p.exists():
30-
yaml_data = yaml_util.load_yaml(p, loader=self.loader) or {}
31-
self.add(ConfigSource(yaml_data, str(p.resolve())))
28+
# def _add_from_path(self, p: Path):
29+
# if p.exists():
30+
# yaml_data = yaml_util.load_yaml(p, loader=self.loader) or {}
31+
# self.add(ConfigSource(yaml_data, str(p.resolve())))
32+
33+
def _add_from_path(self, p: Path, walk_up_tree: bool = False):
34+
p.resolve()
35+
name = p.name
36+
dir = p.parent.resolve()
37+
while True:
38+
f = dir / name
39+
if f.exists():
40+
yaml_data = yaml_util.load_yaml(f, loader=self.loader) or {}
41+
self.add(ConfigSource(yaml_data, str(f)))
42+
break
43+
if not walk_up_tree:
44+
break
45+
if dir == dir.parent:
46+
break
47+
dir = dir.parent
3248

3349
def _add_home_source(self):
3450
'''

tests/test_ConfigSettings.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,59 @@ def test_home_file_default(confuse_config):
4848

4949
finally:
5050
config_file.unlink()
51+
52+
53+
def test_local_more_important_than_home(confuse_config):
54+
'Make sure that we pick the local directory over the home one'
55+
local_config_file = Path('.servicex_test_settings')
56+
home_config_file = Path.home() / '.servicex_test_settings'
57+
try:
58+
with home_config_file.open('w') as f:
59+
f.write('testing_value: 30\n')
60+
with local_config_file.open('w') as f:
61+
f.write('testing_value: 20\n')
62+
63+
confuse_config.clear()
64+
confuse_config.read()
65+
66+
assert get_it(confuse_config) == 20
67+
68+
finally:
69+
local_config_file.unlink()
70+
home_config_file.unlink()
71+
72+
73+
def test_one_level_up(confuse_config):
74+
'Make sure the config file that is one file up is found'
75+
config_file = Path('.').resolve().parent / ('.servicex_test_settings')
76+
try:
77+
with config_file.open('w') as f:
78+
f.write('testing_value: 30\n')
79+
80+
confuse_config.clear()
81+
confuse_config.read()
82+
83+
assert get_it(confuse_config) == 30
84+
85+
finally:
86+
config_file.unlink()
87+
88+
89+
def test_local_more_importnat_than_one_level_up(confuse_config):
90+
'Assure that our local file is found first'
91+
one_up_config_file = Path('.').resolve().parent / ('.servicex_test_settings')
92+
config_file = Path('.').resolve() / ('.servicex_test_settings')
93+
try:
94+
with config_file.open('w') as f:
95+
f.write('testing_value: 30\n')
96+
with one_up_config_file.open('w') as f:
97+
f.write('testing_value: 20\n')
98+
99+
confuse_config.clear()
100+
confuse_config.read()
101+
102+
assert get_it(confuse_config) == 30
103+
104+
finally:
105+
config_file.unlink()
106+
one_up_config_file.unlink()

0 commit comments

Comments
 (0)