# Path: asammdf/blocks/utils.py
# class MdfException(Exception):
#     """MDF Exception class"""
# 
#     pass
# 
# Path: asammdf/blocks/v4_blocks.py
# class TextBlock:
#     """common TXBLOCK and MDBLOCK class
# 
#     *TextBlock* has the following attributes, that are also available as
#     dict like key-value pairs
# 
#     TXBLOCK fields
# 
#     * ``id`` - bytes : block ID; b'##TX' for TXBLOCK and b'##MD' for MDBLOCK
#     * ``reserved0`` - int : reserved bytes
#     * ``block_len`` - int : block bytes size
#     * ``links_nr`` - int : number of links
#     * ``text`` - bytes : actual text content
# 
#     Other attributes
# 
#     * ``address`` - int : text block address
# 
#     Parameters
#     ----------
#     address : int
#         block address
#     stream : handle
#         file handle
#     meta : bool
#         flag to set the block type to MDBLOCK for dynamically created objects; default *False*
#     text : bytes/str
#         text content for dynamically created objects
# 
# 
#     """
# 
#     __slots__ = ("address", "id", "reserved0", "block_len", "links_nr", "text")
# 
#     def __init__(self, **kwargs) -> None:
# 
#         if "safe" in kwargs:
#             self.address = 0
#             text = kwargs["text"]
#             size = len(text)
#             self.id = b"##MD" if kwargs["meta"] else b"##TX"
#             self.reserved0 = 0
#             self.links_nr = 0
#             self.text = text
# 
#             self.block_len = size + 32 - size % 8
# 
#         elif "stream" in kwargs:
#             stream = kwargs["stream"]
#             mapped = kwargs.get("mapped", False) or not is_file_like(stream)
#             self.address = address = kwargs["address"]
# 
#             if mapped:
#                 (self.id, self.reserved0, self.block_len, self.links_nr) = COMMON_uf(
#                     stream, address
#                 )
# 
#                 size = self.block_len - COMMON_SIZE
# 
#                 if self.id not in (b"##TX", b"##MD"):
#                     message = f'Expected "##TX" or "##MD" block @{hex(address)} but found "{self.id}"'
#                     logger.exception(message)
#                     raise MdfException(message)
# 
#                 self.text = text = stream[
#                     address + COMMON_SIZE : address + self.block_len
#                 ]
# 
#             else:
#                 stream.seek(address)
#                 (self.id, self.reserved0, self.block_len, self.links_nr) = COMMON_u(
#                     stream.read(COMMON_SIZE)
#                 )
# 
#                 size = self.block_len - COMMON_SIZE
# 
#                 if self.id not in (b"##TX", b"##MD"):
#                     message = f'Expected "##TX" or "##MD" block @{hex(address)} but found "{self.id}"'
#                     logger.exception(message)
#                     raise MdfException(message)
# 
#                 self.text = text = stream.read(size)
# 
#             align = size % 8
#             if align:
#                 self.block_len = size + COMMON_SIZE + 8 - align
#             else:
#                 if text:
#                     if text[-1]:
#                         self.block_len += 8
#                 else:
#                     self.block_len += 8
# 
#         else:
# 
#             text = kwargs["text"]
# 
#             try:
#                 text = text.encode("utf-8", "replace")
#             except AttributeError:
#                 pass
# 
#             size = len(text)
# 
#             self.id = b"##MD" if kwargs.get("meta", False) else b"##TX"
#             self.reserved0 = 0
#             self.links_nr = 0
#             self.text = text
# 
#             self.block_len = size + 32 - size % 8
# 
#     def __getitem__(self, item: str) -> Any:
#         return self.__getattribute__(item)
# 
#     def __setitem__(self, item: str, value: Any) -> None:
#         self.__setattr__(item, value)
# 
#     def __bytes__(self) -> bytes:
#         return pack(
#             f"<4sI2Q{self.block_len - COMMON_SIZE}s",
#             self.id,
#             self.reserved0,
#             self.block_len,
#             self.links_nr,
#             self.text,
#         )
# 
#     def __repr__(self) -> str:
#         return (
#             f"TextBlock(id={self.id},"
#             f"reserved0={self.reserved0}, "
#             f"block_len={self.block_len}, "
#             f"links_nr={self.links_nr} "
#             f"text={self.text})"
#         )


from io import BytesIO
from asammdf.blocks.utils import MdfException
from asammdf.blocks.v4_blocks import TextBlock
import unittest
#!/usr/bin/env python



class TestATBLOCK(unittest.TestCase):
    @classmethod
    def setUpClass(cls):

        cls.plain_text = "sample text"
        cls.plain_bytes = b"##TX\x00\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00sample text\x00\x00\x00\x00\x00"
        cls.plain_stream = BytesIO()
        cls.plain_stream.write(cls.plain_bytes)

        cls.meta_text = "<CN>sample text</CN>"
        cls.meta_bytes = b"##MD\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00<CN>sample text</CN>\x00\x00\x00\x00"
        cls.meta_stream = BytesIO()
        cls.meta_stream.write(cls.meta_bytes)

    def test_read(self):
        self.plain_stream.seek(0)


