Пример #1
0
def load(filename):
    dirname = os.path.dirname(filename)

    def include(loader, node):
        try:
            filename = loader.construct_scalar(node)
        except:
            try:
                filename, nodename = loader.construct_sequence(node)
            except:
                raise ConstructorError(
                    None, None,
                    'expected a string or sequence(2 elements), but found %s' %
                    node.id, node.start_mark)
        else:
            nodename = None

        with open(os.path.join(dirname, filename)) as f:
            document = yaml.load(f)

        if nodename:
            return document[nodename]

        return document

    Constructor.add_constructor(INCLUDE_TAG, include)

    with open(filename) as f:
        data = yaml.load(f)

    del Constructor.yaml_constructors[INCLUDE_TAG]

    return data
Пример #2
0
def load(filename, encoding='utf-8'):
    dirname = os.path.dirname(filename)

    def include(loader, node):
        try:
            filename = loader.construct_scalar(node)
        except:
            try:
                filename, nodename = loader.construct_sequence(node)
            except:
                raise ConstructorError(None, None,
                    'expected a string or sequence(2 elements), but found %s' % node.id,
                    node.start_mark)
        else:
            nodename = None

        with open(os.path.join(dirname, filename), encoding=encoding) as f:
            document = yaml.load(f)

        if nodename:
            return document[nodename]

        return document

    Constructor.add_constructor(INCLUDE_TAG, include)

    with open(filename, encoding=encoding) as f:
        data = yaml.load(f)

    del Constructor.yaml_constructors[INCLUDE_TAG]

    return data
Пример #3
0
        mapping[key] = value
    
    return mapping

BaseConstructor.construct_mapping = construct_ordered_mapping


def construct_yaml_map_with_ordered_dict(self, node):
    data = OrderedDict()
    yield data
    value = self.construct_mapping(node)
    data.update(value)

for t in [u'tag:yaml.org,2002:map', u'tag:yaml.org,2002:omap']:
    SafeConstructor.add_constructor( t, construct_yaml_map_with_ordered_dict )
    Constructor.add_constructor( t, construct_yaml_map_with_ordered_dict )
    yaml.add_constructor( t, construct_yaml_map_with_ordered_dict )


def represent_ordered_mapping(self, tag, mapping, flow_style=None):
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    best_style = True
    
    if self.alias_key is not None:
        self.represented_objects[self.alias_key] = node
    
    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())
    
    for item_key, item_value in mapping:
Пример #4
0
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass

    return False


def add_bool(self, node):
    return self.construct_scalar(node)


Constructor.add_constructor(u'tag:yaml.org,2002:bool', add_bool)


def main():
    inputFname = 'content.yaml'
    with open(inputFname, 'r') as f:
        content = yaml.load(f)

    # pp.pprint(content)
    validate(content)
    html = htmlize(content)
    # print(html)

    with open('stub.html', 'w') as f:
        f.write(html)
    print('done')
Пример #5
0
    return mapping


BaseConstructor.construct_mapping = construct_ordered_mapping


def construct_yaml_map_with_ordered_dict(self, node):
    data = OrderedDict()
    yield data
    value = self.construct_mapping(node)
    data.update(value)


for t in [u'tag:yaml.org,2002:map', u'tag:yaml.org,2002:omap']:
    SafeConstructor.add_constructor(t, construct_yaml_map_with_ordered_dict)
    Constructor.add_constructor(t, construct_yaml_map_with_ordered_dict)
    yaml.add_constructor(t, construct_yaml_map_with_ordered_dict)


def represent_ordered_mapping(self, tag, mapping, flow_style=None):
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    best_style = True

    if self.alias_key is not None:
        self.represented_objects[self.alias_key] = node

    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())

    for item_key, item_value in mapping: