# Path: openformats/exceptions.py
# class ParseError(OpenformatsError):
#     pass
# 
# Path: openformats/utils/compat.py
# def ensure_unicode(pattern):
#     if isinstance(pattern, six.binary_type):
#         pattern = pattern.decode('utf-8')
#     return pattern
# 
# Path: openformats/formats/yaml/constants.py
# YAML_BINARY_ID = u'tag:yaml.org,2002:binary'
# 
# YAML_STRING_ID = u'tag:yaml.org,2002:str'
# 
# Path: openformats/formats/yaml/yaml_representee_classes.py
# class BlockList(list):
#     pass
# 
# class BlockStyleOrderedDict(OrderedDict):
#     pass
# 
# class FlowList(list):
#     pass
# 
# class FlowStyleOrderedDict(OrderedDict):
#     pass
# 
# class double_quoted_unicode(plain_unicode):  # noqa: N801
#     pass
# 
# class folded_unicode(plain_unicode):  # noqa: N801
#     pass
# 
# class literal_unicode(plain_unicode):  # noqa: N801
#     pass
# 
# class plain_unicode(six.text_type):  # noqa: N801
# 
#     def __new__(self, value, tag=None):
#         self = super(plain_unicode, self).__new__(self, value)
#         self.tag = tag or YAML_STRING_ID
#         return self
# 
#     pass
# 
# class single_quoted_unicode(plain_unicode):  # noqa: N801
#     pass
# 
# Path: openformats/formats/yaml/yaml_representers.py
# def block_list_representer(dumper, data):
#     return dumper.represent_sequence(YAML_LIST_ID, data, flow_style=False)
# 
# def block_style_ordered_dict_representer(dumper, data):
#     return dumper.represent_mapping(YAML_DICT_ID, six.iteritems(data),
#                                     flow_style=False)
# 
# def double_quoted_unicode_representer(dumper, data):
#     tag = getattr(data, 'tag', YAML_STRING_ID)
#     return dumper.represent_scalar(tag, data, style='"')
# 
# def flow_list_representer(dumper, data):
#     return dumper.represent_sequence(YAML_LIST_ID, data, flow_style=True)
# 
# def flow_style_ordered_dict_representer(dumper, data):
#     return dumper.represent_mapping(YAML_DICT_ID, six.iteritems(data),
#                                     flow_style=True)
# 
# def folded_unicode_representer(dumper, data):
#     return dumper.represent_scalar(YAML_STRING_ID, data, style='>')
# 
# def literal_unicode_representer(dumper, data):
#     return dumper.represent_scalar(YAML_STRING_ID, data, style='|')
# 
# def ordered_dict_representer(dumper, data):
#     return dumper.represent_dict(six.iteritems(data))
# 
# def single_quoted_unicode_representer(dumper, data):
#     tag = getattr(data, 'tag', YAML_STRING_ID)
#     return dumper.represent_scalar(tag, data, style="'")
# 
# def unicode_representer(dumper, data):
#     tag = getattr(data, 'tag', YAML_STRING_ID)
#     return yaml.ScalarNode(tag=tag, value=data)


import re
import six
import yaml
from collections import OrderedDict, namedtuple
from yaml.constructor import ConstructorError
from openformats.exceptions import ParseError
from openformats.utils.compat import ensure_unicode
from .constants import YAML_BINARY_ID, YAML_STRING_ID
from .yaml_representee_classes import (BlockList, BlockStyleOrderedDict,
                                       FlowList, FlowStyleOrderedDict,
                                       double_quoted_unicode, folded_unicode,
                                       literal_unicode, plain_unicode,
                                       single_quoted_unicode)
from .yaml_representers import (block_list_representer,
                                block_style_ordered_dict_representer,
                                double_quoted_unicode_representer,
                                flow_list_representer,
                                flow_style_ordered_dict_representer,
                                folded_unicode_representer,
                                literal_unicode_representer,
                                ordered_dict_representer,
                                single_quoted_unicode_representer,
                                unicode_representer)
        if anchor is None:
            return super(TxYamlLoader, self).compose_scalar_node(anchor)
        else:
            node = super(TxYamlLoader, self).compose_scalar_node(anchor)
            if node.tag == u'tag:yaml.org,2002:null':
                # 'key: &anchor' should be interpreted as 'key:', ie the value
                # should be ignored
                return node
            anchor_value = self.stream[
                node.start_mark.index:node.end_mark.index
            ].split(' ', 1)[1]
            leading_spaces = len(anchor_value) - len(anchor_value.lstrip(' '))

            node.start_mark.index = node.end_mark.index - len(anchor_value) \
                + leading_spaces
            return node

    def _is_custom_tag(self, tag):
        """
        Check whether a value is tagged with a custom type.

        Detect custom tags, like:
            `foo: !bar test`
            `foo: !xml "<bar>Bar</bar>"`
        Built-in types, indicated by a `!!` prefix, will not be matched. We
        can't preserve the information whether a built-in tag like `!!str` was
        used for a value since the PyYAML library will tag such entries with
        the built-in identifier. For example `tag:yaml.org,2002:str`, not
        `!!str`.
        """

