def test_read_with_real_file(self, env_read):
     env_read.return_value = 'tests/fixtures/config.sample'
     assert Config.get('models.default_context_name') == 'pdl'
     assert Config.get('models.default_context_name',
                       section='section') == 'pdl2'
     assert Config.get('models.default_type_name') == 'pdl'
     assert Config.get('models.default_type_name') == Config.get(
         'models.default_type_name', section='section')
 def test_get_with_invalid_section(self, load_conf_from_file_mocked,
                                   config_fixture):
     load_conf_from_file_mocked.return_value = {}
     with pytest.raises(InvalidConfigException):
         Config.get('key', section='invalidsection')
 def test_get_invalid_key_with_default(self, load_conf_from_file_mocked,
                                       config_fixture):
     load_conf_from_file_mocked.return_value = config_fixture
     assert 'invalidkey' not in config_fixture
     assert Config.get('invalidkey',
                       default='default_value') == 'default_value'
 def test_get_invalid_key(self, load_conf_from_file_mocked, config_fixture):
     load_conf_from_file_mocked.return_value = config_fixture
     assert 'invalidkey' not in config_fixture
     with pytest.raises(InvalidConfigException):
         Config.get('invalidkey')
 def test_get(self, load_conf_from_file_mocked, config_fixture):
     load_conf_from_file_mocked.return_value = config_fixture
     assert Config.get('key') == config_fixture['key']
 def teardown_method(self, test_method):
     Config.reset()
 def test_keys_with_invalid_section(self, load_conf_from_file_mocked):
     load_conf_from_file_mocked.return_value = {}
     assert not Config.keys(section='invalidsection')
 def test_keys(self, load_conf_from_file_mocked, config_fixture):
     load_conf_from_file_mocked.return_value = config_fixture
     assert Config.keys() == config_fixture.keys()
def marvin_code_export(model, **kwargs):

    import autopep8
    import inspect
    import re
    from marvin_python_daemon.common.config import Config

    print("Executing the marvin export hook script...")

    if model['type'] != 'notebook':
        return

    # import ipdb; ipdb.set_trace()

    cells = model['content']['cells']

    artifacts = {
        'marvin_initial_dataset': re.compile(r"(\bmarvin_initial_dataset\b)"),
        'marvin_dataset': re.compile(r"(\bmarvin_dataset\b)"),
        'marvin_model': re.compile(r"(\bmarvin_model\b)"),
        'marvin_metrics': re.compile(r"(\bmarvin_metrics\b)")
    }

    batch_exec_pattern = re.compile(
        "(def\s+execute\s*\(\s*self\s*,\s*params\s*,\s*\*\*kwargs\s*\)\s*:)")
    online_exec_pattern = re.compile(
        "(def\s+execute\s*\(\s*self\s*,\s*input_message\s*,\s*params\s*,\s*\*\*kwargs\s*\)\s*:)"
    )

    CLAZZES = {
        "acquisitor": "AcquisitorAndCleaner",
        "tpreparator": "TrainingPreparator",
        "trainer": "Trainer",
        "evaluator": "MetricsEvaluator",
        "ppreparator": "PredictionPreparator",
        "predictor": "Predictor",
        "feedback": "Feedback"
    }

    for cell in cells:
        if cell['cell_type'] == 'code' and cell["metadata"].get(
                "marvin_cell", False):
            source = cell["source"]
            new_source = autopep8.fix_code(source,
                                           options={'max_line_length': 160})

            marvin_action = cell["metadata"]["marvin_cell"]
            marvin_action_clazz = getattr(__import__(Config.get("package")),
                                          CLAZZES[marvin_action])
            source_path = inspect.getsourcefile(marvin_action_clazz)

            fnew_source_lines = []
            for new_line in new_source.split("\n"):
                fnew_line = "        " + new_line + "\n" if new_line.strip(
                ) else "\n"

                if not new_line.startswith(
                        "import") and not new_line.startswith(
                            "from") and not new_line.startswith("print"):
                    for artifact in artifacts.keys():
                        fnew_line = re.sub(artifacts[artifact],
                                           'self.' + artifact, fnew_line)

                fnew_source_lines.append(fnew_line)

            if marvin_action == "predictor":
                fnew_source_lines.append("        return final_prediction\n")
                exec_pattern = online_exec_pattern

            elif marvin_action == "ppreparator":
                fnew_source_lines.append("        return input_message\n")
                exec_pattern = online_exec_pattern

            elif marvin_action == "feedback":
                fnew_source_lines.append(
                    "        return \"Thanks for the feedback!\"\n")
                exec_pattern = online_exec_pattern

            else:
                exec_pattern = batch_exec_pattern

            fnew_source = "".join(fnew_source_lines)

            with open(source_path, 'r+') as fp:
                lines = fp.readlines()
                fp.seek(0)
                for line in lines:
                    if re.findall(exec_pattern, line):
                        fp.write(line)
                        fp.write(fnew_source)
                        fp.truncate()

                        break
                    else:
                        fp.write(line)

            print("File {} updated!".format(source_path))

    print("Finished the marvin export hook script...")