Exemple #1
0
def dict_to_yaml(data, width=None, sort=False):
    """ Convert dictionary into yaml """
    output = StringIO()

    # Set formatting options
    yaml = YAML()
    yaml.indent(mapping=4, sequence=4, offset=2)
    yaml.default_flow_style = False
    yaml.allow_unicode = True
    yaml.encoding = 'utf-8'
    yaml.width = width

    # Make sure that multiline strings keep the formatting
    data = copy.deepcopy(data)
    scalarstring.walk_tree(data)

    # Sort the data https://stackoverflow.com/a/40227545
    if sort:
        sorted_data = CommentedMap()
        for key in sorted(data):
            sorted_data[key] = data[key]
        data = sorted_data

    yaml.dump(data, output)
    return output.getvalue()
def update_environment_yml():
    """Update conda_dev_env.yml file for conda."""
    import re

    from ruamel.yaml import YAML
    from ruamel.yaml.comments import CommentedMap, CommentedSeq

    environment_filename = "conda_dev_env.yml"

    cmap = CommentedMap()
    cmap.yaml_set_start_comment(
        ("Usage: conda env create -n myenvname -f {} python=3.6\n"
         "       conda activate myenvname\n"
         "       pip install --no-deps -e .".format(environment_filename)))
    cmap["name"] = "aiida_crystal17"
    cmap["channels"] = CommentedSeq(["conda-forge", "cjs14"])
    cmap["channels"].yaml_add_eol_comment("for sqlalchemy-diff and pgtest", 1)
    cmap["dependencies"] = dmap = CommentedSeq()

    # additional packages
    dmap.append("pip")
    dmap.append("aiida-core.services")

    # fix incompatibilities between conda and pypi
    replacements = {"pre-commit": "pre_commit"}
    setup_json = get_setup_json()

    for base, key in [
        (None, "install_requires"),
        ("extras_require", "testing"),
        ("extras_require", "code_style"),
        ("extras_require", "docs"),
    ]:
        requirements = setup_json.get(base, setup_json)[key]
        count = 0
        for req in sorted(requirements, key=lambda x: x.lower()):
            # skip packages required for specific python versions < 3
            if re.findall("python_version\\s*\\<\\s*\\'?3", req):
                continue
            req = req.split(";")[0]
            for (regex, replacement) in iter(replacements.items()):
                req = re.sub(regex, replacement, req)
            count += 1
            dmap.append(req.lower())

        dmap.yaml_set_comment_before_after_key(len(dmap) - count, before=key)

    yaml = YAML(typ="rt")
    yaml.default_flow_style = False
    yaml.encoding = "utf-8"
    yaml.allow_unicode = True
    file_path = os.path.join(ROOT_DIR, environment_filename)
    with open(file_path, "w") as env_file:
        yaml.dump(cmap, env_file)
def update_environment_yml():
    """
    Updates conda_dev_env.yml file for conda.
    """
    import re
    from ruamel.yaml.comments import CommentedMap, CommentedSeq
    from ruamel.yaml import YAML

    environment_filename = 'conda_dev_env.yml'

    cmap = CommentedMap()
    cmap.yaml_set_start_comment(
        'Usage: conda env create -n myenvname -f {} python=3.6'.format(
            environment_filename))
    cmap['name'] = 'aiida_icl'
    cmap['channels'] = CommentedSeq(['conda-forge', 'cjs'])
    cmap['channels'].yaml_add_eol_comment('for sqlalchemy-diff and pgtest', 1)
    cmap['dependencies'] = dmap = CommentedSeq()

    # fix incompatibilities between conda and pypi
    replacements = {}
    setup_json = get_setup_json()

    for base, key in [(None, 'install_requires'),
                      ('extras_require', 'testing'),
                      ('extras_require', 'code_style')]:
        requirements = setup_json.get(base, setup_json)[key]
        count = 0
        for req in sorted(requirements, key=lambda x: x.lower()):
            # skip packages required for specific python versions < 3
            if re.findall("python_version\\s*\\<\\s*\\'?3", req):
                continue
            req = req.split(';')[0]
            for (regex, replacement) in iter(replacements.items()):
                req = re.sub(regex, replacement, req)
            count += 1
            dmap.append(req.lower())

        dmap.yaml_set_comment_before_after_key(len(dmap) - count, before=key)

    yaml = YAML(typ='rt')
    yaml.default_flow_style = False
    yaml.encoding = 'utf-8'
    yaml.allow_unicode = True
    file_path = os.path.join(ROOT_DIR, environment_filename)
    with open(file_path, 'w') as env_file:
        yaml.dump(cmap, env_file)
Exemple #4
0
def write_yaml(filename:str, dictionary:dict):
    """ Function to convert a dictionary into a YAML file
    """

    yml = YAML()
    yml.explicit_start = True
    yml.default_flow_style = False 
    yml.encoding = "utf-8"     # default when using YAML() or YAML(typ="rt")
    yml.allow_unicode = True   # always default in the new API
    yml.errors = "strict"
    yml.indent(sequence=4, offset=2)
    yml.explicit_end = True

    if isinstance(dictionary,dict):
        with open(filename, 'w') as outfile:
            print(filename)
            yml.dump(dictionary, outfile)

    else:
       raise Exception('its not a dictionary')
Exemple #5
0
    s = re.sub(r"\'\'", r"'",
               s)  # remove the double single quotes from ''hashi_vault''
    s = re.sub(
        r"(\'\"|\"\')", r'"', s
    )  # remove '" or "' from the begining and end of ansible_password section
    s = re.sub(r"\'(\d*)\'", r"\1",
               s)  # remove the single quotes from '0:' and '1:'
    return s


yaml = YAML(typ='safe')
yaml.default_flow_style = False
for root, dirs, files in os.walk("./"):
    for file in files:
        if file.endswith(".json"):
            dir = root.split(os.path.sep)[-1]
            if dir != "terraform":
                with open(root + '/' + file, 'r') as stream:
                    try:
                        filename = os.path.splitext(file)[0]
                        with open(root + '/' + filename + '.yml',
                                  'w') as outfile:
                            #yaml.dump(json.load(stream), outfile)
                            yaml.encoding = None
                            yaml.dump(yaml.load(stream), outfile, transform=tr)
                            print('Yaml file created: ' + root + '/' +
                                  filename + '.yml')
                            os.remove(root + '/' + file)
                    except:
                        print("There is an issue...")
#Network request serve settings
class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.path = GEN_FILE
        return http.server.SimpleHTTPRequestHandler.do_GET(self)


PORT = 8080
HANDLER = RequestHandler

#Yaml configuration
YML = YAML()

YML.indent(mapping=2, sequence=4, offset=2)
YML.encoding = "utf-8"
YML.allow_unicode = True


def get_config():
    with open(MAIN_DIR + '/config/cloud-config.yml', 'r') as sys_config_file:
        sys_config = sys_config_file.read()
    return YML.load(sys_config)


def write_keys(sys_yaml):
    if not 'ssh_authorized_keys' in sys_yaml:
        sys_yaml['ssh_authorized_keys'] = list()
    try:
        with open(MAIN_DIR + '/config/pub_keys', 'r') as pub_key_file:
            for key in pub_key_file: