Ejemplo n.º 1
0
def save_yaml(
    dictionary: Dict,
    path: str,
    encoding: str = "utf-8",
    pretty: bool = False,
    sortkeys: bool = False,
) -> None:
    """Save dictionary to YAML file preserving order if it is an OrderedDict

    Args:
        dictionary (Dict): Python dictionary to save
        path (str): Path to YAML file
        encoding (str): Encoding of file. Defaults to utf-8.
        pretty (bool): Whether to pretty print. Defaults to False.
        sortkeys (bool): Whether to sort dictionary keys. Defaults to False.

    Returns:
        None
    """
    with open(path, "w", encoding=encoding) as f:
        representer = representers[sortkeys][pretty]
        yaml = YAML(typ="rt")
        yaml.Representer = representer
        add_representer(OrderedDict,
                        representer.represent_dict,
                        representer=representer)
        if pretty:
            yaml.indent(offset=2)
        else:
            yaml.default_flow_style = None
        yaml.representer.add_representer(type(None),
                                         representer.represent_none)
        yaml.dump(dictionary, f)
Ejemplo n.º 2
0
def get_yaml_loader(typ):
    if typ == "rt":
        loader = YAML(typ=typ)
        loader.preserve_quotes = True
        loader.default_flow_style = False
        loader.indent(sequence=4, offset=2)
        loader.width = 1000
        loader.Representer = MyRepresenter
        loader.Loader = ruamel.yaml.RoundTripLoader
    elif typ == "safe":
        loader = YAML(typ=typ)
    return loader
Ejemplo n.º 3
0
def get_yaml_instance(
    version: VersionType = (1, 2),
    indent: Any = {'mapping': 2, 'sequence': 4, 'offset': 2},
    **kwargs: Any
) -> YAML:
    yaml = YAML(**kwargs)

    yaml.version = version  # type: ignore
    yaml.Representer = CustomRepresenter

    yaml.indent(**indent)

    return yaml
Ejemplo n.º 4
0
def get_yaml_instance(version=(1, 2),
                      indent={
                          'mapping': 2,
                          'sequence': 4,
                          'offset': 2
                      },
                      **kwargs):
    yaml = YAML(**kwargs)

    yaml.Representer = CustomRepresenter

    yaml.version = version
    yaml.indent(**indent)

    return yaml
Ejemplo n.º 5
0
    def yaml_dump(cls, stream=sys.stdout):
        class MyRepresenter(SafeRepresenter):
            def ignore_aliases(self, data):
                return True

        yaml = YAML(typ="safe")
        yaml.default_flow_style = False
        yaml.Representer = MyRepresenter

        types = EntityTypeBase.get_entity_types()

        for _, t in types.items():
            yaml.register_class(t)

        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.dump(cls, stream=stream)
Ejemplo n.º 6
0
    def save(self):
        """Saves the scenes data to file.
        """

        # This is necessary to prevent the representer from making its own
        # yaml aliases.  While aliases are helpful, the computer generated
        # ones would just confuse people.
        class Representer(RoundTripRepresenter):
            def ignore_aliases(self, data):
                return True

        if self.path is not None:
            with open(self.path, "w") as f:
                yaml = YAML()
                yaml.Representer = Representer
                yaml.preserve_quotes = True
                yaml.indent(mapping=2, sequence=4, offset=2)
                yaml.dump(self.data, f)
        else:
            LOG.error("Scenes File not Defined in Config.  Scenes not saved.")
Ejemplo n.º 7
0
class Representer(RoundTripRepresenter):
    pass


Representer.add_representer(OrderedDict, Representer.represent_dict)


def wrap_yaml_string(s, width=100):
    ss = (l.rstrip() for l in s.splitlines())
    ss = (l for l in ss if l)
    #ss = textwrap.wrap('\n'.join(ss), width=width, drop_whitespace=False, tabsize=2)
    return PreservedScalarString('\n'.join(ss))


yaml = YAML(typ='rt')
yaml.Representer = Representer
yaml.compact()
yaml.default_flow_style = False


def yaml_dumps(document):
    stream = StringIO()
    yaml.dump(document, stream)
    return stream.getvalue()


def write_yaml(dir_, fn, data):
    if not os.path.exists(dir_):
        os.makedirs(dir_)
    with open(os.path.join(dir_, fn), 'w') as f:
        yaml.dump(data, f)
Ejemplo n.º 8
0
def main(docname):

    with open(docname, "r") as fi:
        lines = fi.readlines()
    context = {}
    rest_lines = []
    for line in lines:
        # print(line)
        if "{%" in line:
            set_expr = re.search("{%(.*)%}", line)
            set_expr = set_expr.group(1)
            set_expr = set_expr.replace("set", "", 1).strip()
            exec(set_expr, globals(), context)
        else:
            rest_lines.append(line)

    yaml = YAML(typ="rt")
    yaml.preserve_quotes = True
    yaml.default_flow_style = False
    yaml.indent(sequence=4, offset=2)
    yaml.width = 1000
    yaml.Representer = MyRepresenter
    yaml.Loader = ruamel.yaml.RoundTripLoader

    result_yaml = CommentedMap()
    result_yaml["context"] = context

    def has_selector(s):
        return s.strip().endswith("]")

    quoted_lines = []
    for line in rest_lines:
        if has_selector(line):
            selector_start = line.rfind("[")
            selector_end = line.rfind("]")
            selector_content = line[selector_start + 1 : selector_end]

            if line.strip().startswith("-"):
                line = (
                    line[: line.find("-") + 1]
                    + f" sel({selector_content}): "
                    + line[
                        line.find("-") + 1 : min(line.rfind("#"), line.rfind("["))
                    ].strip()
                    + "\n"
                )
        quoted_lines.append(line)
    rest_lines = quoted_lines

    def check_if_quoted(s):
        s = s.strip()
        return s.startswith('"') or s.startswith("'")

    quoted_lines = []
    for line in rest_lines:
        if "{{" in line:
            # make sure that jinja stuff is quoted
            if line.find(":") != -1:
                idx = line.find(":")
            elif line.strip().startswith("-"):
                idx = line.find("-")
            rest = line[idx + 1 :]

            if not check_if_quoted(rest):
                if "'" in rest:
                    rest = rest.replace("'", '"')

                line = line[: idx + 1] + f" '{rest.strip()}'\n"
        quoted_lines.append(line)
    rest_lines = quoted_lines

    skips, wo_skip_lines = [], []
    for line in rest_lines:
        if line.strip().startswith("skip"):
            parts = line.split(":")
            rhs = parts[1].strip()
            if rhs.startswith("true"):
                selector_start = line.rfind("[")
                selector_end = line.rfind("]")
                selector_content = line[selector_start + 1 : selector_end]
                skips.append(selector_content)
            else:
                print("ATTENTION skip: false not handled!")
        else:
            wo_skip_lines.append(line)

    rest_lines = wo_skip_lines
    result_yaml.update(
        ruamel.yaml.load("".join(rest_lines), ruamel.yaml.RoundTripLoader)
    )

    if len(skips) != 0:
        result_yaml["build"]["skip"] = skips

    if result_yaml.get("outputs"):
        for o in result_yaml["outputs"]:
            name = o["name"]
            package = {"name": name}
            del o["name"]
            if o.get("version"):
                package["version"] = o["version"]
                del o["version"]

            build = {}
            if o.get("script"):
                build["script"] = o["script"]
                del o["script"]

            o["package"] = package
            o["build"] = build

        for d in result_yaml["outputs"]:
            print(order_output_dict(d))
        result_yaml["outputs"] = [order_output_dict(d) for d in result_yaml["outputs"]]

    from io import StringIO

    output = StringIO()
    yaml.dump(result_yaml, output)

    # Hacky way to insert an empty line after the context-key-object
    context_output = StringIO()
    yaml.dump(context, context_output)
    context_output = context_output.getvalue()
    context_output_len = len(context_output.split("\n"))

    final_result = output.getvalue()
    final_result_lines = final_result.split("\n")
    final_result_lines.insert(context_output_len, "")

    print("\n".join(final_result_lines))
Ejemplo n.º 9
0
from ruamel.yaml import YAML, representer

logger = logging.getLogger("submitter." + __name__)


class NonAliasingRTRepresenter(representer.RoundTripRepresenter):
    """ Turn off auto-aliases in ruamel.yaml """
    def ignore_aliases(self, data):
        return True


yaml = YAML()
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.Representer = NonAliasingRTRepresenter


def load_json(path):
    """ Load the dictionary from a json file """

    with open(path, "r") as file:
        data = json.load(file)

    return data


def dump_json(data, path):
    """ Dump the dictionary to a json file """

    with open(path, "w") as file: