Exemple #1
0
def make_entities_yml(agent_folder, lang):
    path = os.path.join(agent_folder, "entities")
    try:
        entity_dic = {}
        for f in os.listdir(path):
            if "entries" in f:
                continue
            entity_name = f.replace(".json", "")
            pair_file = f.replace(".json", "_entries_" + lang + ".json")

            file_path1 = os.path.join(path, f)
            with open(file_path1) as json_file1:
                json_obj = json.load(json_file1)
                filtered_obj = filtering_info(json_obj, template.entity_info)

            file_path2 = os.path.join(path, pair_file)
            with open(file_path2) as json_file2:
                json_obj = json.load(json_file2)
                filtered_obj["data"] = json_obj
            entity_dic[entity_name] = filtered_obj
    except FileNotFoundError:
        entity_dic = template.entity_yaml

    with open(os.path.join(agent_folder, 'entities.yml'), 'w') as yaml_file:
        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.dump(entity_dic, yaml_file)
Exemple #2
0
def save_yaml(f, d: Mapping) -> None:

    try:

        from ruamel.yaml import YAML

    except ImportError:

        import yaml

        class _IndentDumper(yaml.SafeDumper):
            def increase_indent(self, flow=False, indentless=False):

                return super().increase_indent(flow, False)

        kwargs = {
            'Dumper': _IndentDumper,
            'encoding': 'utf-8',
        }

    else:

        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        kwargs = {}

    yaml.dump(d, f, **kwargs)
Exemple #3
0
def make_intents_yml(agent_folder, lang):
    path = os.path.join(agent_folder, "intents")
    intent_dic = {}

    for f in os.listdir(path):
        if "usersays" in f:
            continue
        filtered_info = {}
        filtered_phrases = {}
        contents = {}
        intent_name = f.replace(".json", "")
        pair_file = f.replace(".json", "_usersays_" + lang + ".json")

        file_path = os.path.join(path, f)
        filtered_info = filter_intent_info(file_path)
        contents.update(filtered_info)

        if os.path.exists(os.path.join(path, pair_file)):
            file_path = os.path.join(path, pair_file)
            filtered_phrases = filter_intent_usersays(file_path)
            contents.update(filtered_phrases)
        intent_dic[intent_name] = contents

    with open(os.path.join(agent_folder, 'intents.yml'), 'w') as yaml_file:
        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.dump(intent_dic, yaml_file)
Exemple #4
0
def save_yaml(outdir, fname, data_out):

    if not os.path.isdir(outdir) and outdir!='':
        os.makedirs(outdir)
    fname = os.path.join(outdir, fname)

    data_out = remove_numpy(data_out)

    f = open(fname, "w")
    yaml=ry.YAML()
    yaml.default_flow_style = None
    yaml.width = float("inf")
    yaml.indent(mapping=4, sequence=6, offset=3)
    yaml.dump(data_out, f)
Exemple #5
0
def parse_rule(rule):
    """Display the source code of the user-selected rule."""
    name = STYLES / rule["extends"] / rule["ID"]
    path = str(name) + ".yml"

    yaml = ruamel.yaml.YAML()
    yaml.indent(mapping=2, sequence=4, offset=2)
    yaml.preserve_quotes = True

    buf = io.StringIO()
    with open(path, "r") as f:
        data = yaml.load(f)
        desc = data["description"]
        del data["description"]
        yaml.dump(data, buf)
        return buf.getvalue(), desc
Exemple #6
0
def make_etc_yml(agent_folder):
    agent_file = os.path.join(agent_folder, "agent.json")
    package_file = os.path.join(agent_folder, "package.json")

    with open(agent_file) as f1:
        agent_dic = json.load(f1)
    with open(os.path.join(agent_folder, 'agent.yml'), 'w') as yaml_file:
        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.dump(agent_dic, yaml_file)

    with open(package_file) as f2:
        package_dic = json.load(f2)
    with open(os.path.join(agent_folder, 'package.yml'), 'w') as yaml_file:
        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.dump(package_dic, yaml_file)
