コード例 #1
0
def add_constructor(tag,
                    object_constructor,
                    Loader=None,
                    constructor=Constructor):
    # type: (Any, Any, Any, Any) -> None
    """
    Add an object constructor for the given tag.
    object_onstructor is a function that accepts a Loader instance
    and a node object and produces the corresponding Python object.
    """
    if Loader is None:
        constructor.add_constructor(tag, object_constructor)
    else:
        if hasattr(Loader, 'add_constructor'):
            Loader.add_constructor(tag, object_constructor)
            return
        if issubclass(Loader, BaseLoader):
            BaseConstructor.add_constructor(tag, object_constructor)
        elif issubclass(Loader, SafeLoader):
            SafeConstructor.add_constructor(tag, object_constructor)
        elif issubclass(Loader, Loader):
            Constructor.add_constructor(tag, object_constructor)
        elif issubclass(Loader, RoundTripLoader):
            RoundTripConstructor.add_constructor(tag, object_constructor)
        else:
            raise NotImplementedError
コード例 #2
0
def add_multi_constructor(tag_prefix,
                          multi_constructor,
                          Loader=None,
                          constructor=Constructor):
    # type: (Any, Any, Any, Any) -> None
    """
    Add a multi-constructor for the given tag prefix.
    Multi-constructor is called for a node if its tag starts with tag_prefix.
    Multi-constructor accepts a Loader instance, a tag suffix,
    and a node object and produces the corresponding Python object.
    """
    if Loader is None:
        constructor.add_multi_constructor(tag_prefix, multi_constructor)
    else:
        if False and hasattr(Loader, 'add_multi_constructor'):
            Loader.add_multi_constructor(tag_prefix, constructor)
            return
        if issubclass(Loader, BaseLoader):
            BaseConstructor.add_multi_constructor(tag_prefix,
                                                  multi_constructor)
        elif issubclass(Loader, SafeLoader):
            SafeConstructor.add_multi_constructor(tag_prefix,
                                                  multi_constructor)
        elif issubclass(Loader, ruamel.yaml.loader.Loader):
            Constructor.add_multi_constructor(tag_prefix, multi_constructor)
        elif issubclass(Loader, RoundTripLoader):
            RoundTripConstructor.add_multi_constructor(tag_prefix,
                                                       multi_constructor)
        else:
            raise NotImplementedError
コード例 #3
0
ファイル: loader.py プロジェクト: bopopescu/vinalo
 def __init__(self, stream, version=None, preserve_quotes=None):
     Reader.__init__(self, stream)
     Scanner.__init__(self)
     Parser.__init__(self)
     Composer.__init__(self)
     SafeConstructor.__init__(self)
     Resolver.__init__(self)
コード例 #4
0
ファイル: loader.py プロジェクト: deerishi/uwpythonworkshop
 def __init__(self, stream, version=None, preserve_quotes=None):
     Reader.__init__(self, stream)
     Scanner.__init__(self)
     Parser.__init__(self)
     Composer.__init__(self)
     SafeConstructor.__init__(self)
     Resolver.__init__(self)
コード例 #5
0
def main():
    global jinja, yaml, inp
    parser = argparse.ArgumentParser()
    parser.add_argument("input", help="Input directory", nargs='?')
    parser.add_argument("-o", "--output", help="Output file")

    args = parser.parse_args()

    inp = get_input_dir(args.input)
    outp = args.output or os.path.join(inp, '..', 'ui-lovelace.yaml')

    jinja = jinja2.Environment(loader=jinja2.FileSystemLoader(inp))
    yaml = YAML(typ='safe')
    yaml.Constructor = SafeConstructor

    SafeConstructor.add_constructor("!include", include_statement)
    SafeConstructor.add_constructor("!file", file_statement)

    try:
        data = process_file(jinja, yaml, MAIN_FILE)
    except Exception as e:
        print("Processing of yaml failed.", file=sys.stderr)
        print(e)
        sys.exit(3)

    try:
        with open(outp, 'w') as fp:
            fp.write(GENERATOR_MESSAGE)
            yaml.dump(data, fp)
    except Exception as e:
        print("Writing ui-lovelace.yaml failed.", file=sys.stderr)
        print(e)
        sys.exit(4)
コード例 #6
0
 def __init__(self, stream, version=None, preserve_quotes=None):
     # type: (StreamTextType, Optional[VersionType], Optional[bool]) -> None
     Reader.__init__(self, stream, loader=self)
     Scanner.__init__(self, loader=self)
     Parser.__init__(self, loader=self)
     Composer.__init__(self, loader=self)
     SafeConstructor.__init__(self, loader=self)
     VersionedResolver.__init__(self, version, loader=self)
コード例 #7
0
def as_scalar(constructor: SafeConstructor, node: YAMLNode) -> Any:
    ''' Helper function to turn YAML scalars into Python objects. '''
    if node.tag == 'tag:yaml.org,2002:null':
        return None
    if node.tag == 'tag:yaml.org,2002:bool':
        return constructor.construct_yaml_bool(node)
    if node.tag == 'tag:yaml.org,2002:float':
        return constructor.construct_yaml_float(node)
    if node.tag == 'tag:yaml.org,2002:int':
        return constructor.construct_yaml_int(node)
    return constructor.construct_scalar(node)
コード例 #8
0
from ruamel.yaml.constructor import SafeConstructor
from elements import elements


def construct_yaml_map(self, node):
    # Turns every svg elements keys/values into tuples to allow
    # duplicates keys in yaml files.
    # Adapted from https://stackoverflow.com/a/44906713

    for key_node, value_node in node.value:
        key = self.construct_object(key_node, deep=True)
        if key in elements and key != 'svg':
            break
    else:
        data = {}  # type: Dict[Any, Any]
        yield data
        value = self.construct_mapping(node)
        data.update(value)
        return

    data = []
    yield data
    for key_node, value_node in node.value:
        key = self.construct_object(key_node, deep=True)
        val = self.construct_object(value_node, deep=True)
        data.append((key, val))


SafeConstructor.add_constructor(u'tag:yaml.org,2002:map', construct_yaml_map)
yaml = YAML(typ='safe')
コード例 #9
0
 def __init__(self, stream, version=None, preserve_quotes=None):
     CParser.__init__(self, stream)
     SafeConstructor.__init__(self)
     Resolver.__init__(self)
コード例 #10
0
 def __init__(self, stream, version=None, preserve_quotes=None):
     # type: (StreamTextType, Optional[VersionType], Optional[bool]) -> None
     CParser.__init__(self, stream)
     self._parser = self._composer = self
     SafeConstructor.__init__(self, loader=self)
     Resolver.__init__(self, loadumper=self)
コード例 #11
0
ファイル: cyaml.py プロジェクト: deerishi/uwpythonworkshop
 def __init__(self, stream, version=None, preserve_quotes=None):
     CParser.__init__(self, stream)
     SafeConstructor.__init__(self)
     Resolver.__init__(self)