Example #1
0
def test_schema(yaml_filename, valid_rego):
    """
    All our test files are valid flavour schemas. This test just verifies this
    and it is not intended to catch invalid schemas.
    """
    with Path(yaml_filename).open("r") as f:
        load(f.read(), libflavour.schema.schema_addon)
Example #2
0
def main(args=None):
    if args is None:
        args = sys.argv[1:]
    parser = argparser()
    args = parser.parse_args(args)
    set_colors(args.color)
    if not args.file:
        if sys.stdin.isatty():
            parser.print_help()
            sys.exit(1)
        else:
            args.file = [sys.stdin]
    errors = 0

    for f in args.file:
        success = True
        with f as file_:
            content = file_.read()
            try:
                load(content, label=file_.name)
            except YAMLError as error:
                errors += 1
                success = False
                print(error)
            msg, color = parsed(file_.name, success, symbols=args.symbols)
            cprint(msg, color)

    exit(errors, args.symbols)
    def testGenerateGuidSkipsGoodGuid(self):
        input_file = os.path.join(_TESTCASE_PATH, 'GOOD',
                                  'good_building_type.yaml')
        with open(input_file, 'r', encoding='utf-8') as test_instance:
            test_filenames = [
                self.create_tempfile(content=test_instance.read())
            ]
            initial_guids = []
            for filename in test_filenames:
                yaml_dict = syaml.load(filename.read_text())
                for value in yaml_dict.values():
                    initial_guids.append(
                        value[instance_parser.ENTITY_GUID_KEY])

            for filename in test_filenames:
                GuidGenerator.GenerateGuids(filename.full_path)
            later_guids = []
            for filename in test_filenames:
                yaml_dict = syaml.load(filename.read_text())
                # Starting at the second element to skip over config metadata block
                for i in range(1, len(yaml_dict.values())):
                    later_guids.append(
                        yaml_dict.values()[i][instance_parser.ENTITY_GUID_KEY])

            self.assertEqual(initial_guids, later_guids)
            self.assertEqual(initial_guids[0], 'UK-LON-S2-guid')
Example #4
0
def run():
    arguments = argv[1:]
    assert len(arguments) == 1 or len(arguments) == 2
    template_type = arguments[0]
    assert template_type in ["demo", "skeleton"]
    available_templates = sorted(
        [str(path.basename()) for path in THIS_DIR.joinpath(template_type).listdir()]
    )

    if len(arguments) == 1:
        template_commands = [
            "hk --{} {}".format(template_type, template)
            for template in available_templates
        ]
        for template, template_command in zip(available_templates, template_commands):
            print(
                "{}{}  # {}".format(
                    template_command,
                    (
                        max([len(cmd) for cmd in template_commands])
                        - len(template_command)
                    )
                    * " ",
                    load(
                        THIS_DIR.joinpath(template_type, template, "hitchqs.yml").text()
                    ).data["about"],
                )
            )
        exit(0)

    template = arguments[1]
    if template not in available_templates:
        print("{} '{}' does not exist.".format(template_type, template))
        exit(1)

    assert template in available_templates

    template_path = THIS_DIR / template_type / template

    hitchqs_settings = load(template_path.joinpath("hitchqs.yml").text()).data

    project_path_name = hitchqs_settings["path"]

    if Path(project_path_name).exists():
        print(
            (
                "Directory '{}' already exists here, "
                "remove it to run quickstart again."
            ).format(project_path_name)
        )
        exit(1)
    cwd = Path(os.getcwd()).abspath()
    dirtemplate.DirTemplate(
        src=template_path, dest=cwd / project_path_name
    ).ignore_files("hitchqs.yml").ensure_built()
    Path(cwd / project_path_name / "fingerprint.txt").remove()
    print("Quickstart run successfully!")
