class InventoryDumper(Dumper): """ Yaml Dumper class for PyYAML """ yaml_representers = getattr(Dumper, 'yaml_representers', {}).copy() yaml_representers[type(None)] = lambda dumper, value: (ScalarNode( tag=u'tag:yaml.org,2002:null', value='')) yaml_representers[str] = lambda dumper, value: (ScalarNode( tag=u'tag:yaml.org,2002:str', value=value))
def dupe_node(): tag = 'tag:yaml.org,2002:map' scalar_tag = 'tag:yaml.org,2002:str' mark = Mark(tag, 0, 0, 0, None, None) node = MappingNode(tag, [ (ScalarNode(tag=scalar_tag, value='bar', start_mark=mark), ScalarNode(tag=scalar_tag, value='baz', start_mark=mark)), (ScalarNode(tag=scalar_tag, value='bar', start_mark=mark), ScalarNode(tag=scalar_tag, value='qux', start_mark=mark)), ], start_mark=mark) return node
def __mn__setitem__(self, key: str, value: Union[str, float, int]): assert isinstance(key, str) if isinstance(value, str): new_value = ScalarNode(tag="tag:yaml.org,2002:str", value=value) elif isinstance(value, int): new_value = ScalarNode(tag="tag:yaml.org,2002:int", value=value) elif isinstance(value, float): new_value = ScalarNode(tag="tag:yaml.org,2002:float", value=value) else: raise Exception(f"Unknown type for {value}") if key not in self.keys(): new_key = ScalarNode(tag="tag:yaml.org,2002:str", value="name") self.value.append((new_key, new_value)) else: i = self.keys().index(key) k = self.value[i][0] self.value[i] = (k, new_value)
def mapping_item_to_strings(item, state = BASE): key, value = item tag = key.tag if state is QUOTE_AS_STRINGS and tag != STRING_TAG: key_ = ScalarNode(tag = STRING_TAG, value = key.value) else: key_ = key if state is QUOTE_AS_STRINGS or tag == QuoteAsStrings.yaml_tag: next_state = QUOTE_AS_STRINGS elif state is SYMBOL or tag == Symbol.yaml_tag: next_state = SYMBOL else: next_state = BASE return (key_, scalars_to_strings(value, state = next_state))
def load(self, context: ILoadingContext, field: IBaseField) -> Any: if not context.expect_scalar( _("!env must be set on a string node containing the variable name.") ): return UNDEFINED node = context.current_node() var_name = node.value if var_name not in environ: return UNDEFINED fake_node = ScalarNode("", environ[var_name], node.start_mark, node.end_mark) return context.load(field, fake_node)
def model_representer(dumper, obj): node = MappingNode(tag=u'tag:yaml.org,2002:map', value=[], flow_style=False) for column in obj.__table__.columns: if isinstance(column.type, String): node.value.append((ScalarNode(tag=u'tag:yaml.org,2002:str', value=column.name), dumper.represent_scalar(tag=u'tag:yaml.org,2002:str', value=unicode(getattr(obj, column.name)), style='"'))) elif isinstance(column.type, Integer): node.value.append((ScalarNode(tag=u'tag:yaml.org,2002:str', value=column.name), dumper.represent_scalar(tag=u'tag:yaml.org,2002:int', value=unicode(getattr(obj, column.name)), style=''))) elif isinstance(column.type, Float): node.value.append((ScalarNode(tag=u'tag:yaml.org,2002:str', value=column.name), dumper.represent_scalar(tag=u'tag:yaml.org,2002:float', value=unicode(getattr(obj, column.name)), style=''))) elif isinstance(column.type, Boolean): node.value.append((ScalarNode(tag=u'tag:yaml.org,2002:str', value=column.name), dumper.represent_scalar(tag=u'tag:yaml.org,2002:bool', value=unicode(getattr(obj, column.name)), style=''))) else: raise TypeError("No scalar representation is implemented for SQLAlchemy column type {col_type}".format(column.type)) # This code is to print out the attributes inherited from the class Device by the AnalogDevice and DigitalDevice if (hasattr(obj.__class__.__bases__[0],'__tablename__')): if (obj.__class__.__bases__[0].__tablename__ == "devices"): for column in obj.__class__.__bases__[0].__table__.columns: if (column.name != "type"): if isinstance(column.type, String): node.value.append((ScalarNode(tag=u'tag:yaml.org,2002:str', value=column.name), dumper.represent_scalar(tag=u'tag:yaml.org,2002:str', value=unicode(getattr(obj, column.name)), style='"'))) elif isinstance(column.type, Integer): node.value.append((ScalarNode(tag=u'tag:yaml.org,2002:str', value=column.name), dumper.represent_scalar(tag=u'tag:yaml.org,2002:int', value=unicode(getattr(obj, column.name)), style=''))) elif isinstance(column.type, Float): node.value.append((ScalarNode(tag=u'tag:yaml.org,2002:str', value=column.name), dumper.represent_scalar(tag=u'tag:yaml.org,2002:float', value=unicode(getattr(obj, column.name)), style=''))) elif isinstance(column.type, Boolean): node.value.append((ScalarNode(tag=u'tag:yaml.org,2002:str', value=column.name), dumper.represent_scalar(tag=u'tag:yaml.org,2002:bool', value=unicode(getattr(obj, column.name)), style=''))) else: raise TypeError("No scalar representation is implemented for SQLAlchemy column type {col_type}".format(column.type)) return node
SequenceNode: lambda n: n, ScalarNode: lambda n: n, MappingView: lambda v: v.node, SequenceView: lambda v: v.node, ScalarView: lambda v: v.node, list: lambda l: SequenceNode(Tag.SEQUENCE.value, [node(i) for i in l]), tuple: lambda t: SequenceNode(Tag.SEQUENCE.value, [node(i) for i in t]), str: lambda s: ScalarNode(Tag.STRING.value, str(s)), bool: lambda b: ScalarNode(Tag.BOOL.value, str(b)), int: lambda i: ScalarNode(Tag.INT.value, str(i)), float: lambda f: ScalarNode(Tag.FLOAT.value, str(f)), dict: lambda d: MappingNode(Tag.MAPPING.value, [(node(k), node(v)) for k, v in d.items()]) } def node(value: Any) -> Node: return COERCIONS[type(value)](value)
def scalars_to_strings(node, state = BASE): r'''Turn scalars into strings, except for dict keys and !stubbly content. Also turn !stubbly/quote-as-strings content into strings. >>> document = """ ... number: 5 ... boolean: on ... null: ... unquoted: hello ... quoted: 'hello' ... sequence: ... - 1 # SHOULD be converted to a string ... - 2: [3, 4] # 2 shoud NOT be converted to a string ... $code: ... - 4 # should NOT be converted to a string ... - $quote-as-strings: ... - 5: 6 # should ALL be converted to a string ... - $code: 6 # SHOULD be converted to strings ... ? ... """ >>> loader = yaml.Loader(document) >>> loader.check_data() True >>> node = loader.get_node() >>> stringified = scalars_to_strings(node) >>> print(yaml.serialize(stringified).strip()) number: '5' boolean: 'on' null: '' unquoted: hello quoted: 'hello' sequence: - '1' - 2: - '3' - '4' $code: - 4 - $quote-as-strings: - '5': '6' - '$code': '6' ? : '' ''' tag = node.tag value = node.value if type(node) is SequenceNode: return SequenceNode( tag = tag, value = [scalars_to_strings(item, state) for item in value] ) if type(node) is MappingNode: return MappingNode( tag = tag, value = [mapping_item_to_strings(item, state) for item in value] ) if (tag == STRING_TAG or state is SYMBOL or (state is BASE and tag.startswith(STUBBLY_TAG_PREFIX))): return node return ScalarNode( tag = STRING_TAG, value = value )
def as_node(s): return ScalarNode(u'tag:yaml.org,2002:float', str(s))
def as_node(s): return ScalarNode(u'tag:yaml.org,2002:str', s)