This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 296
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
92f5ed4
Cleanup database params - just pass the dict
erezsh 55aa54c
Config initial
erezsh 6a0ba8b
Tests: Added test for config
erezsh 1daded2
README: Added explanation + example for config file
erezsh 7367c88
Added toml dependency
erezsh 254f449
Remove passwords when logging the conf details. Fix typos.
erezsh e3ea83a
Comment out conf logging for now.
erezsh 3505a4d
Merge branch 'master' into config
erezsh f50bd64
Merge branch 'master' into config
erezsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from typing import Any, Dict | ||
import toml | ||
|
||
|
||
class ConfigParseError(Exception): | ||
pass | ||
|
||
|
||
def is_uri(s: str) -> bool: | ||
return "://" in s | ||
|
||
|
||
def _apply_config(config: Dict[str, Any], run_name: str, kw: Dict[str, Any]): | ||
# Load config | ||
databases = config.pop("database", {}) | ||
runs = config.pop("run", {}) | ||
if config: | ||
raise ConfigParseError(f"Unknown option(s): {config}") | ||
|
||
# Init run_args | ||
run_args = runs.get("default") or {} | ||
if run_name: | ||
if run_name not in runs: | ||
raise ConfigParseError(f"Cannot find run '{run_name}' in configuration.") | ||
run_args.update(runs[run_name]) | ||
else: | ||
run_name = "default" | ||
|
||
# Process databases + tables | ||
for index in "12": | ||
args = run_args.pop(index, {}) | ||
for attr in ("database", "table"): | ||
if attr not in args: | ||
raise ConfigParseError(f"Running 'run.{run_name}': Connection #{index} in missing attribute '{attr}'.") | ||
|
||
database = args.pop("database") | ||
table = args.pop("table") | ||
threads = args.pop("threads", None) | ||
if args: | ||
raise ConfigParseError(f"Unexpected attributes for connection #{index}: {args}") | ||
|
||
if not is_uri(database): | ||
if database not in databases: | ||
raise ConfigParseError( | ||
f"Database '{database}' not found in list of databases. Available: {list(databases)}." | ||
) | ||
database = dict(databases[database]) | ||
assert isinstance(database, dict) | ||
if "driver" not in database: | ||
raise ConfigParseError(f"Database '{database}' did not specify a driver.") | ||
|
||
run_args[f"database{index}"] = database | ||
run_args[f"table{index}"] = table | ||
if threads is not None: | ||
run_args[f"threads{index}"] = int(threads) | ||
|
||
# Update keywords | ||
new_kw = dict(kw) # Set defaults | ||
new_kw.update(run_args) # Apply config | ||
new_kw.update({k: v for k, v in kw.items() if v}) # Apply non-empty defaults | ||
nolar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
new_kw["__conf__"] = run_args | ||
|
||
return new_kw | ||
|
||
|
||
def apply_config_from_file(path: str, run_name: str, kw: Dict[str, Any]): | ||
with open(path) as f: | ||
return _apply_config(toml.load(f), run_name, kw) | ||
|
||
|
||
def apply_config_from_string(toml_config: str, run_name: str, kw: Dict[str, Any]): | ||
return _apply_config(toml.loads(toml_config), run_name, kw) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.