Skip to content

Commit

Permalink
Merge pull request #207 from carlosmmatos/fix-config-issues
Browse files Browse the repository at this point in the history
fix: fixes issue with loading config when not using package
carlosmmatos authored Jan 31, 2025
2 parents 5ee8279 + 3c44f90 commit bf5b704
Showing 4 changed files with 51 additions and 13 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ COPY requirements.txt .
RUN pip install -r ./requirements.txt

COPY . .
RUN chown -R figuser:figuser /fig

USER figuser

2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include config/*
include config/*.ini
59 changes: 48 additions & 11 deletions fig/config/__init__.py
Original file line number Diff line number Diff line change
@@ -50,17 +50,54 @@ def __init__(self):
self._override_from_credentials_store()

def _read_config_files(self):
# If installed via python package, look for defaults.ini in package directory
default_config = files('config').joinpath('defaults.ini')
self.read(default_config)
# Allow overrides from local directory and config directory
config_files = ['defaults.ini', 'config.ini', 'devel.ini']
for file in config_files:
# Try reading from local directory first, then from config directory
if os.path.exists(file):
self.read(file)
elif os.path.exists(os.path.join('config', file)):
self.read(os.path.join('config', file))
# Define possible locations for config files
base_paths = [
os.path.dirname(
os.path.dirname(os.path.dirname(__file__))
), # Root project directory
os.getcwd(), # Current working directory
]

# Try to find and read defaults.ini first
defaults_found = False
for base_path in base_paths:
possible_paths = [
os.path.join(base_path, "config", "defaults.ini"),
os.path.join(base_path, "defaults.ini"),
]

for path in possible_paths:
if os.path.exists(path):
self.read(path)
defaults_found = True
break

if defaults_found:
break

if not defaults_found:
try:
# Try reading from installed package
default_config = files("config").joinpath("defaults.ini")
self.read(default_config)
except (TypeError, ModuleNotFoundError, ValueError) as exc:
raise FileNotFoundError(
"Could not find defaults.ini in any expected location. "
"Make sure defaults.ini exists in the config directory."
) from exc

# Allow overrides from config.ini and devel.ini
override_files = ["config.ini", "devel.ini"]
for file in override_files:
for base_path in base_paths:
possible_paths = [
os.path.join(base_path, "config", file),
os.path.join(base_path, file),
]

for path in possible_paths:
if os.path.exists(path):
self.read(path)

def _override_from_env(self):
for section, var, envvar in self.__class__.ENV_DEFAULTS:
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
url="https://github.com/crowdstrike/falcon-integration-gateway",
packages=find_packages(),
package_data={
'fig': ['../config/*.ini'],
'': ['config/*.ini'],
},
include_package_data=True,
install_requires=[

0 comments on commit bf5b704

Please sign in to comment.