Example #5
0
 def from_yaml(
     cls, filepath: typing.Union[str, pathlib.Path, typing.IO[bytes]]
 ) -> "Categorization":
     """Read Categorization from a StrictYaml file."""
     try:
         yaml = sy.load(filepath.read(), schema=cls._strictyaml_schema)
     except AttributeError:
         with open(filepath) as fd:
             yaml = sy.load(fd.read(), schema=cls._strictyaml_schema)
     return cls.from_spec(yaml.data)
 def test_operations_yaml_schema(self):
     try:
         load(
             Path("../operations.yaml").bytes().decode('utf8'),
             MapPattern(Str(), Map({
                 "command": Str(),
                 "tags": Str()
             })))
     except YAMLError as error:
         self.fail("Something wrong with your schema")
Example #7
0
def load_config_data(path: str) -> dict:
    yaml_string = Path(path).read_text()
    cfg_without_schema = load(yaml_string, schema=None)
    schema_version = int(cfg_without_schema["format_version"])
    if schema_version not in SCHEMA_FORMAT_VERSION_TO_SCHEMA:
        raise Exception(f"Unsupported schema format version: {schema_version}.")

    strict_cfg = load(yaml_string, schema=SCHEMA_FORMAT_VERSION_TO_SCHEMA[schema_version])
    cfg: dict = strict_cfg.data
    return cfg
Example #8
0
def parse_args():
    # Get config file as required arguemnt and load
    f = argument.Arguments()
    f.always("config", help="Machine Config file name")
    arguments, errors = f.parse()

    if arguments.get("config") is not None:
        machine_config = strictyaml.load(
            Path("machine_config/%s.yaml" %
                 arguments.get("config")).bytes().decode('utf8')).data
        if machine_config.get("currency"):

            schema = Map({"denominations": Seq(Str())})
            notes_config = strictyaml.load(
                Path("machine_config/notes_config/%s.yaml" %
                     machine_config.get("currency")).bytes().decode('utf8'),
                schema).data

        else:
            print("Currency must be specified")
            exit(0)
    else:
        print("Config file must be specified")
        exit(0)
    valid_true_values = [
        'true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly'
    ]
    config = Config()
    config.NAME = machine_config.get("name")
    config.BASE_CURRENCY = machine_config.get("currency")
    config.DEBUG = machine_config.get("debug").lower() in valid_true_values
    config.CAMERA_METHOD = machine_config.get("camera_method")
    config.ZBAR_VIDEO_DEVICE = machine_config.get("camera_device")
    config.RELAY_METHOD = machine_config.get("relay_method")
    config.MOCK_VALIDATOR = machine_config.get(
        "mock_validator").lower() in valid_true_values
    config.ZMQ_URL_MOCK_VALIDATOR = machine_config.get(
        "zmq_url_mock_validator")
    config.NOTE_VALIDATOR_NV11 = machine_config.get(
        "validator_nv11").lower() in valid_true_values
    config.VALIDATOR_PORT = machine_config.get("validator_port")
    config.ZMQ_URL_PRICEFEED = machine_config.get("zmq_url_pricefeed")
    config.NOTES_VALUES = notes_config.get("denominations")
    config.ZMQ_URL_RPC = machine_config.get("zmq_url_rpc")
    config.ZMQ_URL_STATUS = machine_config.get("zmq_url_status")
    config.IS_FULLSCREEN = machine_config.get(
        "is_fullscreen").lower() in valid_true_values
    config.DEFAULT_SLIPPAGE = machine_config.get("default_slippage")
    config.BUY_LIMIT = int(machine_config.get("buy_limit"))

    if not os.uname()[4].startswith("arm"):
        config.RELAY_METHOD = RelayMethod.NONE

    return config
Example #9
0
def load():
    with codecs.open(os.path.join(DATA_PATH, LABS_FILENAME),
                     encoding='utf-8') as f:
        labs_yaml = sy.load(f.read(), LABS_SCHEMA, label=f.name)
        labs = labs_yaml['labs'].data

    for lab_id, lab in labs.items():
        with codecs.open(os.path.join(DATA_PATH, lab_id, PROJECTS_FILENAME),
                         encoding='utf-8') as f:
            projects_yaml = sy.load(f.read(), PROJECTS_SCHEMA, label=f.name)
        lab['projects'] = projects_yaml['projects'].data

    return labs
