Exemple #1
0
def yaml_to_json(rc):
    """Converts YAML to JSON"""
    from regolith import fsclient
    for inp in rc.files:
        base, ext = os.path.splitext(inp)
        out = base + '.json'
        fsclient.yaml_to_json(inp, out)
Exemple #2
0
def import_yamls(dbpath: str,
                 dbname: str,
                 host: str = None,
                 uri: str = None) -> None:
    """Import the yaml files to mongo db.

    Each yaml file will be a collection in the database. The _id will be the id_key for each doc in the yaml file.

    Parameters
    ----------
    dbpath : str
        The path to the db folder.

    dbname : str
        The name of the database in mongo.

    host : str
        The hostname or IP address or Unix domain socket path of a single mongod or mongos instance to connect
        to, or a mongodb URI, or a list of hostnames / mongodb URIs.

    uri : str
        Specify a resolvable URI connection string (enclose in quotes) to connect to the MongoDB deployment.
    """
    yaml_files = itertools.chain(
        Path(dbpath).glob('*.yaml'),
        Path(dbpath).glob('*.yml'))
    with TemporaryDirectory() as tempd:
        for yaml_file in yaml_files:
            json_file = Path(tempd).joinpath(
                yaml_file.with_suffix('.json').name)
            loader = YAML(typ='safe')
            loader.constructor.yaml_constructors[u'tag:yaml.org,2002:timestamp'] = \
                loader.constructor.yaml_constructors[u'tag:yaml.org,2002:str']
            fsclient.yaml_to_json(str(yaml_file),
                                  str(json_file),
                                  loader=loader)
        import_jsons(tempd, dbname, host=host, uri=uri)
    return