# Path: openformats/exceptions.py
# class ParseError(OpenformatsError):
#     pass
# 
# Path: openformats/utils/compat.py
# def ensure_unicode(pattern):
#     if isinstance(pattern, six.binary_type):
#         pattern = pattern.decode('utf-8')
#     return pattern
# 
# Path: openformats/formats/yaml/constants.py
# YAML_BINARY_ID = u'tag:yaml.org,2002:binary'
# 
# YAML_STRING_ID = u'tag:yaml.org,2002:str'
# 
# Path: openformats/formats/yaml/yaml_representee_classes.py
# class BlockList(list):
#     pass
# 
# class BlockStyleOrderedDict(OrderedDict):
#     pass
# 
# class FlowList(list):
#     pass
# 
# class FlowStyleOrderedDict(OrderedDict):
#     pass
# 
# class double_quoted_unicode(plain_unicode):  # noqa: N801
#     pass
# 
# class folded_unicode(plain_unicode):  # noqa: N801
#     pass
# 
# class literal_unicode(plain_unicode):  # noqa: N801
#     pass
# 
# class plain_unicode(six.text_type):  # noqa: N801
# 
#     def __new__(self, value, tag=None):
#         self = super(plain_unicode, self).__new__(self, value)
#         self.tag = tag or YAML_STRING_ID
#         return self
# 
#     pass
# 
# class single_quoted_unicode(plain_unicode):  # noqa: N801
#     pass
# 
# Path: openformats/formats/yaml/yaml_representers.py
# def block_list_representer(dumper, data):
#     return dumper.represent_sequence(YAML_LIST_ID, data, flow_style=False)
# 
# def block_style_ordered_dict_representer(dumper, data):
#     return dumper.represent_mapping(YAML_DICT_ID, six.iteritems(data),
#                                     flow_style=False)
# 
# def double_quoted_unicode_representer(dumper, data):
#     tag = getattr(data, 'tag', YAML_STRING_ID)
#     return dumper.represent_scalar(tag, data, style='"')
# 
# def flow_list_representer(dumper, data):
#     return dumper.represent_sequence(YAML_LIST_ID, data, flow_style=True)
# 
# def flow_style_ordered_dict_representer(dumper, data):
#     return dumper.represent_mapping(YAML_DICT_ID, six.iteritems(data),
#                                     flow_style=True)
# 
# def folded_unicode_representer(dumper, data):
#     return dumper.represent_scalar(YAML_STRING_ID, data, style='>')
# 
# def literal_unicode_representer(dumper, data):
#     return dumper.represent_scalar(YAML_STRING_ID, data, style='|')
# 
# def ordered_dict_representer(dumper, data):
#     return dumper.represent_dict(six.iteritems(data))
# 
# def single_quoted_unicode_representer(dumper, data):
#     tag = getattr(data, 'tag', YAML_STRING_ID)
#     return dumper.represent_scalar(tag, data, style="'")
# 
# def unicode_representer(dumper, data):
#     tag = getattr(data, 'tag', YAML_STRING_ID)
#     return yaml.ScalarNode(tag=tag, value=data)


# Path: openformats/formats/yaml/utils.py
import re
import six
import yaml
from collections import OrderedDict, namedtuple
from yaml.constructor import ConstructorError
from openformats.exceptions import ParseError
from openformats.utils.compat import ensure_unicode
from .constants import YAML_BINARY_ID, YAML_STRING_ID
from .yaml_representee_classes import (BlockList, BlockStyleOrderedDict,
                                       FlowList, FlowStyleOrderedDict,
                                       double_quoted_unicode, folded_unicode,
                                       literal_unicode, plain_unicode,
                                       single_quoted_unicode)
from .yaml_representers import (block_list_representer,
                                block_style_ordered_dict_representer,
                                double_quoted_unicode_representer,
                                flow_list_representer,
                                flow_style_ordered_dict_representer,
                                folded_unicode_representer,
                                literal_unicode_representer,
                                ordered_dict_representer,
                                single_quoted_unicode_representer,
                                unicode_representer)

        if anchor is None:
            return super(TxYamlLoader, self).compose_scalar_node(anchor)
        else:
            node = super(TxYamlLoader, self).compose_scalar_node(anchor)
            if node.tag == u'tag:yaml.org,2002:null':
                # 'key: &anchor' should be interpreted as 'key:', ie the value
                # should be ignored
                return node
            anchor_value = self.stream[
                node.start_mark.index:node.end_mark.index
            ].split(' ', 1)[1]
            leading_spaces = len(anchor_value) - len(anchor_value.lstrip(' '))

            node.start_mark.index = node.end_mark.index - len(anchor_value) \
                + leading_spaces
            return node

    def _is_custom_tag(self, tag):
        """
        Check whether a value is tagged with a custom type.

        Detect custom tags, like:
            `foo: !bar test`
            `foo: !xml "<bar>Bar</bar>"`
        Built-in types, indicated by a `!!` prefix, will not be matched. We
        can't preserve the information whether a built-in tag like `!!str` was
        used for a value since the PyYAML library will tag such entries with
        the built-in identifier. For example `tag:yaml.org,2002:str`, not
        `!!str`.
        """

        return re.match(ensure_unicode(r'^[\![a-zA-Z_]*]*$'),