Exemple #7
0
def save_yaml(outdir, fname, data_out):
    ''' Save yaml file - ripped from WEIS 
    
    Parameters:
    -----------
    outdir: str
        directory to save yaml
    fname: str
        filename for yaml
    data_out: dict
        data to dump to yaml
    '''
    fname = os.path.join(outdir, fname)

    if not os.path.isdir(outdir):
        os.makedirs(outdir)

    f = open(fname, "w")
    yaml = ry.YAML()
    yaml.default_flow_style = None
    yaml.width = float("inf")
    yaml.indent(mapping=4, sequence=6, offset=3)
    yaml.dump(data_out, f)
def write_yaml(mf, app):
    """Write metadata in yaml format.

    Parameters
    ----------
    mf
      active file discriptor for writing
    app
      app metadata to written to the yaml file
    """
    # import rumael.yaml and check version
    try:
        import ruamel.yaml
    except ImportError as e:
        raise FDroidException(
            'ruamel.yaml not installed, can not write metadata.') from e
    if not ruamel.yaml.__version__:
        raise FDroidException(
            'ruamel.yaml.__version__ not accessible. Please make sure a ruamel.yaml >= 0.13 is installed..'
        )
    m = re.match(
        r'(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<patch>[0-9]+)(-.+)?',
        ruamel.yaml.__version__)
    if not m:
        raise FDroidException(
            'ruamel.yaml version malfored, please install an upstream version of ruamel.yaml'
        )
    if int(m.group('major')) < 0 or int(m.group('minor')) < 13:
        raise FDroidException(
            'currently installed version of ruamel.yaml ({}) is too old, >= 1.13 required.'
            .format(ruamel.yaml.__version__))
    # suiteable version ruamel.yaml imported successfully

    _yaml_bools_true = ('y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE',
                        'on', 'On', 'ON')
    _yaml_bools_false = ('n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE',
                         'off', 'Off', 'OFF')
    _yaml_bools_plus_lists = []
    _yaml_bools_plus_lists.extend(_yaml_bools_true)
    _yaml_bools_plus_lists.extend([[x] for x in _yaml_bools_true])
    _yaml_bools_plus_lists.extend(_yaml_bools_false)
    _yaml_bools_plus_lists.extend([[x] for x in _yaml_bools_false])

    def _field_to_yaml(typ, value):
        if typ is TYPE_STRING:
            if value in _yaml_bools_plus_lists:
                return ruamel.yaml.scalarstring.SingleQuotedScalarString(
                    str(value))
            return str(value)
        elif typ is TYPE_INT:
            return int(value)
        elif typ is TYPE_MULTILINE:
            if '\n' in value:
                return ruamel.yaml.scalarstring.preserve_literal(str(value))
            else:
                return str(value)
        elif typ is TYPE_SCRIPT:
            if type(value) == list:
                if len(value) == 1:
                    return value[0]
                else:
                    return value
            else:
                script_lines = value.split(' && ')
                if len(script_lines) > 1:
                    return script_lines
                else:
                    return value
        else:
            return value

    def _app_to_yaml(app):
        cm = ruamel.yaml.comments.CommentedMap()
        insert_newline = False
        for field in yaml_app_field_order:
            if field == '\n':
                # next iteration will need to insert a newline
                insert_newline = True
            else:
                if app.get(field) or field == 'Builds':
                    if field == 'Builds':
                        if app.get('Builds'):
                            cm.update({field: _builds_to_yaml(app)})
                    elif field == 'CurrentVersionCode':
                        cm.update({
                            field:
                            _field_to_yaml(TYPE_INT, getattr(app, field))
                        })
                    elif field == 'AllowedAPKSigningKeys':
                        value = getattr(app, field)
                        if value:
                            value = [str(i).lower() for i in value]
                            if len(value) == 1:
                                cm.update({
                                    field:
                                    _field_to_yaml(TYPE_STRING, value[0])
                                })
                            else:
                                cm.update(
                                    {field: _field_to_yaml(TYPE_LIST, value)})
                    else:
                        cm.update({
                            field:
                            _field_to_yaml(fieldtype(field),
                                           getattr(app, field))
                        })

                    if insert_newline:
                        # we need to prepend a newline in front of this field
                        insert_newline = False
                        # inserting empty lines is not supported so we add a
                        # bogus comment and over-write its value
                        cm.yaml_set_comment_before_after_key(field, 'bogus')
                        cm.ca.items[field][1][-1].value = '\n'
        return cm

    def _builds_to_yaml(app):
        builds = ruamel.yaml.comments.CommentedSeq()
        for build in app.get('Builds', []):
            if not isinstance(build, Build):
                build = Build(build)
            b = ruamel.yaml.comments.CommentedMap()
            for field in build_flags:
                value = getattr(build, field)
                if hasattr(build, field) and value:
                    if field == 'gradle' and value == ['off']:
                        value = [
                            ruamel.yaml.scalarstring.SingleQuotedScalarString(
                                'off')
                        ]
                    if field in ('maven', 'buildozer'):
                        if value == 'no':
                            continue
                        elif value == 'yes':
                            value = 'yes'
                    b.update({field: _field_to_yaml(flagtype(field), value)})
            builds.append(b)

        # insert extra empty lines between build entries
        for i in range(1, len(builds)):
            builds.yaml_set_comment_before_after_key(i, 'bogus')
            builds.ca.items[i][1][-1].value = '\n'

        return builds

    yaml_app = _app_to_yaml(app)
    try:
        yaml = ruamel.yaml.YAML()
        yaml.indent(mapping=4, sequence=4, offset=2)
        yaml.dump(yaml_app, stream=mf)
    except AttributeError:  # Debian/stretch's version does not have YAML()
        ruamel.yaml.round_trip_dump(yaml_app, mf, indent=4, block_seq_indent=2)
