def load(cls, stream, constructors=None, multi_constructors=None): loader = Loader(stream) cs = dict(cls._constructors) if constructors: cs.update(constructors) mcs = dict(cls._multi_constructors) if multi_constructors: mcs.update(multi_constructors) if cs: for name, constructor in cs.items(): loader.add_constructor(name, constructor) if mcs: for name, constructor in mcs.items(): loader.add_multi_constructor(name, constructor) try: return loader.get_single_data() finally: loader.dispose()
def load(stream, constructors=None): loader = Loader(stream) constructors = constructors or {} if not "timedelta" in constructors: loader.add_constructor("!timedelta", _timedelta_contructor) if not "re" in constructors: loader.add_constructor("!re", _re_constructor) loader.add_multi_constructor("!ref:", _ref_constructor) loader.add_multi_constructor("!factory:", _factory_constructor) loader.add_multi_constructor("!obj:", _obj_constructor) loader.add_multi_constructor("!extends:", _extends_constructor) loader.add_multi_constructor("!include:", _extends_constructor) if constructors: for name, constructor in constructors.items(): loader.add_constructor("!" + name, constructor) try: return loader.get_single_data() finally: loader.dispose()
""" Class description goes here. """ from uuid import UUID from yaml import Loader, Dumper JAVA_UUID_TAG = u'tag:yaml.org,2002:java.util.UUID' DATACLAY_ID_PREFIX = u'tag:yaml.org,2002:es.bsc.dataclay.util.ids' def uuid_representer(dumper, data): return dumper.represent_scalar(JAVA_UUID_TAG, str(data)) def uuid_constructor(loader, node): value = loader.construct_scalar(node) return UUID(value) Dumper.add_representer(UUID, uuid_representer) Loader.add_constructor(JAVA_UUID_TAG, uuid_constructor) Loader.add_multi_constructor( DATACLAY_ID_PREFIX, # This ignores the tag, as ImplementationID, OperationID, *ID are always # used directly as their UUID, not their specific type. lambda loader, _, node: uuid_constructor(loader, node))
s = loader.construct_scalar(node) return Langs.Value(s) Loader.add_constructor(u"tag:yaml.org,2002:value", lonely_equal_constructor) # The tuple is a bit special itself Loader.add_constructor(u"tag:yaml.org,2002:es.bsc.dataclay.util.structs.Tuple", tuple_constructor) # Not needed for Python, but nice to avoid errors Loader.add_constructor(u"tag:yaml.org,2002:es.bsc.dataclay.util.management.classmgr.features.Feature$FeatureType", feature_constructor) # The language is very special Loader.add_constructor(u"tag:yaml.org,2002:es.bsc.dataclay.communication.grpc.messages.common.CommonMessages$Langs", lang_constructor) for prefix in IGNORE_PREFIXES: yaml_tag_prefix = u"tag:yaml.org,2002:%s" % prefix Loader.add_multi_constructor( yaml_tag_prefix, # The following is used to disregard the tag lambda loader, _, node: trivial_constructor(loader, node)) for class_tag in IGNORE_CLASSES: yaml_class_tag = u"tag:yaml.org,2002:%s" % class_tag Loader.add_constructor(yaml_class_tag, trivial_constructor) # Force all YAML usages to go through this class (so Loader is properly set) dataclay_yaml_load = partial(load, Loader=Loader) dataclay_yaml_dump = dump