Example #10
0
    def from_file(
        cls,
        yaml_path: str,
        label: str = "run config",
        validate: bool = True,
        add_defaults: bool = True,
    ) -> "PipelineConfig":
        """Create a PipelineConfig object from a run configuration YAML file.

        Args:
            yaml_path: Path to the run config YAML file.
            label: A label for the config object that will be used in error messages.
                Default is "run config".
            validate: Perform config schema validation immediately after loading
                the config file. If set to False, the full schema validation
                will not be performed until PipelineConfig.validate() is
                explicitly called. The inputs are always validated regardless.
                Defaults to True.
            add_defaults: Add missing configuration parameters using configured
                defaults. The defaults are read from the Django settings file.
                Defaults to True.

        Raises:
            PipelineConfigError: The run config YAML file fails schema validation.

        """
        schema = PipelineConfig.SCHEMA if validate else yaml.Any()
        with open(yaml_path) as fh:
            config_str = fh.read()
        try:
            config_yaml = yaml.load(config_str, schema=schema, label=label)
        except yaml.YAMLValidationError as e:
            raise PipelineConfigError(e)

        if add_defaults:
            # make a template config based on defaults
            config_defaults_str = make_config_template(
                cls.TEMPLATE_PATH,
                **settings.PIPE_RUN_CONFIG_DEFAULTS,
            )
            config_defaults_dict: Dict[str, Any] = yaml.load(
                config_defaults_str).data

            # merge configs
            config_dict = dict_merge(config_defaults_dict, config_yaml.data)
            config_yaml = yaml.as_document(config_dict,
                                           schema=schema,
                                           label=label)
        return cls(config_yaml)
 def test_contains_missing_input_output_disallows_conditions(self, condition):
     sparse_yaml_input_str = input_requirements_yaml(self.field, condition, [Values.REQUIRED])
     sparse_yaml_output_str = output_requirements_yaml(self.field, condition, [Values.ALWAYS])
     for yaml_str in (sparse_yaml_input_str, sparse_yaml_output_str):
         parsed_yaml = load(yaml_str, get_type_schema_yaml_validator())
         with pytest.raises(YAMLValidationError):
             revalidate_typeschema(parsed_yaml)
Example #12
0
    def __init__(self, file: str) -> None:
        # Define configuration schema
        # Read configuration parameters
        p = Path.home() / file
        if not p.is_file():
            logger.critical(_("File %s does not exist"), str(p))
            raise MissingConfigurationFile

        yaml_text = p.read_text()
        try:
            logger.info(_("Loading YAML configuration %s"), file)
            self._config = load(yaml_text, _ConfSchema).data
        except YAMLValidationError:
            logger.critical(_("Incorrect content in YAML configuration %s"),
                            file)
            logger.critical(_("%s"), sys.exc_info()[1])
            raise
        except YAMLError:  # pragma: no cover
            logger.critical(_("Error while reading YAML configuration %s"),
                            file)
            raise

        self._ctrl_list = {}  # type: _ConfType
        for ctrl in self._config["controler"]:
            self._ctrl_list[ctrl] = cast(_ConfType,
                                         EvnCtrlConf(ctrl, self._config))

        self._site_list = {}  # type: _ConfType
        for site in self._config["site"]:
            self._site_list[site] = EvnSiteConf(site, self._config)
    def test_sparsity_output_only_single_value(self):
        condition = Conditions.EQUALS
        sparse_yaml_str = output_requirements_yaml(self.field, condition, Values.output_values())

        parsed_yaml = load(sparse_yaml_str, get_type_schema_yaml_validator())
        with pytest.raises(YAMLValidationError):
            revalidate_typeschema(parsed_yaml)
 def yaml_str_to_schema_dict(yaml_str: str) -> dict:
     """this emulates how we cast a yaml to a dict for validation in
     `datarobot_drum.drum.common.read_model_metadata_yaml` and these assumptions
     are tested in: `tests.drum.test_units.test_read_model_metadata_properly_casts_typeschema` """
     schema = load(yaml_str, get_type_schema_yaml_validator())
     revalidate_typeschema(schema)
     return schema.data
