Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes issue with loading config when not using package #207

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down