meerschaum.utils.yaml
Meerschaum wrapper around the YAML codec.
YAML is read and written by the Rust codec in meerschaum_core::yaml (meerschaum._core.yaml):
a port of PyYAML's reader, scanner, parser, composer, constructor, representer, serializer and
emitter that builds real Python objects (datetime.date, datetime.datetime, bytes, big
int, any hashable dict key). No YAML package is imported or installed.
1#! /usr/bin/env python 2# -*- coding: utf-8 -*- 3# vim:fenc=utf-8 4 5""" 6Meerschaum wrapper around the YAML codec. 7 8YAML is read and written by the Rust codec in `meerschaum_core::yaml` (`meerschaum._core.yaml`): 9a port of PyYAML's reader, scanner, parser, composer, constructor, representer, serializer and 10emitter that builds real Python objects (`datetime.date`, `datetime.datetime`, `bytes`, big 11`int`, any hashable dict key). No YAML package is imported or installed. 12""" 13 14from meerschaum import _core 15 16### The native codec module. Importing this module loads the extension. 17_native = _core.yaml 18_yaml = _native 19 20### The `dump()` keywords the native codec understands (the others are ignored, like the 21### keywords `filter_keywords()` used to strip before PyYAML saw them). 22_NATIVE_DUMP_KEYWORDS = frozenset(( 23 'default_style', 'default_flow_style', 'sort_keys', 'indent', 'width', 'allow_unicode', 24 'line_break', 'explicit_start', 'explicit_end', 'encoding', 'Dumper', 'canonical', 25 'version', 'tags', 26)) 27 28 29class yaml: 30 """ 31 Wrapper around the native codec with the interface of `PyYAML`. 32 """ 33 34 @staticmethod 35 def safe_load(*args, **kw): 36 """ 37 Parse a YAML document into Python objects. 38 """ 39 return _native.safe_load(*args) 40 41 @staticmethod 42 def load(*args, **kw): 43 """ 44 Parse a YAML document into Python objects (`Loader` is accepted and ignored). 45 """ 46 return _native.load(*args, **{k: v for k, v in kw.items() if k == 'Loader'}) 47 48 @staticmethod 49 def dump(data, stream=None, **kw): 50 """ 51 Dump to a stream. If no stream is provided, return a string instead. 52 """ 53 return _native.dump( 54 data, 55 stream, 56 **{k: v for k, v in kw.items() if k in _NATIVE_DUMP_KEYWORDS} 57 ) 58 59 60def get_dumper_class(): 61 """ 62 Return the dumper class to use when writing. 63 The native codec has no dumper class and returns `None`. 64 """ 65 return None
class
yaml:
30class yaml: 31 """ 32 Wrapper around the native codec with the interface of `PyYAML`. 33 """ 34 35 @staticmethod 36 def safe_load(*args, **kw): 37 """ 38 Parse a YAML document into Python objects. 39 """ 40 return _native.safe_load(*args) 41 42 @staticmethod 43 def load(*args, **kw): 44 """ 45 Parse a YAML document into Python objects (`Loader` is accepted and ignored). 46 """ 47 return _native.load(*args, **{k: v for k, v in kw.items() if k == 'Loader'}) 48 49 @staticmethod 50 def dump(data, stream=None, **kw): 51 """ 52 Dump to a stream. If no stream is provided, return a string instead. 53 """ 54 return _native.dump( 55 data, 56 stream, 57 **{k: v for k, v in kw.items() if k in _NATIVE_DUMP_KEYWORDS} 58 )
Wrapper around the native codec with the interface of PyYAML.
@staticmethod
def
safe_load(*args, **kw):
35 @staticmethod 36 def safe_load(*args, **kw): 37 """ 38 Parse a YAML document into Python objects. 39 """ 40 return _native.safe_load(*args)
Parse a YAML document into Python objects.
@staticmethod
def
load(*args, **kw):
42 @staticmethod 43 def load(*args, **kw): 44 """ 45 Parse a YAML document into Python objects (`Loader` is accepted and ignored). 46 """ 47 return _native.load(*args, **{k: v for k, v in kw.items() if k == 'Loader'})
Parse a YAML document into Python objects (Loader is accepted and ignored).
@staticmethod
def
dump(data, stream=None, **kw):
49 @staticmethod 50 def dump(data, stream=None, **kw): 51 """ 52 Dump to a stream. If no stream is provided, return a string instead. 53 """ 54 return _native.dump( 55 data, 56 stream, 57 **{k: v for k, v in kw.items() if k in _NATIVE_DUMP_KEYWORDS} 58 )
Dump to a stream. If no stream is provided, return a string instead.
def
get_dumper_class():
61def get_dumper_class(): 62 """ 63 Return the dumper class to use when writing. 64 The native codec has no dumper class and returns `None`. 65 """ 66 return None
Return the dumper class to use when writing.
The native codec has no dumper class and returns None.