Example #15
0
def docgen():
    """
    Generate documentation.
    """
    from jinja2.environment import Environment
    from jinja2 import DictLoader
    from strictyaml import load

    docs = DIR.gen.joinpath("docs")
    if docs.exists():
        docs.rmtree()
        docs.mkdir()

    stories = _storybook({}).non_variations().ordered_by_name()
    env = Environment()
    env.loader = DictLoader(
        load((DIR.key/"doctemplates.yml").bytes().decode('utf8')).data
    )

    for story in stories:
        if story.info['docs']:
            doc = docs.joinpath("{0}.rst".format(story.info['docs']))
            if not doc.dirname().exists():
                doc.dirname().makedirs()
            doc.write_text(env.get_template("document").render(
                story=story,
            ))
Example #16
0
def generate(src, name, description):
    """Generate a lsrules JSON from files in src directory."""
    rules = []
    srcs = []

    for prefix, _dirs, files in os.walk(src):
        for filename in sorted(files, key=_natural_keys):
            if filename.endswith(".yml"):
                path = os.path.join(prefix, filename)
                srcs.append(path)

    for path in srcs:
        with open(path) as f:
            rs = strictyaml.load(f.read(), SCHEMA_RULE, label=path)
            for r in rs:
                rule = copy.copy(r.data)
                if "remote-addresses" in rule:
                    rule["remote-addresses"] = ",".join(
                        rule["remote-addresses"])
                for field in ("remote-addresses", "ports", "protocol"):
                    if field in rule:
                        rule[field] = str(rule[field]).replace(" ", "")
                rules.append(rule)

    return json.dumps(
        {
            "name": name,
            "description": description,
            "rules": rules
        },
        indent=4,
        sort_keys=True,
    )
Example #17
0
    def should_be_equal_to(self, rhs):
        """
        Code should be equal to rhs
        """
        class UnexpectedException(Exception):
            pass

        error_path = self.path.state.joinpath("error.txt")
        runpy = self.path.state.joinpath("runmypy.py")
        if error_path.exists():
            error_path.remove()
        env = Environment()
        env.loader = DictLoader(
            load(
                self.path.key.joinpath("codetemplates.yml").bytes().decode(
                    'utf8')).data)
        runpy.write_text(
            env.get_template("shouldbeequal").render(
                setup=self.preconditions['setup'],
                code=self.preconditions['code'],
                variables=self.preconditions.get('variables', None),
                yaml_snippet=self.preconditions.get("yaml_snippet"),
                modified_yaml_snippet=self.preconditions.get(
                    "modified_yaml_snippet"),
                rhs=rhs,
                error_path=error_path,
            ))
        self.python(runpy).run()
        if error_path.exists():
            raise UnexpectedException(error_path.bytes().decode("utf8"))
def test_push(config_yaml):
    config = strictyaml.load(config_yaml, MODEL_CONFIG_SCHEMA).data

    version_mocks()
    mock_post_blueprint()
    mock_get_model(model_type=config["type"])
    mock_get_env()
    mock_train_model()
    push_fn = _push_training if config[
        "type"] == "training" else _push_inference
    push_fn(config, code_dir="", endpoint="http://Yess", token="okay")

    calls = responses.calls
    assert (calls[1].request.path_url
            == "/customModels/{}/versions/".format(modelID)
            and calls[1].request.method == "POST")
    if push_fn == _push_training:
        assert (calls[2].request.path_url
                == "/customModels/{}/".format(modelID)
                and calls[2].request.method == "GET")
        assert (calls[3].request.path_url == "/customTrainingBlueprints/"
                and calls[3].request.method == "POST")
        if "trainingModel" in config:
            assert (calls[4].request.path_url == "/projects/abc123/models/"
                    and calls[4].request.method == "POST")
            assert len(calls) == 6
        else:
            assert len(calls) == 4
    else:
        assert len(calls) == 2