def update_values_using_user_input(values):
    yaml = MyYAML()
    yaml.indent(mapping=4, sequence=4, offset=4)
    values = yaml.load(values)
    values = _recursive_update_values(values)
    return yaml.dump(values)
Exemple #10
0
from .widgets import Progress
from .settings import config as settings
from .eve_model import EveModelBase
from .field import SUPPORTED_SCHEMA_FIELDS, TYPE_MAPPING, Validator
from .session import DEFAULT_SESSION_CLASS, EveSessionBase
from .item import EveItem
from .page import EvePage, EvePageCache, PageZero
from .io import FILE_READERS, read_data_file
from .types import DASK_TYPE_MAPPING, COERCERS
from .utils import NumpyJSONENncoder, to_data_dict

try:
    from ruamel.yaml import YAML
    yaml = YAML()
    yaml.indent(mapping=4, sequence=4, offset=2)
except ImportError:
    import yaml

from concurrent.futures import ThreadPoolExecutor


def not_empty(v):
    if v is None:
        return False
    if isinstance(v, (list, dict, tuple)):
        return bool(len(v))
    return True


class EveResource(EveModelBase):
Exemple #11
0
ipam_rule_groups = ["apiextensions.k8s.io", "fic.f5.com"]
non_ipam_rules = []
ipam_rules = []

root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
rbac_dir = root + "/docs/config_examples/rbac/"
helm_clrole_path = root + "/helm-charts/f5-bigip-ctlr/templates/f5-bigip-ctlr-clusterrole.yaml"

with open(rbac_dir + 'clusterrole.yaml', "r") as f:
    resources = list(yaml.load_all(f, yaml.FullLoader))

for res in resources:
    if "rules" in res.keys():
        for rule in res["rules"]:
            if not list(set(ipam_rule_groups) & set(rule["apiGroups"])):
                non_ipam_rules.append(rule)
            else:
                ipam_rules.append(rule)

with open(helm_clrole_path, "w") as file_data:
    file_data.write(cls_templt)
    yaml = ruamel.yaml.YAML()
    yaml.indent(sequence=4, offset=2)
    yaml.dump({"rules": non_ipam_rules}, file_data)

    if ipam_rules:
        file_data.write("{{- if .Values.args.ipam }} \n")
        yaml.dump(ipam_rules, file_data)
        file_data.write("{{- end }}\n")
        file_data.write("{{- end }}\n")