# Path: asammdf/blocks/utils.py
# class MdfException(Exception):
#     """MDF Exception class"""
# 
#     pass
# 
# Path: asammdf/blocks/v4_blocks.py
# class TextBlock:
#     """common TXBLOCK and MDBLOCK class
# 
#     *TextBlock* has the following attributes, that are also available as
#     dict like key-value pairs
# 
#     TXBLOCK fields
# 
#     * ``id`` - bytes : block ID; b'##TX' for TXBLOCK and b'##MD' for MDBLOCK
#     * ``reserved0`` - int : reserved bytes
#     * ``block_len`` - int : block bytes size
#     * ``links_nr`` - int : number of links
#     * ``text`` - bytes : actual text content
# 
#     Other attributes
# 
#     * ``address`` - int : text block address
# 
#     Parameters
#     ----------
#     address : int
#         block address
#     stream : handle
#         file handle
#     meta : bool
#         flag to set the block type to MDBLOCK for dynamically created objects; default *False*
#     text : bytes/str
#         text content for dynamically created objects
# 
# 
#     """
# 
#     __slots__ = ("address", "id", "reserved0", "block_len", "links_nr", "text")
# 
#     def __init__(self, **kwargs) -> None:
# 
#         if "safe" in kwargs:
#             self.address = 0
#             text = kwargs["text"]
#             size = len(text)
#             self.id = b"##MD" if kwargs["meta"] else b"##TX"
#             self.reserved0 = 0
#             self.links_nr = 0
#             self.text = text
# 
#             self.block_len = size + 32 - size % 8
# 
#         elif "stream" in kwargs:
#             stream = kwargs["stream"]
#             mapped = kwargs.get("mapped", False) or not is_file_like(stream)
#             self.address = address = kwargs["address"]
# 
#             if mapped:
#                 (self.id, self.reserved0, self.block_len, self.links_nr) = COMMON_uf(
#                     stream, address
#                 )
# 
#                 size = self.block_len - COMMON_SIZE
# 
#                 if self.id not in (b"##TX", b"##MD"):
#                     message = f'Expected "##TX" or "##MD" block @{hex(address)} but found "{self.id}"'
#                     logger.exception(message)
#                     raise MdfException(message)
# 
#                 self.text = text = stream[
#                     address + COMMON_SIZE : address + self.block_len
#                 ]
# 
#             else:
#                 stream.seek(address)
#                 (self.id, self.reserved0, self.block_len, self.links_nr) = COMMON_u(
#                     stream.read(COMMON_SIZE)
#                 )
# 
#                 size = self.block_len - COMMON_SIZE
# 
#                 if self.id not in (b"##TX", b"##MD"):
#                     message = f'Expected "##TX" or "##MD" block @{hex(address)} but found "{self.id}"'
#                     logger.exception(message)
#                     raise MdfException(message)
# 
#                 self.text = text = stream.read(size)
# 
#             align = size % 8
#             if align:
#                 self.block_len = size + COMMON_SIZE + 8 - align
#             else:
#                 if text:
#                     if text[-1]:
#                         self.block_len += 8
#                 else:
#                     self.block_len += 8
# 
#         else:
# 
#             text = kwargs["text"]
# 
#             try:
#                 text = text.encode("utf-8", "replace")
#             except AttributeError:
#                 pass
# 
#             size = len(text)
# 
#             self.id = b"##MD" if kwargs.get("meta", False) else b"##TX"
#             self.reserved0 = 0
#             self.links_nr = 0
#             self.text = text
# 
#             self.block_len = size + 32 - size % 8
# 
#     def __getitem__(self, item: str) -> Any:
#         return self.__getattribute__(item)
# 
#     def __setitem__(self, item: str, value: Any) -> None:
#         self.__setattr__(item, value)
# 
#     def __bytes__(self) -> bytes:
#         return pack(
#             f"<4sI2Q{self.block_len - COMMON_SIZE}s",
#             self.id,
#             self.reserved0,
#             self.block_len,
#             self.links_nr,
#             self.text,
#         )
# 
#     def __repr__(self) -> str:
#         return (
#             f"TextBlock(id={self.id},"
#             f"reserved0={self.reserved0}, "
#             f"block_len={self.block_len}, "
#             f"links_nr={self.links_nr} "
#             f"text={self.text})"
#         )


# Path: test/test_v4_blocks/test_txblock.py
from io import BytesIO
from asammdf.blocks.utils import MdfException
from asammdf.blocks.v4_blocks import TextBlock
import unittest

#!/usr/bin/env python



class TestATBLOCK(unittest.TestCase):
    @classmethod
    def setUpClass(cls):

        cls.plain_text = "sample text"
        cls.plain_bytes = b"##TX\x00\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00sample text\x00\x00\x00\x00\x00"
        cls.plain_stream = BytesIO()
        cls.plain_stream.write(cls.plain_bytes)

        cls.meta_text = "<CN>sample text</CN>"
        cls.meta_bytes = b"##MD\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00<CN>sample text</CN>\x00\x00\x00\x00"
        cls.meta_stream = BytesIO()
        cls.meta_stream.write(cls.meta_bytes)

    def test_read(self):
        self.plain_stream.seek(0)


        block = TextBlock(address=0, stream=self.plain_stream)