Example #19
0
def load_scheme(yaml_text):
    '''
    Some optional keys have enforced default values, otherwise use dict.get()
    '''
    schema = MapPattern(
        Str(),
        Map({
            'description':
            Str(),
            Optional('alias'):
            Str(),
            'version':
            Str(),
            'directives':
            MapPattern(
                Str(),
                Map({
                    Optional('description'):
                    Str(),
                    'formats':
                    Seq(Enum(FORMATS)),
                    Optional('prepare'):
                    Map({
                        Optional('remove_whitespace', default=False): Bool(),
                        Optional('remove_characters', default=['']):
                        Seq(Str()),
                        Optional('strip_characters', default=['']): Seq(Str()),
                    }),
                    Optional('validate'):
                    Map({
                        Optional('alphabet'): Enum(ALPHABETS),
                        Optional('min_length'): Int(),
                        Optional('max_length'): Int(),
                    }),
                    Optional('target'):
                    Str(),
                    Optional('helper', default=False):
                    Bool()
                }),
            ),
            'algorithm':
            Enum(ALGORITHMS),
            'encodings':
            MapPattern(
                Str(),
                Map({
                    'type':
                    Enum(ENCODINGS),
                    Optional('length', default=0):
                    Int(),
                    Optional('prefix', default=''):
                    Str(),
                    Optional('separator'):
                    Map({
                        'character': Str(),
                        'interval': Int()
                    })
                }))
        }))
    return load(yaml_text, schema)
Example #20
0
def _load_yaml_with_schema(filepath, schema):
  """Loads an instance YAML file and parses
  it based on a syaml-formatted YAML schema.

  Args:
    filepath: filepath location of the YAML file
    schema: YAML schema in syaml format

  Returns:
    Returns the parsed YAML data in a strictyaml-provided datastructure
    which is similar to a Python dictionary.
  """
  yaml_file = open(filepath)
  content = yaml_file.read()
  yaml_file.close()

  try:
    parsed = syaml.load(content, schema)

    return parsed
  except (ruamel.yaml.parser.ParserError,
          ruamel.yaml.scanner.ScannerError,
          syaml.exceptions.YAMLValidationError,
          syaml.exceptions.DuplicateKeysDisallowed,
          syaml.exceptions.InconsistentIndentationDisallowed) as exception:
    print(exception)
    sys.exit(0)
Example #21
0
    def __init__(self, config_filepath):
        with open(config_filepath, 'r') as stream:
            self.dictionary = load(stream.read(), self._schema).data

        # Some sanity checks.
        assert self.dictionary['simulation'][
            'Ts'] == 1, "Only 1 ms time-step is tested"
        assert self.dictionary['simulation'][
            'tSample'] == 100, "Only 100 ms available"
        assert self.dictionary['simulation']['tSample'] > self.dictionary[
            'simulation']['tStartLoss']
        assert os.path.exists(self.dictionary['model']['CkptFile'])
        assert self.dictionary['batchsize'] >= 1

        model_dtype_str = self.dictionary['model']['dtype']
        if model_dtype_str is None:
            self.dictionary['model']['dtype'] = torch.float32
        else:
            self.dictionary['model']['dtype'] = self._str_to_dtype[
                model_dtype_str]

        self.dictionary['hardware']['gpuDevice'] = torch.device(
            'cuda:{}'.format(self.dictionary['hardware']['gpuDevice']))

        if self.dictionary['hardware']['readerThreads'] is None:
            self.dictionary['hardware']['readerThreads'] = os.cpu_count()
Example #22
0
 def __init__(self, yaml_contents: str) -> None:
     self.config = strictyaml.load(yaml_contents, SCHEMA)
     self.devices = [
         Device.create(device["name"], device["driver"].data,
                       device["args"].data)
         for device in self.config["devices"]
     ]
    def test_contains_missing_output_disallowed_values(self, value):
        condition = Conditions.EQUALS
        sparse_yaml_str = output_requirements_yaml(self.field, condition, [value])

        parsed_yaml = load(sparse_yaml_str, get_type_schema_yaml_validator())
        with pytest.raises(YAMLValidationError):
            revalidate_typeschema(parsed_yaml)
Example #24
0
def load_config_from_file(config_file: Path):  # pragma: no cover
    schema = strictyaml.Map({
        'email_address': strictyaml.Email(),
        'use_keyring': strictyaml.Bool(),
    })

    return strictyaml.load(config_file.read_text(), schema).data
Example #25
0
    def should_be_equal_to(self, rhs):
        """
        Code should be equal to rhs
        """
        class UnexpectedException(Exception):
            pass

        error_path = self.path.state.joinpath("error.txt")
        runpy = self.path.state.joinpath("runmypy.py")
        if error_path.exists():
            error_path.remove()
        env = Environment()
        env.loader = DictLoader(load(self.path.key.joinpath("codetemplates.yml").bytes().decode('utf8')).data)
        runpy.write_text(env.get_template("shouldbeequal").render(
            setup=self.preconditions['setup'],
            code=self.preconditions['code'],
            variables=self.preconditions.get('variables', None),
            yaml_snippet=self.preconditions.get("yaml_snippet"),
            modified_yaml_snippet=self.preconditions.get("modified_yaml_snippet"),
            rhs=rhs,
            error_path=error_path,
        ))
        self.python(runpy).run()
        if error_path.exists():
            raise UnexpectedException(error_path.bytes().decode("utf8"))
Example #26
0
def upload_dataset_yaml(yamlfile: str):
    """
    Same as upload dataset
    Arguments specified in yaml files
    """

    with open(yamlfile) as f:
        yaml_string = f.read()

    try:
        params = load(yaml_string=yaml_string, schema=datasetSchema)
    except YAMLValidationError as e:
        print(e)
        raise typer.Exit()

    params = params.data

    rucio_client = Client()
    dataset_path = params["specs"]["datasetPath"]
    dataset_name = params["specs"]["datasetName"]
    rule_params = params["specs"]["options"]["rule"]
    upload_params = params["specs"]["options"]["upload"]

    upload_dataset_and_create_rule(rucio_client, dataset_path, dataset_name,
                                   rule_params, upload_params)
Example #27
0
def load_config(config: str, config_vars: Dict[str, str]) -> YAML:
    """Loads config and return a Dictionary of the data"""
    environment = Environment(undefined=StrictUndefined)
    template = environment.from_string(config)
    rendered_config = template.render(vars=config_vars)

    return load(rendered_config, schema=generate_schema())
Example #28
0
    def raises_exception(self, exception):
        """
        Expect an exception.
        """
        class ExpectedExceptionDidNotHappen(Exception):
            pass

        error_path = self.path.state.joinpath("error.txt")
        runpy = self.path.state.joinpath("runmypy.py")
        if error_path.exists():
            error_path.remove()
        env = Environment()
        env.loader = DictLoader(load(self.path.key.joinpath("codetemplates.yml").bytes().decode('utf8')).data)
        runpy.write_text(env.get_template("raises_exception").render(
            setup=self.preconditions['setup'],
            code=self.preconditions['code'],
            variables=self.preconditions.get('variables', None),
            yaml_snippet=self.preconditions.get("yaml_snippet"),
            modified_yaml_snippet=self.preconditions.get("modified_yaml_snippet"),
            exception=exception,
            error_path=error_path,
        ))
        self.python(runpy).run()
        if not error_path.exists():
            raise ExpectedExceptionDidNotHappen()
        else:
            assert exception.strip() in error_path.bytes().decode('utf8'), "expected:\n{0}\nshould be:\n{1}".format(
                exception,
                error_path.bytes().decode('utf8'),
            )
Example #29
0
 def __init__(self, file_or_yaml=open('config.yaml', 'r')):
     if isinstance(file_or_yaml, str):
         yaml_str = file_or_yaml
     else:
         yaml_str = file_or_yaml.read()
         file_or_yaml.close()
     self.cfg = load(yaml_str, Config.SCHEMA).data
Example #30
0
 def __init__(self, port, filename):
     assert type(port) is int, "port must be integer"
     assert 1024 < port < 65535, "port number must be between 1024 and 65535"
     self._port = port
     self._filename = filename
     self._data = load(Path(filename).bytes().decode('utf8'), SCHEMA)
     self._name = None
Example #31
0
def get_testcase_options(testcase):
    # options are optional
    options = {}
    if os.path.isfile(testcase):
        with open(testcase, 'r') as f:
            options = strictyaml.load(f.read(), options_schema).data

    return options
