Skip to content

Commit

Permalink
Merge pull request #451 from dssg/cli-setupfile
Browse files Browse the repository at this point in the history
Added an argument for a pythonic entrypoint for setting an arbitrary initial configuration
  • Loading branch information
thcrock authored Oct 5, 2018
2 parents b41212c + 5404f4e commit a553361
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/triage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

logging.basicConfig(level=logging.INFO)

import importlib.util


def natural_number(value):
natural = int(value)
Expand All @@ -44,6 +46,20 @@ def __init__(self, parser):
type=argparse.FileType('r'),
help="database connection file",
)
parser.add_argument(
'-s', '--setup',
help="file path to Python module to import before running the Experiment",
)

def setup(self):
setup_path = self.args.setup or os.path.abspath('experiment.py')
if setup_path is not None:
logging.info(f"Loading configurations from {setup_path}")
spec = importlib.util.spec_from_file_location("triage_config", setup_path)
triage_config = importlib.util.module_from_spec(spec)
spec.loader.exec_module(triage_config)
logging.info(f"Configuration loaded")
return None

@cachedproperty
def db_url(self):
Expand Down Expand Up @@ -100,6 +116,7 @@ def __init__(self, parser):
)

def __call__(self, args):
self.root.setup # Loading configuration (if exists)
db_engine = create_engine(self.root.db_url)
feature_config = yaml.load(args.feature_config_file)

Expand Down Expand Up @@ -166,6 +183,7 @@ def __init__(self, parser):

@cachedproperty
def experiment(self):
self.root.setup # Loading configuration (if exists)
db_url = self.root.db_url
config = yaml.load(self.args.config)
db_engine = create_engine(db_url)
Expand Down Expand Up @@ -213,6 +231,7 @@ def __init__(self, parser):

@cachedproperty
def runner(self):
self.root.setup # Loading configuration (if exists)
db_url = self.root.db_url
dir_plot = self.args.directory
config = yaml.load(self.args.config)
Expand Down Expand Up @@ -254,7 +273,7 @@ def upgrade(self, args):
@cmdmethod('configversion', choices=REVISION_MAPPING.keys(), help='config version of last experiment you ran')
def stamp(self, args):
"""Instruct the triage results database to mark itself as updated to a known version without doing any upgrading.
Use this if the database was created without an 'alembic_version' table. Uses the config version of your experiment to infer what database version is suitable.
"""
revision = REVISION_MAPPING[args.configversion]
Expand Down
2 changes: 2 additions & 0 deletions src/triage/util/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# coding: utf-8

import sqlalchemy
import wrapt

Expand Down

0 comments on commit a553361

Please sign in to comment.