# Path: src/pubsub/core/topicexc.py
# class TopicNameError(ValueError):
#     """
#     Raised when the topic name is not properly formatted or
#     no corresponding Topic object found.
#     """
# 
#     def __init__(self, name: str, msg: str):
#         ValueError.__init__(self, 'Topic name "%s": %s' % (name, msg))


from textwrap import TextWrapper, dedent
from typing import Tuple, List, Sequence, Mapping, Dict, Callable, Any, Optional, Union, TextIO
from .topicexc import TopicNameError
import sys
import re

_validNameRE = re.compile(r'[-0-9a-zA-Z]\w*')


def validateName(topicName: str):
    """Raise TopicNameError if nameTuple not valid as topic name."""
    topicNameTuple = tupleize(topicName)
    if not topicNameTuple:
        reason = 'name tuple must have at least one item!'
        raise TopicNameError(None, reason)

    class topic:
        pass

    for subname in topicNameTuple:
        if not subname:
            reason = 'can\'t contain empty string or None'
            raise TopicNameError(topicNameTuple, reason)

        if subname.startswith(UNDERSCORE):
            reason = 'must not start with "%s"' % UNDERSCORE
            raise TopicNameError(topicNameTuple, reason)

        if subname == ALL_TOPICS:
            reason = 'string "%s" is reserved for root topic' % ALL_TOPICS
            raise TopicNameError(topicNameTuple, reason)

        if _validNameRE.match(subname) is None:
            reason = 'element #%s ("%s") has invalid characters' % \
                     (1 + list(topicNameTuple).index(subname), subname)

# Path: src/pubsub/core/topicexc.py
# class TopicNameError(ValueError):
#     """
#     Raised when the topic name is not properly formatted or
#     no corresponding Topic object found.
#     """
# 
#     def __init__(self, name: str, msg: str):
#         ValueError.__init__(self, 'Topic name "%s": %s' % (name, msg))


# Path: src/pubsub/core/topicutils.py
from textwrap import TextWrapper, dedent
from typing import Tuple, List, Sequence, Mapping, Dict, Callable, Any, Optional, Union, TextIO
from .topicexc import TopicNameError
import sys
import re


_validNameRE = re.compile(r'[-0-9a-zA-Z]\w*')


def validateName(topicName: str):
    """Raise TopicNameError if nameTuple not valid as topic name."""
    topicNameTuple = tupleize(topicName)
    if not topicNameTuple:
        reason = 'name tuple must have at least one item!'
        raise TopicNameError(None, reason)

    class topic:
        pass

    for subname in topicNameTuple:
        if not subname:
            reason = 'can\'t contain empty string or None'
            raise TopicNameError(topicNameTuple, reason)

        if subname.startswith(UNDERSCORE):
            reason = 'must not start with "%s"' % UNDERSCORE
            raise TopicNameError(topicNameTuple, reason)

        if subname == ALL_TOPICS:
            reason = 'string "%s" is reserved for root topic' % ALL_TOPICS
            raise TopicNameError(topicNameTuple, reason)

        if _validNameRE.match(subname) is None:
            reason = 'element #%s ("%s") has invalid characters' % \
                     (1 + list(topicNameTuple).index(subname), subname)

            raise TopicNameError(topicNameTuple, reason)