Example #32
0
def rot():
    """
    Test for code rot by upgrading dependency and running tests (ruamel.yaml).
    """
    print("Checking code rot for strictyaml project...")
    latest_version = requests.get(
        "https://pypi.python.org/pypi/ruamel.yaml/json"
    ).json()['info']['version']
    base_story = load(DIR.key.joinpath("strictyaml.story").bytes().decode("utf8"))
    latest_tested_version = str(base_story['strictyaml']['params']['ruamel version'])

    if latest_version != latest_tested_version:
        base_story['strictyaml']['params']['ruamel version'] = load(latest_version)
        DIR.key.joinpath("strictyaml.story").write_text(base_story.as_yaml())
        regression()
    else:
        print("No dependency changes")
Example #33
0
    def __init__(self):
        gdb.Command.__init__(self, "pp", gdb.COMMAND_NONE)

        config_path = os.path.join(os.path.dirname(__file__), "pp-mozilla.yaml")
        with open(config_path, "r") as f:
            # This recursively flattens the YAML instances to dicts and lists
            # and scalars.
            self.mapping = load(f.read()).data
Example #34
0
def load_config():
    schema = strictyaml.Map({
        'email_address': strictyaml.Email(),
    })

    root_dir = path.abspath(path.dirname(__file__))
    with open(path.join(root_dir, 'config.yaml')) as config_file:
        return strictyaml.load(config_file.read(), schema).data
Example #35
0
    def load_config(self):
        config_paths = [self.config_path, self.config_path_fallback]
        config_files = [p for p in config_paths if os.path.exists(p)]

        if not any(config_files):
            self.logger.error('Config file not found (load_config() failed!)')
            return

        data = open(config_files[0], 'r').read()
        config = yaml.load(data, CommentedMap)

        for key, value in config.items():
            self.config[key] = value
Example #36
0
    def __call__(self, name):
        if self._override_selectors is not None:
            with open(self._override_selectors) as handle:
                selectors = strictyaml.load(handle.read()).data

            if name in selectors.keys():
                selector = selectors[name]

                if isinstance(selector, str):
                    return CSSSelector(selectors[name])
                else:
                    if "xpath" in selector.keys():
                        return XPathSelector(selector['xpath'])
        return self.default(name)
Example #37
0
    def __init__(self, filename):
        """Load config from YAML file."""
        filename = path.abspath(filename)

        if filename is None:
            self._config = []
        else:
            try:
                with open(filename, 'r') as handle:
                    self._yaml = handle.read()

                self._config = load(
                    self._yaml,
                    Seq(
                        Map({
                            Optional("name"): "name",
                            "request": Map({
                                Optional("path"): Str(),
                                Optional("method"): Enum([
                                    "get", "post", "put", "delete",
                                    "GET", "POST", "PUT", "DELETE",
                                ]),
                                Optional("headers"): MapPattern(Str(), Str()),
                                Optional("data"): Str(),
                            }),
                            "response": Map({
                                "content": Str() | Map({"file": Str()}),
                                Optional("code"): Int(),
                                Optional("headers"): MapPattern(Str(), Str()),
                            }),
                        })
                    )
                )
            except Exception as e:
                sys.stderr.write(
                    "Error reading YAML config file: {0}\n".format(str(e))
                )
                sys.exit(1)

            # Read and store all references to external content files
            for pair in self._config:
                content = pair.get('response', {}).get('content')
                if type(content) != str and "file" in content:
                    with open(
                        path.join(path.dirname(filename), content['file']), 'r'
                    ) as content_file_handle:
                        pair['response']['content'] = \
                            content_file_handle.read()
Example #38
0
def _personal_settings():
    settings_file = DIR.key.joinpath("personalsettings.yml")

    if not settings_file.exists():
        settings_file.write_text(
            (
                "engine:\n"
                "  rewrite: no\n"
                "  cprofile: no\n"
                "params:\n"
                "  python version: 3.7.0\n"
            )
        )
    return load(
        settings_file.bytes().decode("utf8"),
        Map(
            {
                "engine": Map({"rewrite": Bool(), "cprofile": Bool()}),
                "params": Map({"python version": Str()}),
            }
        ),
    )