예제 #1
0
    def test_boolean_application_to_context(self):
        if _debug:
            TestApplicationTag._debug("test_boolean_application_to_context")

        # create an application
        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0)
        if _debug: TestApplicationTag._debug("    - tag: %r", tag_tuple(tag))

        # convert it to context tagged, context 0
        ctag = tag.app_to_context(0)
        if _debug: TestApplicationTag._debug("    - ctag: %r", tag_tuple(ctag))

        # create a context tag with the same shape
        ttag = ContextTag(0, xtob('00'))
        if _debug: TestApplicationTag._debug("    - ttag: %r", tag_tuple(ttag))

        # check to see they are the same
        assert ctag == ttag

        # convert the context tag back to an application tag
        dtag = ctag.context_to_app(Tag.booleanAppTag)
        if _debug: TestApplicationTag._debug("    - dtag: %r", tag_tuple(dtag))

        # check to see it round-tripped
        assert dtag == tag
예제 #2
0
def tag_encode(obj, context=None):
    """Encode an atomic object into a tag."""
    if _debug: tag_encode._debug("tag_encode %r", obj)

    # encode it normally
    tag = Tag()
    obj.encode(tag)

    # check for context encoding
    if context is not None:
        tag = tag.app_to_context(context)

    if _debug: tag_encode._debug("    - tag: %r", tag)

    return tag
예제 #3
0
def statement_to_tag(line):
    """Parse a line of text and return the appropriate tag."""
    if _debug: statement_to_tag._debug("statement_to_tag %r", line)

    # look for a matching statement pattern
    for stmt_fn, stmt_re in statements:
        match = stmt_re.match(line)
        if match:
            break
    else:
        raise SyntaxError("syntax error: %r" % (line,))

    # extract the pieces captured by the pattern
    match_groups = match.groupdict()
    value = match_groups.get('value', None)
    context = match_groups.get('context', None)
    if _debug: statement_to_tag._debug("    - value: %r", value)
    if _debug: statement_to_tag._debug("    - context: %r", context)

    # let the function work on the value, skip blank lines
    element = stmt_fn(value)
    if not element:
        return None

    # check for element already a tag
    if isinstance(element, Tag):
        tag = element
        if context is not None:
            raise SyntaxError("syntax error: %r" % (line,))

    elif isinstance(element, Atomic):
        tag = Tag()
        element.encode(tag)
        if _debug: statement_to_tag._debug("    - encoded tag: %r", tag)

        if context is not None:
            tag = tag.app_to_context(int(context))
            if _debug: statement_to_tag._debug("    - with context: %r", tag)

    else:
        raise TypeError("element must be a tag or atomic")
    if _debug: statement_to_tag._debug("    - tag: %r", tag)

    return tag
예제 #4
0
def statement_to_tag(line):
    """Parse a line of text and return the appropriate tag."""
    if _debug: statement_to_tag._debug("statement_to_tag %r", line)

    # look for a matching statement pattern
    for stmt_fn, stmt_re in statements:
        match = stmt_re.match(line)
        if match:
            break
    else:
        raise SyntaxError("syntax error: %r" % (line, ))

    # extract the pieces captured by the pattern
    match_groups = match.groupdict()
    value = match_groups.get('value', None)
    context = match_groups.get('context', None)
    if _debug: statement_to_tag._debug("    - value: %r", value)
    if _debug: statement_to_tag._debug("    - context: %r", context)

    # let the function work on the value, skip blank lines
    element = stmt_fn(value)
    if not element:
        return None

    # check for element already a tag
    if isinstance(element, Tag):
        tag = element
        if context is not None:
            raise SyntaxError("syntax error: %r" % (line, ))

    elif isinstance(element, Atomic):
        tag = Tag()
        element.encode(tag)
        if _debug: statement_to_tag._debug("    - encoded tag: %r", tag)

        if context is not None:
            tag = tag.app_to_context(int(context))
            if _debug: statement_to_tag._debug("    - with context: %r", tag)

    else:
        raise TypeError("element must be a tag or atomic")
    if _debug: statement_to_tag._debug("    - tag: %r", tag)

    return tag
예제 #5
0
    def test_boolean_application_to_context(self):
        if _debug: TestApplicationTag._debug("test_boolean_application_to_context")

        # create an application
        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0)
        if _debug: TestApplicationTag._debug("    - tag: %r", tag_tuple(tag))

        # convert it to context tagged, context 0
        ctag = tag.app_to_context(0)
        if _debug: TestApplicationTag._debug("    - ctag: %r", tag_tuple(ctag))

        # create a context tag with the same shape
        ttag = ContextTag(0, xtob('00'))
        if _debug: TestApplicationTag._debug("    - ttag: %r", tag_tuple(ttag))

        # check to see they are the same
        assert ctag == ttag

        # convert the context tag back to an application tag
        dtag = ctag.context_to_app(Tag.booleanAppTag)
        if _debug: TestApplicationTag._debug("    - dtag: %r", tag_tuple(dtag))

        # check to see it round-tripped
        assert dtag == tag