Exemple #1
0
 def eval_constructor(loader: Loader, node):
     value = loader.construct_sequence(node, deep=True)
     try:
         expr = value[0]
     except:
         expr = None
     return EvalType(expr)
Exemple #2
0
 def _from_yaml(cls, loader: yaml.Loader, node: yaml.Node) -> "ToJson":
     """Load an internal yaml node parsing."""
     try:
         value = loader.construct_mapping(node, deep=True)
     except yaml.constructor.ConstructorError:
         value = loader.construct_sequence(node, deep=True)
     return cls(value)
Exemple #3
0
 def join_constructor(loader: Loader, node):
     values = loader.construct_sequence(node, deep=True)
     try:
         sep, parts = str(values[0]), [str(_) for _ in values[1]]
         return sep.join(parts)
     except:
         return ''
Exemple #4
0
 def not_constructor(loader: Loader, node) -> bool:
     value = loader.construct_sequence(node, deep=True)
     try:
         expr = not value[0]
     except:
         expr = False
     return expr
Exemple #5
0
 def key_constructor(loader: Loader, node):
     value = loader.construct_sequence(node, deep=True)
     try:
         obj = value[0]
         keys = value[1:]
         return getAttribute(obj, *keys)
     except:
         return None
Exemple #6
0
def construct_tagless_yaml(loader: yaml.Loader, node: yaml.Node):
    # From yaml.constructor.BaseConstructor#construct_object
    if isinstance(node, yaml.ScalarNode):
        return loader.construct_scalar(node)
    elif isinstance(node, yaml.SequenceNode):
        return loader.construct_sequence(node)
    elif isinstance(node, yaml.MappingNode):
        return loader.construct_mapping(node)
    raise NotImplementedError('invalid node')
def construct_custom_class(cls, loader: yaml.Loader, node: yaml.Node):
    result = cls.__new__(cls)
    yield result
    if isinstance(node, yaml.ScalarNode):
        value = loader.construct_scalar(node)
    elif isinstance(node, yaml.SequenceNode):
        value = loader.construct_sequence(node)
    elif isinstance(node, yaml.MappingNode):
        value = loader.construct_mapping(node)
    else:
        assert False
    result.__init__(value)
def construct_dataframe(loader: yaml.Loader, node: yaml.Node):
    result = DataFrameFuture.__new__(DataFrameFuture)
    yield result
    if isinstance(node, yaml.SequenceNode):
        data = loader.construct_sequence(node)
    elif isinstance(node, yaml.MappingNode):
        data = loader.construct_mapping(node)
    else:
        raise ValueError(
            f'Error constructing DataFrame. Expected dictionary or array of dictionaries, found {type(node)}'
        )
    result.__init__(data)
Exemple #9
0
def inline_keyboard_constructor(loader: yaml.Loader,
                                node: yaml.Node) -> types.ReplyKeyboardMarkup:
    inline_keyboard = types.InlineKeyboardMarkup()
    source = loader.construct_sequence(node=node, deep=True)
    for item in source:
        if isinstance(item, list):
            inline_keyboard.row(
                *[types.InlineKeyboardButton(**button) for button in item])
        elif isinstance(item, dict):
            inline_keyboard.add(types.InlineKeyboardButton(**item))
        else:
            assert False

    return inline_keyboard
Exemple #10
0
 def join_constructor(loader: Loader, node):
     values = loader.construct_sequence(node, deep=True)
     try:
         sep = str(values[0])
         parts = [str(_) for _ in values[1]]
         # value = sep.join(parts);
         value = None
         for part in parts:
             if value is None:
                 value = part
             else:
                 value += sep + part
         return value or ''
     except:
         return ''
Exemple #11
0
def reply_markup_constructor(loader: yaml.Loader,
                             node: yaml.Node) -> types.ReplyKeyboardMarkup:
    reply_markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    source = loader.construct_sequence(node=node, deep=True)
    for item in source:
        if isinstance(item, list):
            reply_markup.row(*[button_from_source(button) for button in item])
        elif isinstance(item, dict):
            reply_markup.add(types.KeyboardButton(**item))
        elif isinstance(item, str):
            reply_markup.add(types.KeyboardButton(text=item))
        else:
            assert False

    return reply_markup
def intrinsics_multi_constructor(  # pylint: disable=unused-argument
        loader: yaml.Loader, tag_prefix: str,
        node: yaml.Node) -> Dict[str, Any]:
    """YAML constructor to parse CloudFormation intrinsics.

    This will return a dictionary with key being the intrinsic name

    """
    # Get the actual tag name excluding the first exclamation
    tag = node.tag[1:]

    # Some intrinsic functions doesn't support prefix "Fn::"
    prefix = "Fn::"
    if tag in ["Ref", "Condition"]:
        prefix = ""

    cfntag = prefix + tag

    if tag == "GetAtt" and isinstance(node.value, str):
        # ShortHand notation for !GetAtt accepts Resource.Attribute format
        # while the standard notation is to use an array
        # [Resource, Attribute]. Convert shorthand to standard format
        value = node.value.split(".", 1)

    elif isinstance(node, yaml.ScalarNode):
        # Value of this node is scalar
        value = loader.construct_scalar(node)

    elif isinstance(node, yaml.SequenceNode):
        # Value of this node is an array (Ex: [1,2])
        value = cast(MutableSequence[Any], loader.construct_sequence(node))

    else:
        # Value of this node is an mapping (ex: {foo: bar})
        value = cast(MutableMapping[Any, Any], loader.construct_mapping(node))

    return {cfntag: value}
Exemple #13
0
 def ordered_dict_constructor(loader: yaml.Loader, node: yaml.Node):
     # NOTE(ycho): `deep` has to be true for `construct_yaml_seq`.
     value = loader.construct_sequence(node, deep=True)
     return OrderedDict(*value)
Exemple #14
0
 def ordered_dict_constructor(loader: yaml.Loader, node: yaml.Node):
     value = loader.construct_sequence(node)
     return OrderedDict(*value)