Skip to content

Commit f8587a9

Browse files
committed
Adding file_override
1 parent 409a4cb commit f8587a9

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

dotconfig/__init__.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212

1313
class Config(object):
14-
def __init__(self, app, name, base_dir='~/.config/',
14+
def __init__(self, app=None, name=None, base_dir='~/.config/',
1515
template_file=None, template=None, envvars={},
16-
defaults={}, cli_args={}, path_override=None):
16+
defaults={}, cli_args={}, path_override=None,
17+
file_override=None):
1718
"""
1819
Automatically load an app config with environment variable and
1920
default overrides.
@@ -30,46 +31,54 @@ def __init__(self, app, name, base_dir='~/.config/',
3031
:param dict defaults: Default values for config keys
3132
:param dict cli_args: Config key overrides likely from the app CLI.
3233
These override any config file or env var values.
34+
:param str path_override: Specify direct path instead of default base dir
35+
:param str file_override: Direct path to config file to read
3336
3437
:raises exceptions.IOError: when permissions denied
3538
"""
3639

37-
if path_override:
38-
self.base_dir = os.path.expanduser(path_override)
39-
else:
40-
self.base_dir = os.path.expanduser(os.path.join(base_dir, app))
41-
42-
if not os.path.isdir(self.base_dir):
43-
os.makedirs(self.base_dir)
44-
4540
self.filename = None
4641
self.full_path = None
4742

48-
for ext in VALID_EXT:
49-
for _ext in [ext, ext.upper()]:
50-
filename = '{}.{}'.format(name, _ext)
51-
cfg = os.path.join(self.base_dir, filename)
52-
if os.path.isfile(cfg):
53-
self.full_path = cfg
54-
self.filename = filename
55-
break
56-
57-
if self.filename is None:
58-
self.filename = name + '.' + VALID_EXT[0]
59-
self.full_path = os.path.join(self.base_dir, self.filename)
60-
if template_file:
61-
shutil.copyfile(template_file, self.full_path)
43+
if file_override is not None:
44+
self.full_path = file_override
45+
if not os.path.isfile(self.full_path):
46+
raise Exception('Unable to find specified file {}'.format(self.full_path))
47+
else:
48+
if path_override:
49+
self.base_dir = os.path.expanduser(path_override)
6250
else:
63-
with open(self.full_path, 'w') as f:
64-
if template:
65-
if isinstance(template, str):
66-
f.write(template)
67-
elif isinstance(template, dict):
68-
yaml.dump(template, f, default_flow_style=False)
69-
else:
70-
yaml.dump({}, f, default_flow_style=False)
51+
self.base_dir = os.path.expanduser(os.path.join(base_dir, app))
52+
53+
if not os.path.isdir(self.base_dir):
54+
os.makedirs(self.base_dir)
55+
56+
for ext in VALID_EXT:
57+
for _ext in [ext, ext.upper()]:
58+
filename = '{}.{}'.format(name, _ext)
59+
cfg = os.path.join(self.base_dir, filename)
60+
if os.path.isfile(cfg):
61+
self.full_path = cfg
62+
self.filename = filename
63+
break
64+
65+
if self.filename is None:
66+
self.filename = name + '.' + VALID_EXT[0]
67+
self.full_path = os.path.join(self.base_dir, self.filename)
68+
if template_file:
69+
shutil.copyfile(template_file, self.full_path)
70+
else:
71+
with open(self.full_path, 'w') as f:
72+
if template:
73+
if isinstance(template, str):
74+
f.write(template)
75+
elif isinstance(template, dict):
76+
yaml.dump(template, f, default_flow_style=False)
77+
else:
78+
yaml.dump({}, f, default_flow_style=False)
7179

7280
self._data = defaults
81+
print(self.full_path)
7382
with open(self.full_path, 'r') as f:
7483
data = yaml.full_load(f)
7584
self._data.update(data)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name="pydotconfig",
11-
version="0.1.4",
11+
version="0.1.5",
1212
description="Super simple python module for parsing structured config files with overrides",
1313
long_description=open('README.md').read(),
1414
url="https://github.com/adammhaile/dotconfig",

0 commit comments

Comments
 (0)